Question

In: Computer Science

The design should consist of two things: (1) a list of every semaphore, its purpose, and...

The design should consist of two things:
(1) a list of every semaphore, its purpose, and its initial value, and
(2) pseudocode for each function. The pseudocode should be similar to the pseudocode shown in the textbook for the barbershop problem.
Every wait and signal call must be included in the pseudocode.    

Must use Java Threads and Java Semaphores (java.util.concurrent.Semaphore).
You may not use the “synchronized” keyword in Java for mutual exclusion.
You may not use Java data structures that have built-in mutual exclusion.

Please provide corresponding PSEUDOCODE along with JAVA CODE.


Hotel Simulation
A hotel is simulated by using threads and semaphores to model customer and employee behavior.
This project is similar to the “barbershop” example in the textbook. The following rules apply:


The hotel to be simulated has two employees at the front desk to register guests and two bellhops to handle guests’ bags.
A guest will first visit the front desk to get a room number. The front desk employee will find an available room and assign it to the guest.
If the guest has less than 3 bags, the guest proceeds directly to the room. Otherwise, the guest visits the bellhop to drop off the bags.
The guest will later meet the bellhop in the room to get the bags, at which time a tip is given.

Threads:

Guest:
1)   25 guests visit the hotel (1 thread per guest created at start of simulation).
2)   Each guest has a random number of bags (0-5).
3)   A guest must check in to the hotel at the front desk.
4)   Upon check in, a guest gets a room number from the front desk employee.
5)   A guest with more than 2 bags requires a bellhop.
6)   The guest enters the assigned room.
7)   Receives bags from bellhop and gives tip (if more than 2 bags).
8)   Retires for the evening.

Front Desk:
1)   Two employees at the front desk (1 thread each).
2)   Checks in a guest, finds available room, and gives room number to guest.

Bellhop:
1)   Two bellhops (1 thread each).
2)   Gets bags from guest.
3)   The same bellhop that took the bags delivers the bags to the guest after the guest is in the room.
4)   Accepts tip from guest.

Other rules:
1)   All mutual exclusion and coordination must be achieved with semaphores.
2)   A thread may not use sleeping as a means of coordination.
3)   Busy waiting (polling) is not allowed.
4)   Mutual exclusion should be kept to a minimum to allow the most concurrency.
5)   Each thread should print when it is created, and main should print when it joins the customer threads.
6)   Each thread should only print its own activities. The guest threads print guest actions and the employee threads print their own actions.
7)   Your output must include the same information, same wording, and the same set of steps as the sample output. Of course, each run can be different depending on the order of thread execution and the random assignments made.


Output

1)   Each step of each task of each thread should be printed to the screen with identifying numbers so it is clear which threads are involved.
2)   Thread output sample for 3 guests. The wording in your output should exactly match the sample.

Simulation starts
Front desk employee 0 created
Front desk employee 1 created
Bellhop 0 created
Bellhop 1 created
Guest 0 created
Guest 1 created
Guest 2 created
Guest 0 enters hotel with 1 bag
Guest 1 enters hotel with 4 bags
Guest 2 enters hotel with 3 bags
Front desk employee 0 registers guest 0 and assigns room 1
Front desk employee 1 registers guest 1 and assigns room 2
Guest 0 receives room key for room 1 from front desk employee 0
Guest 1 receives room key for room 2 from front desk employee 1
Front desk employee 0 registers guest 2 and assigns room 3
Guest 0 enters room 1
Guest 2 receives room key for room 3 from front desk employee 0
Guest 1 requests help with bags
Guest 0 retires for the evening
Guest 0 joined
Guest 2 requests help with bags
Bellhop 1 receives bags from guest 2
Bellhop 0 receives bags from guest 1
Guest 1 enters room 2
Guest 2 enters room 3
Bellhop 0 delivers bags to guest 1
Bellhop 1 delivers bags to guest 2
Guest 1 receives bags from bellhop 0 and gives tip
Guest 2 receives bags from bellhop 1 and gives tip
Guest 2 retires for the evening
Guest 1 retires for the evening
Guest 1 joined
Guest 2 joined
Simulation ends

