Thursday, May 28, 2015

SEMAPHORES - MULTIPROCESSOR OPERATING SYSTEMS

AIM:
            To write a Java program for semaphores in multiprocessor operating systems


ALGORITHM



Step 1:              Start the program

Step 2:              Assume there are three processes namely Pa, Pb and Pc.

Step 3:              Only Pa can output the letter A, Pb, B and Pc, C.

Step 4:              Utilizing only semaphores(and no other variables) the processes are synchronized

Step 5:              So that the output satisfies the following conditions

Step 6:              a) A B must be output before any C’s can be output.

Step 7:              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.

Step 8:              Similarly, once a C is output, another C cannot be output until a B is output

Step 9:              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

Step 10:          For more than two processes busy wait until they allowed to enter the critical
section

Step 11:          End of program.



  
Program:

import java.io.*;
class printing
{
    boolean Bsem=true,Csem=false;
    int sum=0;
    printing()
    {
    }
    synchronized void printa(String str)
    {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum++;
    }


     synchronized void printb(String str)
    {
       if(sum>1&&Bsem==true)
       {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum--;
        Bsem=false;
        Csem=true;
        notifyAll();
       }
       else
       {
           try
           {
           wait();
           }
           catch(Exception e)
           {
           }
       }
    }


     synchronized void printc(String str)
    {
       if(sum>=1&&Csem==true)
       {
        System.out.println(str);
        try
        {
        Thread.sleep(1000);
        }
        catch(Exception e)
        {

        }
        sum--;
        Bsem=true;
        Csem=false;
        notifyAll();
       }
       else
       {
           try
           {
           wait();
           }
           catch(Exception e)
           {

           }
       }

    }

}


class processA implements Runnable
{
   printing p1;
    processA(printing p)
    {
         p1=p;

    }
    public void run()
    {

        for(int i=0;i<10;i++)
             p1.printa("A");
    }
}

class processB implements Runnable
{
    printing p2;
    processB(printing p)
    {

        p2=p;

    }
    public void run()
    {
      for(int i=0;i<10;i++)
       p2.printb("B");
    }
}


class processC implements Runnable
{
    printing p3;

    processC(printing p)
    {

          p3=p;

    }
    public void run()
    {
      for(int i=0;i<10;i++)
       p3.printc("C");
    }
}




public class printabc {

    public static void main(String[] args)
{
        // TODO code application logic here
        printing p=new printing();

        processA pa=new processA(p);
        Thread t1=new Thread(pa);
        processB pb=new processB(p);
        Thread t2=new Thread(pb);
        processC pc=new processC(p);
        Thread t3=new Thread(pc);
        t1.start();
        t2.start();
        t3.start();

    } 








Output:

U:\OS_Lab\EX1>javac printabc.java

D:\java>java printabc
A
A
B
C
A
A
A
A
A
B
C
A
B
C
A
A
B
C
B
C


No comments:

Post a Comment