2 * Procon1.Cpp - Producer/Consumer Demonstration Program 1 :Jss
Total Page:16
File Type:pdf, Size:1020Kb
Feb 20 09:29 procon1.cpp Page 1
1 /* 2 * "procon1.cpp" - producer/consumer demonstration program 1 :jss 3 * first attempt: UNsynchronized 4 * 5 * build with: c++ procon1.cpp -lpthread 6 */ 7 8 #include
Feb 20 09:29 procon1.cpp Page 2
61 /***** C O N S U M E R C L A S S *******************************/ 62 63 class Consumer { 64 public: 65 Consumer( Buffer &buf ): sharedLoc(buf) 66 { cout << "consumer created" << endl; } 67 void run(); // method to simulate a consumer 68 private: 69 Buffer &sharedLoc; // reference to global shared buffer 70 }; 71 72 // read & sum shared buffer 4 times at random times, report sum 73 void Consumer::run() 74 { 75 int sum = 0; // for running sum of buffer get values 76 for (int count = 1; count <= 4; count++) 77 { 78 sleep( rand() % 3 + 1 ); // simulate some consumer processing 79 sum += sharedLoc.get( "consumer" ); // retrieve a datum from buffer 80 } 81 cout << "consumer finished, sum = " << sum << endl; 82 } 83 84 /***** M A I N P R O G R A M C O D E *************************/ 85 86 Buffer buf; // this is the global shared buffer object 87 88 void *producer_thread(void *) 89 { 90 Producer p(buf); // instantiate one object of class Producer 91 p.run(); // be a producer 92 } 93 94 void *consumer_thread(void *) 95 { 96 Consumer c(buf); // instantiate one object of class Consumer 97 c.run(); // be a consumer 98 } 99 100 // main thread is the test-driver 101 int main() 102 { 103 pthread_t ptid, ctid; // producer, consumer thread IDs 104 105 srand(time(NULL)); // seed random number generator 106 cout << "\nThreaded Producer/Consumer Test, first attempt...\n\n"; 107 cout << "current buf value = " << buf.get("main") << endl; 108 // create & launch producer thread 109 if (pthread_create(&ptid, NULL, producer_thread, NULL) != 0) 110 perror("pthread_create() error, could not create producer thread"); 111 // create & launch consumer thread 112 if (pthread_create(&ctid, NULL, consumer_thread, NULL) != 0) 113 perror("pthread_create() error, could not create consumer thread"); 114 // wait for the 2 threads to finish 115 pthread_join(ptid, NULL); 116 pthread_join(ctid, NULL); 117 cout << "\nexiting program...\n"; 118 } Sample runs
~/400- c++ procon1.cpp -lpthread ~/400- a.out # RUN 1
Threaded Producer/Consumer Test, first attempt... < main reading -1 current buf value = -1 producer created consumer created > producer writing 1 < consumer reading 1 < consumer reading 1 > producer writing 2 > producer writing 3 < consumer reading 3 > producer writing 4 producer finished. < consumer reading 4 consumer finished, sum = 9
~/400- a.out # RUN 2
Threaded Producer/Consumer Test, first attempt... < main reading -1 current buf value = -1 producer created consumer created > producer writing 1 > producer writing 2 < consumer reading 2 > producer writing 3 < consumer reading 3 > producer writing 4 producer finished. < consumer reading 4 < consumer reading 4 consumer finished, sum = 13
~/400- a.out # RUN 3
Threaded Producer/Consumer Test, first attempt... < main reading -1 current buf value = -1 producer created consumer created < consumer reading -1 > producer writing 1 < consumer reading 1 > producer writing 2 < consumer reading 2 < consumer reading 2 consumer finished, sum = 4 > producer writing 3 > producer writing 4 producer finished. Feb 20 09:33 procon2.cpp Page 1
1 /* 2 * "procon2.cpp" - producer/consumer demonstration program 2 :jss 3 * second attempt: synchronized via busy-wait solution :-( 4 * 5 * build with: c++ procon2.cpp -lpthread 6 */ 7 8 #include
Feb 20 09:33 procon2.cpp Page 2
65 /***** C O N S U M E R C L A S S *******************************/ 66 67 class Consumer { 68 public: 69 Consumer( Buffer &buf ): sharedLoc(buf) 70 { cout << "consumer created\n"; } 71 void run(); // method to simulate a consumer 72 private: 73 Buffer &sharedLoc; // reference to global shared buffer 74 }; 75 76 // read & sum shared buffer 4 times at random times, report sum 77 void Consumer::run() 78 { 79 int sum = 0; // for running sum of buffer get values 80 for (int count = 1; count <= 4; count++) 81 { 82 sleep( rand() % 3 + 1 ); // simulate some consumer processing 83 sum += sharedLoc.get( "consumer" ); // retrieve a datum from buffer 84 } 85 cout << "consumer finished, sum = " << sum << endl; 86 } 87 88 /***** M A I N P R O G R A M C O D E *************************/ 89 90 Buffer buf; // this is the global shared buffer object 91 92 void *producer_thread(void *) 93 { 94 Producer p(buf); // instantiate one object of class Producer 95 p.run(); // be a producer 96 } 97 98 void *consumer_thread(void *) 99 { 100 Consumer c(buf); // instantiate one object of class Consumer 101 c.run(); // be a consumer 102 } 103 104 // main thread is the test-driver 105 int main() 106 { 107 pthread_t ptid, ctid; // producer, consumer thread IDs 108 109 srand(time(NULL)); // seed random number generator 110 cout << "\nThreaded Producer/Consumer Test w/ busy-wait...\n\n"; 111 // cout << "current buf value = " << buf.get("main") << endl; // can't do! 112 // create & launch producer thread 113 if (pthread_create(&ptid, NULL, producer_thread, NULL) != 0) 114 perror("pthread_create() error, could not create producer thread"); 115 // create & launch consumer thread 116 if (pthread_create(&ctid, NULL, consumer_thread, NULL) != 0) 117 perror("pthread_create() error, could not create consumer thread"); 118 // wait for the 2 threads to finish 119 pthread_join(ptid, NULL); 120 pthread_join(ctid, NULL); 121 cout << "\nexiting program...\n"; 122 }Feb 13 17:53 procon3.c Page 1
1/* 2 * "procon3.c" - producer/consumer demonstration program 3 :jss 3 * 3rd attempt: semaphore-controlled to guarantee correct results :-) 4 * 5 * build with: cc procon3.c -lpthread 7 */ 8 9#include