Solutions

Expert Solution

Answer:

Code:

/Include libraries

import java.util.concurrent.Semaphore;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import java.util.*;

//Define a class

public class Project2

{

    //Define a class

     class shrdSyncrnztnDta

     {

         //Declare variable

          public Semaphore        lPtnts;  

          //Declare variable     

          public Semaphore        lRcpnst;

          //Declare variable     

          public Semaphore        lNrses;  

          //Declare variable     

          public int                   lMxPtnts;    

         

          //Declare variable

          public int                   lNmPtnts = 0;

         

          //Declare variable

          public Lock                  lMtx;             

         

          //Declare variable

          public int                   lPtId;

         

          //Declare variable

          public Queue<Integer>   lPQue;

         

          //Declare variable

          public Lock                  lMtx2;            

         

          //Declare variable

          public int                   lPRgstd;

         

          //Declare variable

          public int                   lNmNrses;

         

          //Declare variable

          public int                   lNmDctrs;

          //Define constructor

          shrdSyncrnztnDta(int lNm)

          {

              //Assign value

              lPtnts        =    new Semaphore(0);

             

              //Assign value

              lRcpnst   =    new Semaphore(0);

             

              //Assign value

              lNrses             =    new Semaphore(0);

             

              //Assign value

              lMtx          =    new ReentrantLock();

             

              //Assign value

              lMtx2              =    new ReentrantLock();

             

              //Assign value

              lMxPtnts      =    lNm;

             

              //Assign value

              lPQue              =    new LinkedList<Integer>();

             

               //Assign value

              lPRgstd       =    0;

          }

     }

     //Define class

     class Receptionist extends Thread

     {

          //Create instance

          boolean lBsy;

         

          //Create instance

          shrdSyncrnztnDta lSych;

          //Define a constructor

          public Receptionist( shrdSyncrnztnDta s )

          {

              //Assign value

              lBsy = false;

             

              //Assign value

              lSych = s;

          }

          //Define method

          public void run()

          {

              //Loop

              for (int li = 0; li<lSych.lNmPtnts; li++)

              {

                 //Try

                   try

                   {

                       //Call method

                        lSych.lPtnts.acquire();      

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}

                   //Call method

                   lSych.lMtx.lock();                   

                  

                   //Increment

                   lSych.lMxPtnts++;                

                  

                   //Call method

                   lSych.lRcpnst.release();     

                  

                   //Call method

                   lSych.lMtx.unlock();

                   //Try

                   try

                   {

                       //Sleep

                        Thread.sleep(20);

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}         

              }

          }

     }

     //Defien a class

     class Patient extends Thread

     {

         //Declare variable

          int lId;

         

          //Declare variable

          boolean isWaiting;

         

          //Declare variable

          shrdSyncrnztnDta lSych;

          //Define constructor

          public Patient(int lNm, shrdSyncrnztnDta s)

          {

              //Assign value

              lId = lNm;

             

              //Assign value

              isWaiting = false;

             

              //Assign value

              lSych = s;

          }

          //Define method

          public void run()

          {

              //Display message

System.out.println("Patient " + lId + " enters waiting room, waits for lRcpnst.");

             

              //Call method

              lSych.lMtx.lock();                        

              //If condition satisfies

              if (lSych.lMxPtnts > 0)

              {

                   //Decrement

                   --lSych.lMxPtnts;                

                   //Call method

                   lSych.lPtnts.release();              

                   //Call metjod

                   lSych.lMtx.unlock();

                   //Try

                   try

                   {

                       //Call method

                        lSych.lRcpnst.acquire();    

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}

                  

                   //Display message

System.out.println("Receptionist registers patient " + lId);

                  

                   //Try

                   try

                   {

                       //Sleep

                        Thread.sleep(20);

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}

                   //Display message

                   System.out.println("Patient " + lId + "                                        leaves receptionist and sits in waiting room");

                  

                   //Increment

                   lSych.lPRgstd ++;

                  

                   //Call method

                   lSych.lPQue.add(lId);

                  

                   //Assign value

                   isWaiting = true;

              }

             

              //Stop

              Thread.currentThread().stop();

          }

     }

