LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Forking 4 writers for semaphore/shared memory ?Help? please. (https://www.linuxquestions.org/questions/linux-newbie-8/forking-4-writers-for-semaphore-shared-memory-help-please-772066/)

eaglei22 11-28-2009 05:25 AM

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);

stoggy 11-29-2009 08:21 AM

If I have 2 coins that total 30 cents and 1 is not a quarter what are they?


All times are GMT -5. The time now is 02:53 AM.