![]() ![]() For this project, you will use standard counting semaphores forĮmpty and full and a mutex lock, rather than a binary semaphore, to represent mutex. The solution presented in Section 5.7.1 uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary (or mutualexclusion) semaphore that protects the actual insertion or removal of items in the buffer. Refer to that section for specific instructions on creating threads.Design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in Figures 5.9 and 5.10. Windows Threads Section 4.4.2 discusses thread creation using the Windows API. Refer to those sections for specific instructions on Pthreads thread creation and synchronization. Coverage of mutex locks and semaphores using Pthreads is provided in Section 7.3. Pthreads Thread Creation and Synchronization Creating threads using the Pthreads API is discussed in Section 4.4.1. In the following sections, we supply more information on each of these choices. An outline of the producer and consumer threads appears in Figure 7.16.įigure 7.16 An outline of the producer and consumer threads.Īs noted earlier, you can solve this problem using either Pthreads or the Windows API. The consumer will also sleep for a random period of time and, upon awakening, will attempt to remove an item from the buffer. Random numbers will be produced using the rand() function, which produces random integers between 0 and RAND_MAX. The Producer and Consumer Threads The producer thread will alternate between sleeping for a random period of time and inserting a random integer into the buffer. Get command line arguments argv,argv,argv */ The number of consumer threads A skeleton for this function appears in Figure 7.15. The main() function will be passed three parameters on the command line:ġ. Once it has created the producer and consumer threads, the main() function will sleep for a period of time and, upon awakening, will terminate the application. The main() function will initialize the buffer and create the separate producer and consumer threads. The buffer will also require an initialization function that initializes the mutual-exclusion object mutex along with the empty and full semaphores. The insert_item() and remove_item() functions will synchronize the producer and consumer using the algorithms outlined in Figure 7.1 and Figure 7.2. Return -1 indicating an error condition */įigure 7.14 Outline of buffer operations. A skeleton outlining these functions appears in Figure 7.14. The buffer will be manipulated with two functions, insert_item() and remove_item(), which are called by the producer and consumer threads, respectively. The definition of buffer_item, along with the size of the buffer, can be stored in a header file such as the following: The array of buffer_item objects will be manipulated as a circular queue. The Buffer Internally, the buffer will consist of a fixed-size array of type buffer_item (which will be defined using a typedef). These are the notes to help create the program: You can solve this problem using either Pthreads or the Windows API. The producer and consumer-running as separate threads-will move items to and from a buffer that is synchronized with the empty, full, and mutex structures. For this project, you will use standard counting semaphores for empty and full and a mutex lock, rather than a binary semaphore, to represent mutex. ![]() ![]() The solution presented in Section 7.1.1 uses three semaphores: empty and full, which count the number of empty and full slots in the buffer, and mutex, which is a binary (or mutual-exclusion) semaphore that protects the actual insertion or removal of items in the buffer. In this project, you will design a programming solution to the bounded-buffer problem using the producer and consumer processes shown in Figures Section 5.9 and 5.10. In Section 7.1.1, we presented a semaphore-based solution to the producer-consumer problem using a bounded buffer. ![]()
0 Comments
Leave a Reply. |