     //Define class

     class Nurse extends Thread

     {

         //Declare variable

          int lId;

         

          //Declare variable

          boolean isBusy;

         

          //Declare variable

          shrdSyncrnztnDta lSych;

          //Define constructor

          public Nurse(int lNm, shrdSyncrnztnDta s)

          {

              //Assign value

              lId = lNm;

             

              //Assign value

              isBusy = false;

             

              //Assign value

              lSych = s;

          }

          //Define method

          public void run()

          {

              //Lock

              lSych.lMtx2.lock();                       

              //If condition satisfies

              if (!lSych.lPQue.isEmpty())

              {

                  //Assign value

                   Integer lPtntId = lSych.lPQue.remove();

                  

                   //Decrement

                   --lSych.lPRgstd;

                   //Call method

                   lSych.lMtx2.unlock();                

                  

                   //Try

                   try

                   {

                       //Sleep

                        Thread.sleep(20);

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}

                   //Display message

System.out.println("Nurse " + lId + " takes patient " + lPtntId + " to doctor's office");

                  

                   //Assign value

                   isBusy = true;

              }

             

              //Call method

              Thread.currentThread().stop();

          }

     }

     //Define method

     public void simulate(int lNmP, int lNmN, int lNmD)

     {

          //Create instance

          shrdSyncrnztnDta lSych = new shrdSyncrnztnDta(15);

         

          //Assign value

          lSych.lNmPtnts = lNmP;

         

          //Assign value

          lSych.lNmNrses = lNmN;

         

          //Assign value

          lSych.lNmDctrs = lNmD;

          //Create instance

          Thread lRcpThrd;

         

          //Assign value

          Thread[] lptThrd = new Thread[lSych.lNmPtnts];

         

          //Assign value

          Thread[] lnrThrd = new Thread[lSych.lNmNrses];

          //Declare variable

          int li;

          //Assign value

          lRcpThrd = new Receptionist(lSych);

         

          //Start

          lRcpThrd.start();

          //Loop

          for (li = 0; li < lSych.lNmPtnts; li++)

          {

             //Try

              try

              {

                  //Sleep

                   Thread.sleep(10);

              }

             

              //Catch

              catch (InterruptedException lE){}

             

              //Create instance

              lptThrd[li] = new Patient(li, lSych);

             

              //Start

              lptThrd[li].start();

    }

    

         //Assign value

          int lj = lSych.lNmPtnts;

          //Loop

          while(lj>0)

          {

              //Loop

         for (li = 0; li < lSych.lNmNrses; li++)

              {

                  //Try

                   try

                    {

                       //Sleep

                        Thread.sleep(10);

                   }

                  

                   //Catch

                   catch (InterruptedException lE){}

                  

                   //Assign value

                   lnrThrd[li] = new Nurse(li, lSych);

                  

                   //Start

                   lnrThrd[li].start();

              }

             

              //Assign value

              lj -= 5;

          }

          //Try

          try

          {

              //Join

              lRcpThrd.join();

          }

         

          //Catch

          catch (InterruptedException lE){}

          //Loop

    for( li = 0; li < lSych.lNmPtnts; ++li )

          {

              //Try

              try

              {

                   //Join

                   lptThrd[li].join();

              }

             

              //Catch

              catch (InterruptedException lE){}

          }

     }

     //Define main

     public static void main(String args[])

     {

         //Declare variables

          int lNmP = 5, lNmN = 5, lNmD = 5;

          //Create instance

          Project2 lClnc;

         

          //Create instance

          lClnc = new Project2();

         

          //Call method

          lClnc.simulate(lNmP, lNmN, lNmD);

     }

}

