Friday, May 29, 2015

MULTITHREADING – MULTIPROCESSOR OPERATING SYSTEMS

AIM:

To write a java program for multithreading in multiprocessor operating systems.

ALGORITHM:

Step 1:              Start the program

Step 2:              There are two processes namely agent and smoker

Step 3:              Pick a random number from 1 to 3.

Step 4:              If random number is 1, then put tobacco on table, put paper on table.

Step 5:              Wake up smoker with match

Step 6:              If random number is 2, then put tobacco on table, put match on table

Step 7:              Wake up smoker with paper

Step 8:              If random number is 3, then put match on table, put paper on table

Step 9:              Wake up smoker with tobacco

Step 10:          Lock the agent process

Step 11:          For smoker process, pick up match, pick up paper

Step 12:          Lock the smoker process

Step 13:          Smoke

Step 14:          End of the program





Program:
import java.util.*;
 
public class Cigarette {

            public class Table {

                          public static final int Nothing = 0;
                          public static final int Tobacco = 1;
                          public static final int Paper = 2;
                          public static final int Matches = 4;

                          public static final int Tobacco_Paper = Tobacco + Paper;
                          public static final int Paper_Matches = Paper + Matches;
                          public static final int Matches_Tobacco = Matches + Tobacco;
                          public static final int Everything = Tobacco + Paper + Matches;
                         
                          private int contains;
                         
                          public Table () {
                                    contains = Nothing;
                          }

                          public synchronized void Put(int what) {
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": putting "+Contains(what));
                                    contains = contains | what;
                                    notifyAll();
                                    try {
                                      wait();
                                    } catch (InterruptedException e) {}
                          }

                          public synchronized void Get(int what) {
                                    while ((contains & what) != what) {
                                      try {
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": Getting " + Contains(what) + " - No!");
                                    wait();
                                      } catch (InterruptedException e) {}
                                    }
                                    System.out.println(Thread.currentThread().getName() +
                                                               ": Getting " + Contains(what) + " - Yes!");
                                    contains = contains ^ what;
                          }

                          public synchronized void DoneSmoking() {
                                    notifyAll();
                          }
                         
                          public String Contains(int what) {
                                    String s = "";
                                    if ((what & Tobacco) == Tobacco)
                                      s = s + "tobacco ";
                                    if ((what & Paper) == Paper)
                                      s = s + "paper ";
                                    if ((what & Matches) == Matches)
                                      s = s + "matches ";
                                    return s;
                          }
            }
            public class TableCS extends Table{
                        TableCS Table;
            }
            public class Agent extends Thread {

                          private Table table;
                          private Random rand;
                         
                          public Agent(Table tab, String name) {
                                    super (name);
                                    table = tab;
                                    rand = new Random();
                          }
                         
                          public void run() {
                                    while (true) {
                                      switch (Math.abs(rand.nextInt()) % 3) {
                                      case 0:
                                    table.Put(Table.Tobacco_Paper);
                                    break;
                                      case 1:
                                    table.Put(Table.Paper_Matches);
                                    break;
                                      case 2:
                                    table.Put(Table.Matches_Tobacco);
                                    break;
                          }
                        }
              }
            }
           
            public class Smoker extends Thread {

                          private Table table;
                          private Random rand;
                          private int needs;
                         
                          public Smoker(Table tab, String name, int what) {
                                    super (name);
                                    table = tab;
                                    rand = new Random();
                                    needs = Table.Everything ^ what;
                          }
                         
                          public void run() {
                                    while (true) {
                                      try {
                                    table.Get(needs);
                                    System.out.println(getName() + ": I got what I needed!");
                                   
                                    System.out.println(getName() + ": Rolling.");
                                    sleep(Math.abs(rand.nextInt()) % 1000);
                                   
                                    System.out.println(getName() + ": Smoking.");
                                    sleep(Math.abs(rand.nextInt()) % 1000);
                                   
                                    System.out.println(getName() + ": Done smoking.");
                                    table.DoneSmoking();
                                      }
                                      catch (InterruptedException e) {}
                        }
              }
            }
           

            public void mainRun(String[] args) {
                        Smoker smo1, smo2, smo3;
                        Agent agent;
                        Table table;
                       
                        table = new Table();
                        agent = new Agent(table, "Agent");
                        smo1 = new Smoker(table, "   Smoker 1", Table.Paper);
                        smo2 = new Smoker(table, "   Smoker 2", Table.Matches);
                        smo3 = new Smoker(table, "   Smoker 3", Table.Tobacco);
                        agent.start();
                        smo1.start();
                        smo2.start();
                        smo3.start();
            }

            public static void main(String[] arg)
            {
               Cigarette  a = new Cigarette();
               a.mainRun(arg);                  
            }


}
OUTPUT:

C:\Documents and Settings\me22>javac cigarate.java

C:\Documents and Settings\me22 >java cigarate

Agent:puttingtobaccopaper
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -No!
Smoker 3: I Got What I Need!!
Smoker 3: Rolling
Smoker 3: Smoking!!
Smoker 3: Done Smoking!!
Smoker 3: I Got What I Need!!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 3: Rolling
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Agent:puttingtobaccopaper
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 3: Smoking!!
Smoker 3: Done Smoking!!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1: I Got What I Need!!
Smoker 2:Getttingpapermatches -Yes!
Smoker 1: Rolling
Smoker 2:Getttingpapermatches -No!
Agent:puttingtobaccopaper
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1: Smoking!!
Smoker 1: Done Smoking!!
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3: I Got What I Need!!

Agent:puttingtobaccomatches
Smoker 3: Rolling
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1:Getttingtobaccomatches -No!
Smoker 3: Smoking!!
Smoker 3: Done Smoking!!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -Yes!
Smoker 2: I Got What I Need!!
Agent:puttingtobaccopaper
Smoker 2: Rolling
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1:Getttingtobaccomatches -No!
Smoker 2: Smoking!!
Smoker 2: Done Smoking!!
Smoker 2:Getttingpapermatches -No!
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1: I Got What I Need!!
Agent:puttingpapermatches
Smoker 1: Rolling
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3:Getttingtobaccopaper -No!
Smoker 1: Smoking!!
Smoker 1: Done Smoking!!
Smoker 1:Getttingtobaccomatches -No!
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 3:Getttingtobaccopaper -Yes!
Smoker 3: I Got What I Need!!
Agent:puttingtobaccomatches
Smoker 3: Rolling
Smoker 2:Getttingpapermatches -Yes!
Smoker 2:Getttingpapermatches -No!
Smoker 1:Getttingtobaccomatches -Yes!
Smoker 1:Getttingtobaccomatches -No!
Smoker 3: Smoking!!




No comments:

Post a Comment