trouble with race conditions?

I'm supposed to allocate a shared memory segment that contains an integer variable and set it to zero. This creates a clone that will start adding 1 to the shared memory segment's integer 100,000,000 times. The parent process will add 1 to the shared memory segment's integer 100,000,000 times. The parent will make sure that the child process has completed and then it will display the value of integer in shared memory.The parent then deletes the memory segment.

I have the shared memory segment and the deleting. any other help is greatly appreciated :) I'm very confused

<code>

#include <iostream>
using namespace std;

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>

const int SHMSZ = 16000;
const int SHMSZ = 20000;

int main()
{
int sid;
key_t key;
int *shm_ptr;
//char *sbuff;
if( (key = ftok( ".", 'a' )) == -1 ) {
perror( "ftok" );
return 1;
}

if( (sid = shmget( key, 1*sizeof(int), IPC_CREAT | SHM_R | SHM_W | SHM_R >> 6
| SHM_W >> 6 )) == -1 ) {
perror( "shmget" );
return 1;
}
if( (sbuff = (char *) shmat( sid, NULL, 0 )) == (void *) -1 ) {
if( (sbuff = (char *) shmat( sid, NULL, SHM_RDONLY )) == (void *) -1 ) {

perror( "shmat" );
return 1;
}
memcpy( shm_ptr, 0, 1 );
for (int i = 0, i < 100000, i++) {
for (int j = 0; j < 100000, j++) {

}
}

sleep( 300 );

sleep(10);
cout << "Removing shared memory" << endl;
if( shmctl( sid, IPC_RMID, NULL ) == -1 ) {
perror( "shmctl" );
return 1;
}

if( shmdt( sbuff ) )
{
perror( "shmdt" );
return 1;
}
exit( 0 );
}
</code>
Last edited on
Use fork() to create the child process. The return value from fork() tells you if it's the parent or child. After the parent increments the counter, it should call wait() to wait for the child to exit. Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
int pid = fork();
if (pid < 0) {
    cerr << "Fork failed\n";
    exit(1);
}

for (int i=0; i<100000; i++) (*shmptr)++;  // parent and child execute this
if (pid == 0) {
    // child
    exit(0);
}

// Parent. Detach. 
thanks I think i get it now. or i'm really close. i'm not sure if i put everything where it's supposed to go. i have the integer in shared memory that is set to 0, then i have the adding 100,000 to parent and child, after child is completed it prints the value of the integer, then the parent deletes the shared memory segment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <stdlib.h>

const int SHMSZ = 16000;
const int SHMSZ = 20000;

int main()
{
    int sid;
    key_t key;
    int *shm_ptr;     
    if( (key = ftok( ".", 'a' )) == -1 ) {
        perror( "ftok" );
        return 1;
    }

    if( (sid = shmget( key, 1*sizeof(int), IPC_CREAT | SHM_R | SHM_W | SHM_R >> 6
      | SHM_W >> 6 )) == -1 ) {
        perror( "shmget" );
        return 1;
    }
    if( (sbuff = (char *) shmat( sid, NULL, 0 )) == (void *) -1 ) {
      if( (sbuff = (char *) shmat( sid, NULL, SHM_RDONLY )) == (void *) -1 ) {

        perror( "shmat" );
        return 1;
    }
    memcpy( shm_ptr, 0, 1 );
    int pid = fork();
    if (pid < 0) {
    cerr << "Fork failed\n";
    wait(2);
    exit(1);
    }

    for (int i=0; i<100000; i++) (*shmptr)++;  \
    if (pid == 0) {
    exit(0);
    }

  sleep( 300 ); 
    printf ("The contents of the shared memory is:\n%s\n", *shm_ptr);
  sleep(10);
    cout << "Removing shared memory" << endl;
    if( shmctl( sid, IPC_RMID, NULL ) == -1 ) {
        perror( "shmctl" );
        return 1;
    }

    if( shmdt( sbuff ) )
    {
    	perror( "shmdt" );
    	return 1;
    }
    exit( 0 );
}
Last edited on
Line 12: SMHSZ is redefined from the line before. Why?
Line 29: sbuff is undefined.
Line 35: shm_ptr is uninitialized. Also, you're copying one byte from address 0. That's wrong.
Line 48. Use wait() to wait for the child.

And in all of this, you're using 2 processes to increment a shared bit of memory. You need a mutex for this or you need to be certain that the increment is atomic.
Topic archived. No new replies allowed.