Forking 4 writers for semaphore/shared memory ?Help? please.
I'm currently working on a program where you have to make two separate programs. One is a reader, the other is a writer and 2 semaphores are used, created in the reader. The writer changes some things in shared memory and the reader reads it and prints it out. I have it working correctly with one writer process. The problem is the writer program is supposed to create 4 writers. I'm stuck on how to do this.
There is supposed to be a loop in both the reader and writer and both will end when shared memory reaches 0. I'm assuming only the loop in the writer should be forked.
Here is my forked attempt in the writer area..
for (int p_count = 1; p_count <= 4; p_count++)
{ if (-1 == (pid1 = fork()))
{ perror ("error in fork");
exit (1);
}
}
if(pid1==0)
{
while(val >=0)
{
////sem wait
val--;
if (semop(semid, &semb, 1) == -1)
{
perror("semop");
exit(1);
}
cout<<"writer "<<semctl(semid, semb.sem_num, GETVAL)
<<" semaphores in C.S.: 0 0"<<endl;
*shm = val;
////sem signal
semb.sem_op = 1;
semb.sem_num = 1;
if (semop(semid, &semb, 1) == -1)
{
perror("semop");
exit(1);
}
sleep(1);
}
exit(0);
}
else
wait(&status);
|