Note: If you have any doubts or queries please comment I will get back to you.


Related Solutions

1.What is a code of practice and what is its purpose in structural design? 2.List the...
1.What is a code of practice and what is its purpose in structural design? 2.List the principal sources of uncertainty in structural design and discuss how these uncertainties are rationally allowed for in design. 3.The characteristic strengths and design strengths are related via the partial safety factor for materials. The partial safety factor for concrete is higher than for steel reinforcement. Discuss why this should be so 4. Describe in general terms the ways in which a beam and column...
1- List the things the system should be capable of doing. List the common facilities and...
1- List the things the system should be capable of doing. List the common facilities and services the framework should have and differentiating features of reservation systems separately. Also, list the possible constraints required for this framework. And I want to create a framework for a dental appointment. 2 - If you are asked to develop a dental appointment reservation system. What kind of slots and hooks do you need to develop? Give some descriptions of the specifications in an...
Make two lists. List A should identify the things that represent the core cultures of organizations....
Make two lists. List A should identify the things that represent the core cultures of organizations. List B should id identify the tings that represent the observable cultures of the organization. For each item on the two lists , identify one or more indicators that you might use to describe this aspect of the culture for an actual organization.
The project will study the coordination of multiple threads using semaphores. The design should consist of...
The project will study the coordination of multiple threads using semaphores. The design should consist of two things: (1) a list of every semaphore, its purpose, and its initial value, and (2) pseudocode for each function. Code Your code should be nicely formatted with plenty of comments. The code should be easy to read, properly indented, employ good naming standards, good structure, and should correctly implement the design. Your code should match your pseudocode. Project Language/Platform This project must target...
The purpose of universal usability is to design products or services that are usable by almost every citizen.
To help students understand the universal usability concept.Topic of DiscussionThe purpose of universal usability is to design products or services that are usable by almost every citizen. Discuss universal usability in the context of cultural and international diversity. Give some examples of such products that you think provide better universal usability in terms of cultural and international diversity.
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of...
Your hardcopy submission will consist of these two things: Source code (your java file). Screenshot of Eclipse showing output of program. (JPG or PNG format only) Write a program to play the game of Stud Black Jack. This is like regular Black Jack except each player gets 2 cards only and cannot draw additional cards. Create an array of 52 integers. Initialize the array with the values 0-51 (each value representing a card in a deck of cards) in your...
A researcher notes that two things are correlated. This should result in a ... 1.Frequency claim...
A researcher notes that two things are correlated. This should result in a ... 1.Frequency claim 2. Association claim 3. Causal claim 4. Something else
1.List three important things that kidneys do to REGULATE your blood chemistry 2.List two types of...
1.List three important things that kidneys do to REGULATE your blood chemistry 2.List two types of molecules or cells that are retained by the glomerulus. Explain why these items are NOT filtered out of the blood. 3.Describe how urea is used in the loop of the nephron to create the countercurrent multiplier system. What other benefit does this have for the body
List three things for following types of calculations: (1) conservation equation should be steady-state or unsteady-state,...
List three things for following types of calculations: (1) conservation equation should be steady-state or unsteady-state, (2) conservation equation should be macroscopic or microscopic (differential), (3) what kind of conservation equation (e.g., mass balance) or equations should be used, Calculating the force a curved water pipe exerts on its bracket at the highest flow rate. Calculating the power required to pump benzene into an elevated tank. Calculating the time required for a hot reactor to cool after it is shut...
1. Explain why adjusting entries are needed, and identify two key things that should be kept...
1. Explain why adjusting entries are needed, and identify two key things that should be kept in mind when preparing them. 2. Explain why closing entries are made. What are the two objectives that are accomplished by making closing entries? 3.In general terms, explain the purpose of the statement of income and the statement of financial position. Outline the type of information that each statement presents, and explain the basic difference between the types of items that appear on the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT