Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include #define NUM_LOAVES_PER_BATCH 7 #define NUM_BATCHES 6 #define NUM_LOAVES_TO_EAT 14 /** This system has four threads: the duck, cat, and dog that

#include  #include  #include  #include  #define NUM_LOAVES_PER_BATCH 7 #define NUM_BATCHES 6 #define NUM_LOAVES_TO_EAT 14 /** This system has four threads: the duck, cat, and dog that eat bread, and the little red hen that makes the bread. The little red hen makes seven loaves per batch, but she only has the patience for six batches. The little red hen only makes a batch if there are no loaves left. The other three animals each want to eat 14 loaves of bread, but only one of them can be in the kitchen at a time (to avoid fights over who gets what bread). When the duck, cat, or dog notices that there are no loaves of bread available, they complain to the little red hen and wait (in the kitchen) for the next batch to be ready. Use semaphores to enforce this constraint. Note: the global numLoaves variable should be left as is (i.e. do not make it a semaphore). **/ int numLoaves; void *littleRedHenThread(void *arg) { char *name = (char*)arg; int batch; for (batch = 1; batch <= 6; batch++) { sleep(2); // just makes it obvious that it won't work without // semaphores numLoaves += 7; printf("%-20s: A fresh batch of bread is ready. ", name); } printf("%-20s: I'm fed up with feeding you lazy animals! " "No more bread! ", name); return NULL; } void *otherAnimalThread(void *arg) { char *name = (char*)arg; int numLoavesEaten = 0; while (numLoavesEaten < NUM_LOAVES_TO_EAT) { if (numLoaves <= 0) { printf("%-20s: Hey, Little Red Hen, make some more bread! ", name); } numLoaves--; printf("%-20s: Mmm, this loaf is delicious. ", name); numLoavesEaten++; if (random() > random()) { // Adds variety to output sleep(1); } } printf("%-20s: I've had my fill of bread. Thanks, Little Red Hen! ", name); return NULL; } int main(int argc, char **argv) { pthread_t dog, cat, duck, hen; numLoaves = 0; char dogName[] = "Lazy Dog"; char catName[] = "Sleepy Cat"; char duckName[] = "Noisy Yellow Duck"; char henName[] = "Little Red Hen"; pthread_create(&dog, NULL, otherAnimalThread, dogName); pthread_create(&cat, NULL, otherAnimalThread, catName); pthread_create(&duck, NULL, otherAnimalThread, duckName); pthread_create(&hen, NULL, littleRedHenThread, henName); pthread_join(dog, NULL); pthread_join(cat, NULL); pthread_join(duck, NULL); pthread_join(hen, NULL); printf("Everything finished. "); } 

Sample Output:

Please make a balloon alligator. I am making a balloon alligator now. Thanks for my balloon alligator, Bryan! Please make a balloon starfish. I am making a balloon starfish now. Thanks for my balloon starfish, Bryan! Please make a balloon lion. I am making a balloon lion now. Thanks for my balloon lion, Bryan! Please make a balloon pterodactyl. I am making a balloon pterodactyl now. Thanks for my balloon pterodactyl, Bryan! Please make a balloon praying mantis. I am making a balloon praying mantis now. Thanks for my balloon praying mantis, Bryan! Please make a balloon rhino. I am making a balloon rhino now. Thanks for my balloon rhino, Bryan! Please make a balloon dog. I am making a balloon dog now. Thanks for my balloon dog, Bryan! Please make a balloon worm. I am making a balloon worm now. Thanks for my balloon worm, Bryan! Please make a balloon snail. I am making a balloon snail now. Thanks for my balloon snail, Bryan! Please make a balloon cat. I am making a balloon cat now. Thanks for my balloon cat, Bryan! Please make a balloon giraffe. I am making a balloon giraffe now. Thanks for my balloon giraffe, Bryan! Please make a balloon monkey. I am making a balloon monkey now. Thanks for my balloon monkey, Bryan! Please make a balloon catfish. I am making a balloon catfish now. Thanks for my balloon catfish, Bryan! Please make a balloon goose. I am making a balloon goose now. Thanks for my balloon goose, Bryan! Please make a balloon lemur. I am making a balloon lemur now. Thanks for my balloon lemur, Bryan! I'm all done making balloons. Bye, kids! Everything finished.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

6. Conduct an implementation study of mini/focus lesson.

Answered: 1 week ago