Operation Systems

ImplementationofMonitor

 

 

 

1          Producer/ConsumerwithSemaphores(35pts)

 

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.

2          Producer/ConsumerwithMonitor(65pts)

 

Thegoalofthisproblem istocreate yourownmonitortoprovidesynchronizationsupportforaproducer/consumerproblem.Youwillusethe pthreadpackagetocreateproducerthreadsand consumerthreads.Eachproducerthreadinsertsarandomlygeneratedalphabet(upperandlowercases)intothefirstavailableslota inbuffer.Eachconsumerthreadremovesacharacterfromthemost recentlyusedslotofthebuffer.Eachthreadthenrepeatstheprocess.Youwillperformyourworkincse.unl.edu.RefertoFigure2forthepseudo-code.Notethatthepseudo-codeonlysuggestsaguideline.Feelfreetoaddmoreparameters, variables, andfunctionsastheybecomenecessary.

 

  1. Youwill create anewvariabletypecalledconditionvariable(CV).Basically,conditionvariablesareusedtodelay processesorthreadsthatcannot continueexecutingduetospecific monitorstate(e.g.fullbuffer).Theyarealsousedto awakendelayedprocessesorthreadswhentheconditionsaresatisfiYouwillcreateanewstructurecalledcond.Thestructureconsistsofanintegervariablethatindicates thenumberofthreadsblockedonacondition variableandasemaphore thatisusedtosuspendthreads.TherearethreeoperationsthatcanbeperformedontheCV.Theyare:

 

  • count(cv)—returnsthenumberofthreadsblocked onthe
  • wait(cv)—relinquishesexclusiveaccesstothemonitorandthensuspendsthe
  • signal(cv)—unblocksonethreadsuspendedattheheadofthecvThesignaledthreadresumesexecutionwhereitwaslastsuspended.

 

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:

 

  • Howwouldyouguaranteethatonlyonethreadisinside themonitor atonetime?
  • Willyourmonitorfollowthesignalandwaitorsignalandcontinuediscipline?
  • Howwouldyoumakesure thatasuspendedthread (duetowait)resumeswhereitleft off?
  • Howwouldyouinitializethenecessarydatastructurestosupportyourmonitorandmakethemvisible toallthreads?

Please include answers to the above questions in the readme file. Include the strategy you choose

 

  1. You will create function mon_insert that inserts a character into the buffer. If the buffer is full, it invokes wait on the condition variable full. It also invokes signal on condition variable empty.
  2. You will create function mon_remove that removes a character from the buffer. If the buffer is empty, it invokes wait on the condition variable empty. It also invokes signal on condition variable full. The function returns the removed character.

 

3          Program Specification

 

  1. Implement switch

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.

  1. Output Convention

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).

  1. Exit

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.

 

4          Submissionprocedure

 

Createazipfilethathasallyoursolutionsandsubmitthroughhandin.Thestepstocreateproperdirectorystructure areasfollows:

  1. Create a directory called lastname_pa2 (replace lastname with your lastname).
  2. Create subdirectories: prob1 and prob2 under lastname_pa2.
  3. Place your solutions in the proper directory. Provide README.txt file for each problem. Each README file should specify:
  1. Specificinstructionsonhowtotest
  2. Specifywhetheryour monitorfollowssignal andwaitorsignalandcontinue

 

  1. Add makefile for both the problems. The name of the executable produced by first part and second part should be “part1” and “part2”
  • Once all solutions are properly stored, zip the folder lastname_pa2 and submit the zip file through handin.

 

5          Helpful Links

http://beej.us/guide/bgipc/output/html/multipage/index.html

 

 

 

 

 

Order from us and get better grades. We are the service you have been looking for.