Kontera

Sunday, March 3, 2013

Semaphores - Multiprocessor operating systems using Java

/*PROGRAM 1 – Semaphores - Multiprocessor operating systems
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) B's and C's 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 A's 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*/

/*AUTHORS: Sarju S, Nidhin AS
Date: 3rd March 2013*/
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++;
        try{
            Thread.sleep(1000);
            notify();
        }
        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<10 br="" 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<10 br="" 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<10 br="" i="">        abc.printC();
        }
    }
}
//Main Class
class Multi_Process_OS {
    public static void main(String args[]) {
    ABC abc = new ABC();
        new Pa(abc);
        new Pb(abc);
        new Pc(abc);
    }
}

0 comments:

Post a Comment