ImplementationofMonitor
The goal of this problem is to solve a producer/consumer problem using semaphores. You will use the pthread package to create producer threads and consumer threads. Each producer thread inserts character ‘X’ into a buffer. Each consumer thread removes the most-recently-inserted character from the buffer. Each thread then repeats the process. You will perform your work in cse.unl.edu. Use POSIX semaphore (sem_init, sem_get, sem_post, etc.). The pseudo-code is given in Figure 1. Note that the pseudo-code only suggests a guideline. Feel free to add more parameters, variables, and functions as they become necessary.
Thegoalofthisproblem istocreate yourownmonitortoprovidesynchronizationsupportforaproducer/consumerproblem.Youwillusethe pthreadpackagetocreateproducerthreadsand consumerthreads.Eachproducerthreadinsertsarandomlygeneratedalphabet(upperandlowercases)intothefirstavailableslota inbuffer.Eachconsumerthreadremovesacharacterfromthemost recentlyusedslotofthebuffer.Eachthreadthenrepeatstheprocess.Youwillperformyourworkincse.unl.edu.RefertoFigure2forthepseudo-code.Notethatthepseudo-codeonlysuggestsaguideline.Feelfreetoaddmoreparameters, variables, andfunctionsastheybecomenecessary.
Notice that you are not allowed to use existing condition variables or synchronization mechanisms such as one from pthread library (pthread_cond_init, or any of the other pthread_) You should be able to complete the assignment with semwait and semsignal. In addition, pay special attention to the following questions during your implementation:
Please include answers to the above questions in the readme file. Include the strategy you choose
To make the program easier to evaluate, we will be implementing additional switches to specify the buffer length, number of producers and the number of consumers as indicated below.
Switch | Specifies |
-b | Buffer length in bytes |
-p | Number of producer threads |
-c | Number of Consumer threads |
-i | Number of Items to insert |
eg: cse> ./part1 –b 1000 –p 10 –c 10 -i 100000
should create a buffer of length 1000 bytes, 10 producer threads and 10 consumer threads.
Whenever an item is inserted or removed from the buffer, a message needs to be printed to the screen with the following convention.
“p:<%u>, item: %c, at %d”, threadid, item, bufferPos
This is to indicate the (p)roducer thread with <threadid>, inserted an item at the index in buffer.
The same goes with consumer thread as well. Here we use (c) instead of (p).
The producer thread must exit when it has no items to be inserted to buffer.
The consumer thread must exit when the buffer is empty and it has consumed all the items. Manage this by using a counter for number of items inserted and removed.
Createazipfilethathasallyoursolutionsandsubmitthroughhandin.Thestepstocreateproperdirectorystructure areasfollows:
http://beej.us/guide/bgipc/output/html/multipage/index.html