Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A chief minister office is designed in such a way that there is a waiting room a

ID: 3636300 • Letter: A

Question

A chief minister office is designed in such a way that there is a waiting room adjoining the minister’s office. Waiting room has two doors D1 and D2, D1 opens inside the minister’s room and D2 is used for entrance from out side. Waiting room has N chairs for some contractors who have to see chief minister. If the minister is busy, the door D1 is closed and arriving contractor sits in one of the available chairs. If a contractor enters the waiting room and all chairs are occupied, the contractor leaves the office without meeting. If there are no contractors in the waiting room, the minister goes to rest (sleep) in the chair with the door D1 open. If minister is asleep, the contractor makes the minister awake by ringing the bell and gets meeting with him.


While keeping in mind the given scenario, you have to write the pseudo code OR code fragment using semaphores to define synchronization scheme for the contractor and chief minister.

Explanation / Answer

example of similar kind import java.util.Stack; import java.util.concurrent.atomic.AtomicInteger; /** * 1 producer and 3 consumers producing/consuming 10 items * * @author pt * */ public class ProducerConsumer { Stack items = new Stack(); final static int NO_ITEMS = 10; public static void main(String args[]) { ProducerConsumer pc = new ProducerConsumer(); Thread t1 = new Thread(pc.new Producer()); Consumer consumer = pc.new Consumer(); Thread t2 = new Thread(consumer); Thread t3 = new Thread(consumer); Thread t4 = new Thread(consumer); t1.start(); try { Thread.sleep(100); } catch (InterruptedException e1) { e1.printStackTrace(); } t2.start(); t3.start(); t4.start(); try { t2.join(); t3.join(); t4.join(); } catch (InterruptedException e) { e.printStackTrace(); } } class Producer implements Runnable { public void produce(int i) { System.out.println("Producing " + i); items.push(new Integer(i)); } @Override public void run() { int i = 0; // produce 10 items while (i++ = NO_ITEMS; } @Override public void run() { while (!theEnd()) { synchronized (items) { while (items.isEmpty() && (!theEnd())) { try { items.wait(10); } catch (InterruptedException e) { Thread.interrupted(); } } consume(); } } } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote