Pseudocode- Semaphore Abstract

Added on - 16 Sep 2019

  • Dissertation

    type

  • 5

    pages

  • 1114

    words

  • 143

    views

  • 0

    downloads

Showing pages 1 to 2 of 5 pages
1.Please complete the following pseudocode to implement the semaphore abstract.publicclassSemaphore {// A private variable to set the bufferPrivate int value;voidinit(intval){//initialize value if val>0}publicvoidWait(){repeatwait();until value is 0decrement the value}voidsignal(){If value is 0Notify();End ifIncrement value;}}2.Write the Pseudocode for the following problems: ssignalpose there are three threads A,B and C. A keeps producing an item into a buffer B1 of size M, B keeps taking an itemfrom B1, and put into another buffer B2 of size N. C keeps consuming an item frombuffer B2. Please write the pseudocode for Thread A,B,Ca.Using the semaphores class defined above to synchronize them.b.Using the Monitor abstract to synchronize them.A)Initialize Buffer b1 with buffer size M.semaphoremutex = 1; // Controls access to critical sectionsemaphoreempty = M; // counts number of empty buffer slotssemaphorefull = 0; // counts number of full bufferslotssemaphoremutex1 = 1;semaphoreempty1 = N;semaphorefull1 = 0;
Thread A:while(TRUE) { // loop forevermake_new(object); // create a new widget to put in bufferwait(&empty); // decrement the empty semaphorewait(&mutex); // enter critical sectionput_item(object); // put widget in buffersignal(&mutex); // leave critical sectionsignal(&full); // increment the full semaphore}Thread B:while(TRUE) { // loop foreverwait(&full); // decrement the full semaphorewait(&mutex); // enter critical sectionremove_item(object);signal(&mutex); // leave critical sectionsignal(&empty); // increment the empty semaphorewait(&empty1); // decrement the empty semaphorewait(&mutex1); // enter critical sectionput_item(object); // put widget in buffersignal(&mutex1); // leave critical sectionsignal(&full1); // increment the full semaphore}while(TRUE) { // loop forevermake_new(object); // create a new widget to put in buffer}Thread C:while(TRUE) { // loop foreverwait(&full1); // decrement the full semaphorewait(&mutex1); // enter critical sectionremove_item(object); // take a widget from the buffersignal(&mutex1); // leave critical sectionsignal(&empty1); // increment the empty semaphoreconsume_item(widget); // consume the item}B)monitorThreeThreadsconditionfull, empty,full1,empty1;intcount,count1;ThreadAenter();{if(count == M) wait(full); // if buffer is full, blockput_item(widget); // put item in buffercount = count + 1; // increment count of full slotsif(count == 1) signal(empty); // if buffer was empty, wake Thread B}
desklib-logo
You’re reading a preview
card-image

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Unlock This Document