Sie sind auf Seite 1von 8

Ex.

No: 2 MULTIPROCESSOR OPERATING SYSTEMS SEMAPHORES

Problem Statement :

Assume there are three processes: Pa, Pb and Pc. Only Pa can output the letter A, Pb B and Pc C. Utilizing only semaphores (and no other variables) the processes are synchronized so that the output satisfies the following conditions:

a) A B must be output before any C's can be output. b) Bs and Cs must alternate in the output string, that is, after the first B is output, another B cannot be output until a C is output. Similarly, once a C is output, another C cannot be output until a B is output. c) The total number of B's and C's which have been output at any given point in the output string cannot exceed the number of As which have been output up to that point. Examples AACB invalid, violates (a) ABACAC invalid, violates (b) AABCABC invalid, violates (c) AABCAAABC valid AAAABCBC -- valid AB -- valid

AIM:
To write a Java program to implement a multiprocessor operating system using semaphores that satify the above problem statement.

ALGORITHM:
Assume that there are 3 processes Pa, Pb and Pc. Only Pa can output the letter A, Pb B and Pc C. Utilizing only semaphores, the processes are synchronized so that the output satisfies some condition

1. Binary Semaphores B and C are initialized to 0 and 1 respectively and the counting semaphore sum is assigned to zero. 2. The start time is assigned to the current time. 3. For the Binary Semaphore, the synchronized P() and V() operations are to be implemented by testing and setting a Boolean variable locked. 4. For the Counting Semaphore, the synchronized P() and V() operations are to be implemented by incrementing and decrementing a counter as and when required. 5. Three threads Pa, Pb and Pc are created and started where each thread waits for a random amount of time to display the output as required. 6. The execution gets terminated after printing a valid sequence of A, B and C.

SOURCE CODE:
import java.io.*; class ABC { int aCount=0;//Variable used to ensure the third rule boolean isPrintBC= false;//Used to ensure the second rule //Function used to print A synchronized void printA() { System.out.print("A"); aCount++; notifyAll(); try{

Thread.sleep(500); notifyAll(); } catch(Exception e){} } //Function used to print B synchronized void printB() { if(isPrintBC)//True try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); } //False

if(aCount>0){ System.out.print("B"); try{ Thread.sleep(1000); } catch(Exception e){} aCount--; isPrintBC=true; notify();

} //Rule 3 voilated else{ isPrintBC=true; notify(); } } //Function used to print C synchronized void printC() { if(!isPrintBC)//False try { wait(); } catch(InterruptedException e) { System.out.println("InterruptedException caught"); }

//True if(aCount>0){ System.out.print("C"); try{ Thread.sleep(1000); } catch(Exception e){}

aCount--; isPrintBC=false; notify();

} //Rule 3 voilated else{ isPrintBC=false; notify(); } } } //Process Pa outputs A class Pa implements Runnable { ABC abc; Pa(ABC abc) { this.abc = abc; new Thread(this, "Pa").start();

} public void run() { for(int i=0;i<20;i++) { abc.printA(); } } } //Process Pb outputs B class Pb implements Runnable { ABC abc; Pb(ABC abc) { this.abc = abc; new Thread(this, "Pb").start(); } public void run() { for(int i=0;i<20;i++){ abc.printB();

} } } //Process Pc outputs C class Pc implements Runnable { ABC abc; Pc(ABC abc) { this.abc = abc;

new Thread(this, "Pc").start(); } public void run() { for(int i=0;i<20;i++) { abc.printC(); } } } class check{ } //Main Class class MultiOS { public static void main(String args[]) { String input=""; ABC abc = new ABC(); new Pa(abc); new Pb(abc); new Pc(abc); } }

OUTPUT:
[csme1012@csplus ex]$ javac MultiOS.java [csme1012@csplus ex]$ java MultiOS AAAAAAAAAAAAAAAAABAAACBCBCBCBCBCBCBCBCBC

RESULT:
Thus the Java program for implementing multiprocessor operating system using semaphore is executed successfully.

Das könnte Ihnen auch gefallen