1 / 15

CS470 Lab 4

CS470 Lab 4. TA Notes. Objective. Simulate the activities of a producer and consumer – Page 326 Use thread synchronization to solve the producer-consumer problem – Page 329 You will need to use Full & Empty Buffers Simulate this with an array of size 25. Objective cont.

Télécharger la présentation

CS470 Lab 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS470 Lab 4 TA Notes

  2. Objective • Simulate the activities of a producer and consumer – Page 326 • Use thread synchronization to solve the producer-consumer problem – Page 329 • You will need to use Full & Empty Buffers • Simulate this with an array of size 25

  3. Objective cont. • If the current EMPTY slot is k and producer fills it, the the next EMPTY slot will be (k+1)%N where N is the size of the array • In this simulation the consumer thread is slower than the producer thread. • The producer produces widgets which are represented by an ever increasing widget number.

  4. Objective cont. • The consumer consumes a widget by replacing it with an EMPTY indicator (-1)

  5. Sample Output Producer: Produce widget #0 in buffer #0 0|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #0 in buffer #0 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #1 in buffer #1 -1|1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #1 in buffer #1 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #2 in buffer #2 -1|-1|2|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #2 in buffer #2 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|

  6. Output cont. Producer: Produce widget #3 in buffer #3 -1|-1|-1|3|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #3 in buffer #3 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #4 in buffer #4 -1|-1|-1|-1|4|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #4 in buffer #4 -1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #5 in buffer #5 -1|-1|-1|-1|-1|5|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Producer: Produce widget #6 in buffer #6 -1|-1|-1|-1|-1|5|6|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1| Consumer: Consumed widget #5 in buffer #5 -1|-1|-1|-1|-1|-1|6|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|-1|

  7. Shared Variables • An integer array (size 25 for our simulation) • Two other fields nextEmpty(initial=0) and nextFull(initial=0) • Three handles for the synchronization objects: full, empty, and mutex • Keep a running flag as in Project 2

  8. CS 470Lab 4 Program Layout

  9. Overall Layout • Producer Function • Consumer Function • Driver (main)

  10. Producer Say that its running Initialize Widget Number While(runFlag) { Simulate producing by sleeping a random time (0..200) And incrementing the widget number P(empty) /*If there is an empty spot we can add to the buffer*/ P(mutex) /*If the consumer is not in it’s critical section the producer can be in the critical section*/ Store the widget number in the next available empty slot Say what you did Say what the buffer looks like (show the buffer) Update the next available empty position V(mutex) /*Exit critical section*/ V(full) /*Make sure to let the consumer know there is at least one widget*/ } Say terminating Return (DWORD) 0

  11. Consumer Say running While(runFlag) { P(full) /*If there is a Widget we (consumer) can go, wait otherwise*/ P(mutex) /* If the producer is not in its critical section we can go*/ Get the widget number in the next available full slot, And store an empty holder (-1) there Say what you did Say what the buffer looks like (show the buffer) Update the next available full position V(mutex) /*Exit our critical section */ V(empty) /* Let the producer know there is an open spot for a widget */ Simulate consuming by sleeping a random time (0..300) } Say terminating Return (DWORD) 0

  12. Driver Driver Error check input argc (the number of parameters) Create synchronization objects(mutex as a MutexObject, full and empty as SemaphoreObjects) Initialize the buffer record so that nextFull = 0 nextEmpty = 0, And all array positions represent empty(-1) Generate the producer thread Generate the consumer thread Generate and set a waitable timer Wait for the timer’s signal Change the keep running signal to FALSE Wait for thread objects to terminate Clean up by closing handles Say done

  13. 2006 addition • Program 4: Create a program that has two threads, one that fills a buffer and another that empties it. The rate at which the buffer is filled and emptied is variable. In doing this, one thread might have to wait for the other to process the buffer to continue. • General program comments: • Define handles globally, there should be a mutex, full, empty, and one for each thread. • Define a producer and consumer sleep time, or use random times. Up to you. • Producer and consumer methods should be defined as: • DWORD WINAPI producer( LPVOID ) • DWORD WINAPI consumer( LPVOID ) • Main pseudo-code: • initialize variables, widgets, random number generator • error check run time input • create semaphores and mutex • set the timer • say running • create consumer and producer threads • wait until timer is done • say finishing, set run flag to false • close handles

  14. 2006 addition • Producer pseudo-code: • say running • initialize widget number • while run flag • sleep for sleep time or random time • wait for empty and mutex • store widget in empty slot • say what the buffer looks like • update next available widget position • release mutex and full • say terminating

  15. 2006 addition • Consumer pseudo-code: • say running • while run flag • wait for full and mutex • say what you did • get widget number from next available full slot, store -1 there • say what buffer looks like • update next available full position • release mutex and empty • sleep for sleep time or random time • say terminating • Here are the msdn links for using semaphores and mutex objects: • http://msdn.microsoft.com/library/en-us/dllproc/base/using_semaphore_objects.asp • http://msdn.microsoft.com/library/en-us/dllproc/base/using_mutex_objects.asp

More Related