java programming

java programming

Part 1.

Consider the three threads shown below. Using pseudo-code to show how it is possible to ensure that section a2 will run before section b2 and that section b2 will run

before section c2.

Thread A Thread B Thread C

section a1 section b1 section c1

section a2 section b2 section c2

Part 2

Process

Burst

Priority

P1

8

4

P2

6

1

P3

1

2

Using the above data

Find the Avg Wait time and Avg Turnaround time using

FCFS

SJF

non preemptive priority

Round Robin(Quantum of 1)

Part 3

use this code as a guide

/**

* Starter code for the Sandwich Shop Problem

*/

import java.util.concurrent.Semaphore;

public class SandwichShopStarter {

//DECLARE YOUR STATIC SEMAPHORES HERE

public static void main(String[] args) {

if (args.length != 1) {

printUsage();

}

int num = 0;

try {

num = Integer.parseInt(args[0]); // number of Customers

}

catch (NumberFormatException e) {

printUsage();

}

//INITIALIZE THE SEMAPHORES HERE THINK IF THEY START AT 0 OR 1

System.out.println(“Customer:ttttttttttt| Employee:”);

System.out.print(“TravelingtArrivedttOrderingtBrowsingtAt RegistertLeaving”);

System.out.println(“tt| WaitingtSandwich MakingttAt RegistertPayment Accepted”);

System.out .println(“—————————————————”

+ “———————————————+——–”

+ “——————————————————————-“);

Thread emp = new EmployeeThread();

emp.start();

Thread[] custs = new Thread[num];

for (int i = 0; i < num; i++) { custs[i] = new CustomerThread(i); custs[i].start(); } for (int i = 0; i < num; i++) { try { custs[i].join(); } catch (InterruptedException e) { } } System.exit(0); } private static void printUsage() { System.out.println("Usage: java SandwichShop "); System.out.println(" : Number of customers."); System.exit(-1); } public static void randomSleep(int max) { try { Thread.sleep((int) (Math.random() * max)); } catch (InterruptedException e) { } } } class CustomerThread extends Thread { private int id; public CustomerThread(int id) { this.id = id; } public void run() { //HERE you add the acquire and release of the semaphore travelToShop(); arriveAtShop(); placeOrder(); browseShop(); atRegister(); leaveShop(); } private void travelToShop() { System.out.println("Customer " + id + "ttttttttttt|"); SandwichShop.randomSleep(1000); } private void arriveAtShop() { System.out.println("ttCustomer " + id + "ttttttttt|"); } private void placeOrder() { System.out.println("ttttCustomer " + id + "ttttttt|"); } private void browseShop() { System.out.println("ttttttCustomer " + id + "ttttt|"); SandwichShop.randomSleep(1000); } private void atRegister() { System.out.println("ttttttttCustomer " + id + "ttt|"); } private void leaveShop() { System.out.println("ttttttttttCustomer " + id + "t|"); } } class EmployeeThread extends Thread { public void run() { while (true) { //HERE you add the acquire and release of the semaphore waitForCustomer(); makeSandwich(); atCashRegister(); paymentAccepted(); } } private void waitForCustomer() { System.out.println("tttttttttttt| Employee"); } private void makeSandwich() { System.out.println("tttttttttttt| ttEmployee"); SandwichShop.randomSleep(1000); } private void atCashRegister() { System.out.println("tttttttttttt| tttttEmployee"); } private void paymentAccepted() { System.out.println("tttttttttttt| tttttttEmployee"); } } This code simulates a Sandwich shop. You read in from the command line the number of Customer Threads. There is just one Employee Thread. Here are the rules: 1.The Employee waits on one customer at a time. 2. The Employee cannot make the sandwich until the customer orders it. 3. After ordering the customer browses the store. After making the sandwich the employee waits at register for Customer. 4. Customer pays and leaves and the Employee gets the next customer

find the cost of your paper