Producer and consumer problem There should be a shared resou

Producer and consumer problem. There should be a shared resource that they both share. You could implement it either by developing two programs with shared file or single program forked to implement consumer and producer threads. Apply Mutex to the consumer/producer program earlier implemented to make sure shared data remains secure.

Solution

// global1.c #include #include static volatile int balance = 0; void *deposit(void *param) { char *who = param; int i; printf(\"%s: begin\ \", who); for (i = 0; i < 1000000; i++) { balance = balance + 1; } printf(\"%s: done\ \", who); return NULL; } int main() { pthread_t p1, p2; printf(\"main() starts depositing, balance = %d\ \", balance); pthread_create(&p1;, NULL, deposit, \"A\"); pthread_create(&p2;, NULL, deposit, \"B\"); // join waits for the threads to finish pthread_join(p1, NULL); pthread_join(p2, NULL); printf(\"main() A and B finished, balance = %d\ \", balance); return 0; }
Producer and consumer problem. There should be a shared resource that they both share. You could implement it either by developing two programs with shared file

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site