In: Computer Science
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
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.