Question

In: Computer Science

Implement synchronous send and receive of one word messages (also known as Ada-style rendezvous), using condition...

Implement synchronous send and receive of one word messages (also known as Ada-style rendezvous), using condition variables (don't use semaphores!). Implement the Communicator class with operations, void speak(int word) and int listen().

speak() atomically waits until listen() is called on the same Communicator object, and then transfers the word over to listen(). Once the transfer is made, both can return. Similarly, listen() waits until speak() is called, at which point the transfer is made, and both can return (listen() returns the word). Your solution should work even if there are multiple speakers and listeners for the same Communicator (note: this is equivalent to a zero-length bounded buffer; since the buffer has no room, the producer and consumer must interact directly, requiring that they wait for one another). Each communicator should only use exactly one lock. If you're using more than one lock, you're making things too complicated.

Solutions

Expert Solution

package nachos.threads;
import nachos.machine.*;

/**
* A communicator allows threads to synchronously exchange 32-bit
* messages. Multiple threads can be waiting to speak,
* and multiple threads can be waiting to listen. But there should never
* be a time when both a speaker and a listener are waiting, because the two
* threads can be paired off at this point.
*/

public class Communicator {

/**
* Allocate a new communicator.
*/

    public Communicator() {
      mutex = new Lock();
    listenReady = new Condition(mutex);
    speakReady = new Condition(mutex);
    }

/**
* Wait for a thread to listen through this communicator, and then transfer
* <i>word</i> to the listener.
*
* <p>
* Does not return until this thread is paired up with a listening thread.
* Exactly one listener should receive <i>word</i>.
*
* @param
 word the integer to transfer.
*/

    public void speak(int word) {

        mutex.acquire();
  speaker++;
      while(listener==0){
            listenReady.sleep();
        }
    listener--;

 messenger = word;
    speakReady.wake();

    mutex.release();
    }

/**
* Wait for a thread to speak through this communicator, and then return
* the word that thread passed to speak().
*
* @return the integer transferred.
*/

    public int listen() {

        mutex.acquire();
  listener++;
  listenReady.wake();
  while(speaker==0) {
            speakReady.sleep();
        }
  speaker--;
  mutex.release();
  return messenger;
    }
    private Condition speakReady;

 private Condition listenReady;
 private int speaker=0, listener=0;
 private int messenger;
 private Lock mutex;
}

package nachos.threads;
import nachos.machine.*;

/**
* A communicator allows threads to synchronously exchange 32-bit
* messages. Multiple threads can be waiting to speak,
* and multiple threads can be waiting to listen. But there should never
* be a time when both a speaker and a listener are waiting, because the two
* threads can be paired off at this point.
*/

public class Communicator {

/**
* Allocate a new communicator.
*/

    public Communicator() {
      mutex = new Lock();
    listenReady = new Condition(mutex);
    speakReady = new Condition(mutex);
    }

/**
* Wait for a thread to listen through this communicator, and then transfer
* <i>word</i> to the listener.
*
* <p>
* Does not return until this thread is paired up with a listening thread.
* Exactly one listener should receive <i>word</i>.
*
* @param
 word the integer to transfer.
*/

    public void speak(int word) {

        mutex.acquire();
  speaker++;
      while(listener==0){
            listenReady.sleep();
        }
    listener--;

 messenger = word;
    speakReady.wake();

    mutex.release();
    }

/**
* Wait for a thread to speak through this communicator, and then return
* the word that thread passed to speak().
*
* @return the integer transferred.
*/

    public int listen() {

        mutex.acquire();
  listener++;
  listenReady.wake();
  while(speaker==0) {
            speakReady.sleep();
        }
  speaker--;
  mutex.release();
  return messenger;
    }
    private Condition speakReady;

 private Condition listenReady;
 private int speaker=0, listener=0;
 private int messenger;
 private Lock mutex;
}

Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......


Related Solutions

You can estimate the total number of text messages you send and receive in a year...
You can estimate the total number of text messages you send and receive in a year as follows: Count up the number of text messages sent and received during the one-hour period of a typical day when you do the most texting; then ... Multiply that count by 13, and multiply that result by 365, the number of days in a year. 13 is a guesstimate or SWAG for the ratio of the total number of messages sent and received...
You and a friend are using the C(7,4) Hamming code to send some 4-bit messages to...
You and a friend are using the C(7,4) Hamming code to send some 4-bit messages to each other. (a) You encode the message 1010 and send the encoded 7-bit sequence to your friend, who receives 1011011. How many errors were introduced during transmission? (b) You subsequently receive the encoded sequence 0111011 from your friend. Assuming at most one error, what is the 4-bit message that your friend sent?
Osteogenesis Imperfecta is also known as Brittle Bone Disease. The condition results from a lack of...
Osteogenesis Imperfecta is also known as Brittle Bone Disease. The condition results from a lack of collagen. What role does collagen play in the homeostatic condition of normal bone tissue? What other systems in the body might be impacted by this lack of collagen
We can implement a basic web search engine by building an inverted index (also known as...
We can implement a basic web search engine by building an inverted index (also known as an index) that maps each term (word) to the set of web pages in which it appears. A search query, which is a sequence of one or more keywords, can be answered by returning the web pages that contain all of the terms in a given query. Implement a JAVA SearchEngine class that represents an inverted index. 1) SearchEngine() Constructs an empty SearchEngine instance....
A property owner decides to restore a property to satisfactory condition without changing the floor plan, form, or style of the structure. This is known as what?
A property owner decides to restore a property to satisfactory condition without changing the floor plan, form, or style of the structure. This is known as what?   1) rehabilitation   2) remodeling   3) conversion   4) adaptive reuse   What term describes a tenant's transfer of a portion of his or her rights to another?   1) a lease   2) a sublease   3) an assignment   4) a contract
COP2271 MATLAB HW9 Homework: Modified Vigenere Cipher Implement a decryption cipher to decode messages using a...
COP2271 MATLAB HW9 Homework: Modified Vigenere Cipher Implement a decryption cipher to decode messages using a secret key. You are required to submit the solution and screenshots for this question. Key programming concepts: if statements, loops, strings Approximate lines of code: 27 (does not include comments or white space) Commands you can’t use: None... Program Inputs • Enter message to decrypt: • Enter secret key: – The user will always enter text for all prompts, no error checking needed. The...
One page (on Word, about 400 words) summary of cardiac tissue engineering. Please don't send in...
One page (on Word, about 400 words) summary of cardiac tissue engineering. Please don't send in handwritten answers since they are hard to read.
Task Description:  Create a referral letter for the following patient using Microsoft word & Full Block Style...
Task Description:  Create a referral letter for the following patient using Microsoft word & Full Block Style . Patient: Lisa Duck Dentist: Dr. Harry DDS, M.S  Address ... Radiograph Findings: Impacted third molars, mild bone loss around max and mandibular molars. Fractured amalgam on the mandibular left first molar Oral Examination Findings: Confirmed mild periodontitis, fractured amalgam, Class III Mobility on 37 electrical shock like pain that radiates to his lower lip when tooth clenched.
Describe how Warby Parker’s donations positively impact those who receive the donations. Provide 300 word also...
Describe how Warby Parker’s donations positively impact those who receive the donations. Provide 300 word also watch this video to answe this question: http://www.viddler.com/embed/9af02da%20/?f=1&autoplay=0&player=full&disablebranding=0
Describe the history of the New Silk Road (also known as One Belt One Road (OBOR),...
Describe the history of the New Silk Road (also known as One Belt One Road (OBOR), Belt Road Initiative (BRI)) and Chinese economic and political motivations underlying the various initiatives Analyze an example of an OBOR project in Africa, in Latin America and in Asia. How do you synthesize your learning of OBOR with your learning on globalization?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT