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. |
I need 6 threads to update the global variable "Seats_Available".
|
All times are GMT -5. The time now is 02:25 PM. |