LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to use fork/pipe to decrement a global varialbe Y and exit when Y is 0 (https://www.linuxquestions.org/questions/programming-9/how-to-use-fork-pipe-to-decrement-a-global-varialbe-y-and-exit-when-y-is-0-a-292488/)

keiwu 02-19-2005 11:32 PM

how to use fork/pipe to decrement a global varialbe Y and exit when Y is 0
 
suppose I have the following already:

#include <stdio.h>
#include <stdlib.h>


int Seats_Available=36;

void thread(int id)
{
while (1)
{

if (Seats_Available <=0)
return;

if ( rand()%4==0 && Seats_Available < 36)
{
Seats_Available++;
printf("Agent %d cancel a seat. ", id);
printf("Seats available: %d \n", Seats_Available);
sleep(1);
}

else
{
Seats_Available--;
printf("Agent %d books a seat. ", id);
printf("Seats available: %d \n", Seats_Available);
}

}
}

How do I write use fork, pipe to update the Seats_Available, and finishes when Seats_Available is 0?
Please give me some hint.

keiwu 02-19-2005 11:33 PM

I need 6 threads to update the global variable "Seats_Available".


All times are GMT -5. The time now is 02:25 PM.