Three persons are involved in processing pizza orders. Person A reads the order
ID: 3729799 • Letter: T
Question
Three persons are involved in processing pizza orders. Person A reads the order from the computer and put the pizza into oven. Person B takes the pizza from oven to quality control desk (QCD). Finally, Person C takes the pizza from QCD and hands it over to the customer. (I know, this is oversimplification. Also, consider, it requires no time to prepare/cook/quality-control the pizza.) Assume all three persons operate on one pizza at a time, both oven and QCD have capacity of one pizza. Write a program (in a programming language you like) to coordinate the three persons using semaphores.
*include the readme file for running the program.
Explanation / Answer
Hi Student,
Below is the java implemetation of the same. As any programming language is mentioned. Please go through the code and in case you need any further assistance do let me know. Happy to Help. I have also attached a screenshot of the output.
package com.oracle.array;
import java.util.concurrent.*;
//A shared resource/class.
class Shared
{
static int count = 0;
}
class CheggMyThread extends Thread
{
Semaphore sem;
String threadName;
public CheggMyThread(Semaphore sem, String threadName)
{
super(threadName);
this.sem = sem;
this.threadName = threadName;
}
@Override
public void run() {
// run by thread A
if(this.getName().equals("A"))
{
System.out.println("Starting " + threadName);
try
{
// First, get a permit.
System.out.println(threadName + " is waiting for a permit.");
// acquiring the lock
sem.acquire();
System.out.println(threadName + " gets a permit.");
// Now, accessing the shared resource.
// other waiting threads will wait, until this
// thread release the lock
for(int i=0; i < 5; i++)
{
Shared.count++;
//System.out.println(threadName + ": " + Shared.count);
// Now, allowing a context switch -- if possible.
// for thread B to execute
Thread.sleep(10);
}
} catch (InterruptedException exc) {
System.out.println(exc);
}
// Release the permit.
System.out.println(threadName + " releases the permit.");
sem.release();
}
// run by thread B
else if(this.getName().equals("B"))
{
System.out.println("Starting " + threadName);
try
{
// First, get a permit.
System.out.println(threadName + " is waiting for a permit.");
// acquiring the lock
sem.acquire();
System.out.println(threadName + " gets a permit.");
// Now, accessing the shared resource.
// other waiting threads will wait, until this
// thread release the lock
for(int i=0; i < 5; i++)
{
Shared.count++;
//System.out.println(threadName + ": " + Shared.count);
// Now, allowing a context switch -- if possible.
// for thread B to execute
Thread.sleep(10);
}
} catch (InterruptedException exc) {
System.out.println(exc);
}
// Release the permit.
System.out.println(threadName + " releases the permit.");
sem.release();
}
else
{
System.out.println("Starting " + threadName);
try
{
// First, get a permit.
System.out.println(threadName + " is waiting for a permit.");
// acquiring the lock
sem.acquire();
System.out.println(threadName + " gets a permit.");
// Now, accessing the shared resource.
// other waiting threads will wait, until this
// thread release the lock
for(int i=0; i < 5; i++)
{
Shared.count++;
// System.out.println(threadName + ": " + Shared.count);
// Now, allowing a context switch -- if possible.
// for thread A to execute
Thread.sleep(10);
}
} catch (InterruptedException exc) {
System.out.println(exc);
}
// Release the permit.
System.out.println(threadName + " releases the permit.");
sem.release();
}
}
}
//Driver class
public class SemaphoreCheggDemo
{
public static void main(String args[]) throws InterruptedException
{
// creating a Semaphore object
// with number of permits 1
Semaphore sem = new Semaphore(1);
// creating two threads with name A and B
// Note that thread A will increment the count
// and thread B will decrement the count
CheggMyThread mt1 = new CheggMyThread(sem, "A");
CheggMyThread mt2 = new CheggMyThread(sem, "B");
CheggMyThread mt3 = new CheggMyThread(sem, "C");
// stating threads A and B and C
mt1.start();
mt2.start();
mt3.start();
// waiting for threads A and B and C
mt1.join();
mt2.join();
mt3.join();
// count will always remain 0 after
// all threads will complete their execution
}
}
Screen Shot Output :
Starting A
A is waiting for a permit.
Starting B
A gets a permit.
B is waiting for a permit.
Starting C
C is waiting for a permit.
A releases the permit.
B gets a permit.
B releases the permit.
C gets a permit.
C releases the permit.
If you like this answer please give a thumbs up! Happy Learning :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.