How to use shared memory with fork() ?

P

priyanka

I am writing a simple code in which parent and child process are
sharing one queue using shared memory method. But when child process
modifies the shared data its somehow not reflecting for parent process
and parent process is still accessing the previous value. Here I am
attaching a sample code what am I trying to do:

int main()
{
/* Declare fork variables */
pid_t childpid;

/* Declare shared memory variables */
key_t key;
int shmid;
int *front;
int *rear;
int i;
/* Declare semaphore variables */
sem_t sem;
int pshared = 1;
unsigned int value = 1;

/* Initialize Shared Memory */
key = ftok("thread1.c",'R');
shmid = shmget(key, 1024, 0644 | IPC_CREAT);

/* Attach to Shared Memory */
rear = shmat(shmid, (void *)0, 0);
if(rear == (int *)(-1))
perror("shmat");

front = shmat(shmid, (void *)0, 0);
if(front == (int *)(-1))
perror("shmat");

/* Write initial value to shared memory */
rear= front;
/* Write to Shared Memory */
for(i=0; i<5; i++)
{
rear = 5000+(i+1);
}
printf("value of front is: %d", *front);
rear = &rear[i-1];
/* Initialize Semaphore */
if((sem_init(&sem, pshared, value)) == 1)
{
perror("Error initializing semaphore");
exit(1);
}

if((childpid = fork()) < 0) // error occured
{
perror("Fork Failed");
exit(1);
}
else if(childpid == 0) // child process
{
printf("came in child process\n");
sem_wait(&sem);
for(i=0; i<3; i++){
printf("rear is: %d", *rear);
rear--;
}
sem_post(&sem);
shmdt(rear);

}
else // parent process
{
/* Write to Shared Memory */
wait(1);
sem_wait(&sem);
printf("came in parent process and rear is: %d\n", *rear);
sem_post(&sem);
shmdt(rear);
}
return 0;
}

What can be the reason that modified shared data is not reflecting ?

Thanks in advance.
 
B

Barry Schwarz

I am writing a simple code in which parent and child process are
sharing one queue using shared memory method. But when child process
modifies the shared data its somehow not reflecting for parent process
and parent process is still accessing the previous value. Here I am
attaching a sample code what am I trying to do:

Since most of the functions you call are not part of the standard C
library, you will probably have better luck asking your question in a
newsgroup that discusses your system (comp.unix.programmer?).
 
M

Malcolm McLean

The semaphores look a bit suspicious.

Parent needs to set the semaphore flag, write to the shared memory,
then clear the semaphore flag.

Child needs to wait for the sempahore to be cleared, then read the
shared memory. But child needs to distinguish between a cleared
semaphore and parent not having written the data yet.

I'm notr familiar with the calls, but it doesn't seem to me you're
doing this.
 
P

priyanka

The semaphores look a bit suspicious.

Parent needs to set the semaphore flag, write to the shared memory,
then clear the semaphore flag.

Child needs to wait for the sempahore to be cleared, then read the
shared memory. But child needs to distinguish between a cleared
semaphore and parent not having written the data yet.

I'm notr familiar with the calls, but it doesn't seem to me you're
doing this.

Sem_wait and sem_post(from POSIX) I used for that.
 
M

Malcolm McLean

Sem_wait and sem_post(from POSIX) I used for that.
I'm not familiar with the calls. But child calls sem_wait(), whilst
parent idles for a millesecond, then also calls sem_wait(). So where
will the two process be when they exit form the sem_wait() call?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

place stl container object on shared memory 15
fork() 5
fork() 27
Question about struct in shared memory (C on linux) 4
Help understanding some C code 12
C pipe 1
'fork' query 3
Linux: using "clone3" and "waitid" 0

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top