Ex.no:1MULTIPROCESSOR OPERATING SYSTEM

Date:

AIM

To write a java program for multiprocessor operating system using semaphores.

ALGORITHM

Step 1: Start the program.

Step 2: Declare the variables for more than two processes.

Step 3: Declare the entry section for P1 and entry section for P2.

Step 4: Define Q1:=TRUE and Q2:=TRUE.

Step 5: Assign TURN:=1 and TURN:=2.

Step 6: Wait while Q2 of TURN:=1 and wait while Q1 of TURN:=2.

Step 7: The processes are synchronized, so that output displayed in the order ABC.

Step 8: Exit from section for P1 and exit section for P2 while Q1!=FALSE and Q2:=FALSE.

Step 9: B’s and C’s must be alternate in the output String i.e) after the first B is displayed, another B cannot be the displayed until a C is displayed.

Step 10: Similarly, once C is displayed, another C cannot be displayed until a B is displayed.

Step 11: The total number of B’s and C’s which have been displayed at any given point in the output string cannot exceed.

Step 12: Utilize only semaphores, the processes are synchronized so that the output is satisfied.

Step 13: Execute the program.

.

PROGRAM

//Program name ABCs.java

import java.io.Serializable;

import java.util.Date;

import java.util.Random;

class BinarySemaphore

{

private boolean locked = false;

BinarySemaphore() {} // constructors

BinarySemaphore(boolean initial)

{

locked = initial;

}

BinarySemaphore(int initial)

{

locked = (initial == 0);

}

public synchronized void P()

{

while (locked)

{

try

{

wait();

}

catch (InterruptedException e)

{ }

}

locked = true;

}

public synchronized void V()

{

if (locked) notify();

locked = false;

}

}

class CountingSemaphore

{

private int value = 0;

private int waitCount = 0;

private int notifyCount = 0;

public CountingSemaphore(int initial)

{

if (initial > 0) value = initial;

}

public synchronized void P()

{

if (value <= waitCount)

{

waitCount++;

try

{

do

{

wait();

} while (notifyCount == 0);

}

catch(InterruptedException e)

{

notify();

}

finally

{

waitCount--;

}

notifyCount--;

}

value--;

}

public synchronized void V()

{

value++;

if (waitCount > notifyCount)

{

notifyCount++;

notify();

}

}

}

class Pa extends ABCs implements Runnable

{ // extend ABCs to

// access semaphore sum

public void run ()

{

while (true)

{

nap(1+(int)(random(500)));

System.out.print("A"); System.out.flush();

try

{

V(sum);

}

catch (Exception e){}

}

}

}

class Pb extends ABCs implements Runnable

{

public void run ()

{

while (true)

{

nap(1+(int)(random(800)));

P(C); P(sum);

System.out.print("B"); System.out.flush();

V(B);

}

}

}

class Pc extends ABCs implements Runnable

{

public void run ()

{

while (true)

{

nap(1+(int)(random(800)));

P(B); P(sum);

System.out.print("C"); System.out.flush();

V(C);

}

}

}

class ABCs

{

protected static final BinarySemaphore B // these semaphores

= new BinarySemaphore(0); // are static

protected static final BinarySemaphore C // so subclasses

= new BinarySemaphore(1); // Pa, Pb,

protected static final CountingSemaphore sum // and Pc share

= new CountingSemaphore(0); // them

private static final long startTime = System.currentTimeMillis();

protected static final long age()

{

return System.currentTimeMillis() - startTime;

}

private static final Random rnd = new Random();

protected static final double random()

{

return rnd.nextDouble();

}

protected static final double random(int ub)

{

return rnd.nextDouble()*ub;

}

protected static final void P(BinarySemaphore s)

{

s.P();

}

protected static final void V(BinarySemaphore s)

{

s.V();

}

protected static final void P(CountingSemaphore s)

{

s.P();

}

protected static final void V(CountingSemaphore s)

{

s.V();

}

protected static final int nap(int napTimeMS)

{

long napStart = age();

try

{

Thread.sleep(napTimeMS);

}

catch (InterruptedException e)

{

System.err.println("interrupted out of sleep");

}

return (int) (age() - napStart - (long) napTimeMS);

}

public static void main(String[] args) throws InterruptedException

{

Thread pa = new Thread(new Pa());

Thread pb = new Thread(new Pb());

Thread pc = new Thread(new Pc());

pa.start(); pb.start(); pc.start();

nap(9000);

pa.stop(); pb.stop(); pc.stop();

System.exit(0);

}

}

OUTPUT

ABACABACABACABACAAABCABCABAACBACABACAABCABACAAAABCABCBACABACABACABAC

RESULT

Thus the java program for multiprocessor operating system using semaphores is written and the output is executed successfully.

Ex.No:2CIGARETTE SMOKERS PROBLEM

Date:

AIM

To Write a Java program for multiclass multithread that uses a monitor to synchronize the agents thread and three smokers thread.

ALGORITHM

Step 1: Start the Program.

Step 2: Declare the variables to define agent and smoker and its needs.

Step 3: do forever {

P(lock);

randNum=rand(1,3); }

Step 4: if (randNum==1) {

V(smoker-match);

}

Step 5: Else if(randNum==2) {

V(smoker-paper);

}

Step 6: Else {

V(smoker-tobacco);

V(lock);

P(agent);}

Step 7: In smokers code and other analgus do forever{

P(smoker-tobacco);

P(lock);

V(agent);

V(lock); }

Step 8: Use a single monitor object instantiated from a class control for synchronization.

No smokers, semaphores, synchronized blocks are allowed, allows only

Synchronized methods.

Step 9: No smokers semaphores, synchronized blocks are allowed allows only synchronized methods.

Step 10: No class to map inside a synchronized methods are allowed.

Step 11: Each smoker thread has only thing to include inhale cigarette.

Step 12: But three smokers threads are initially blocked. The agent places two randomly chosen ingredients on the table and unblocks the smoker who has remaining ingredients agents then block

Do forever {

P(lock);

randNum=rand(1,3);

if(randNum==1)

{

V(smoker_match);

} }

Step 13: Execute the Program.

PROGRAM

import java.util.*;

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;

}

}

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()+":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){}

}

}

}

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 cigarette

{

public static void main(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();

}

}

OUTPUT

Agent:puttingtobaccomatches

Smoker 3:gettingpapermatches-No!

Smoker 1:gettingtobaccomatches-Yes!

Smoker 2:gettingtobaccopaper-No!

Smoker 1:Rolling.

Smoker 1:Smoking.

Smoker 1:Done Smoking.

Smoker 1:gettingtobaccomatches-No!

Smoker 2:gettingtobaccopaper-No!

Smoker 3:gettingpapermatches-No!

Agent:puttingtobaccomatches

Smoker 3:gettingpapermatches-No!

Smoker 2:gettingtobaccopaper-No!

Smoker 1:gettingtobaccomatches-Yes!

Smoker 1:Rolling.

Smoker 1:Smoking.

Smoker 1:Done Smoking.

Smoker 1:gettingtobaccomatches-No!

Smoker 2:gettingtobaccopaper-No!

Smoker 3:gettingpapermatches-No!

Agent:puttingpapermatches

Smoker 3:gettingpapermatches-Yes!

Smoker 3:Rolling.

Smoker 2:gettingtobaccopaper-No!

Smoker 1:gettingtobaccomatches-No!

Smoker 3:Smoking.

Smoker 3:Done Smoking.

Smoker 3:gettingpapermatches-No!

Smoker 1:gettingtobaccomatches-No!

Smoker 2:gettingtobaccopaper-No!

Agent:puttingtobaccopaper

Smoker 2:gettingtobaccopaper-Yes!

Smoker 2:Rolling.

Smoker 1:gettingtobaccomatches-No!

Smoker 3:gettingpapermatches-No!

Smoker 2:Smoking.

Smoker 2:Done Smoking.

Smoker 2:gettingtobaccopaper-No!

Smoker 3:gettingpapermatches-No!

Smoker 1:gettingtobaccomatches-No!

Agent:puttingtobaccomatches

Smoker 1:gettingtobaccomatches-Yes!

Smoker 1:Rolling.

Smoker 3:gettingpapermatches-No!

Smoker 2:gettingtobaccopaper-No!

Smoker 1:Smoking.

RESULT

Thus the java program for multi-class Multithreading- multiprocessor that uses a monitor to synchronize the agent, thread and the three smoker threads are written and the output is executed successfully.

EX.NO:3 Sleeping Barbers Problem

Date:

AIM

To write a multiclass multithread java program that simulates multiple sleeping barbers.

ALGORITHM:

Step1: The Barber (Thread/Process)

while(true)

{ run in an infinite loop

P(customers)//tries to acquire a customer if name is

available,he goes to sleep

Step 2: P(access state) at this time he has been awakened, want to modify the number of available seats.

Step 3: Number of free state++//one gets free.

Step 4: V(Barber)//the barber is ready to cut.

V(access seats)//we don’t need the lock on the chairs anyone,the barber is cutting hair.

Step 5: The customer (Thread/process)

While (true) { //run in infinite loop

P(access seats) //tries to get access the chairs

Step 6: If the number of free seats is greater than 0, are any customers sitting down on a chair, notify the barber who is waiting that there is a customer.

Step 7: V(access seats) don’t need to lock the chairs anyone, now its

this customers turn, but wait if wait the barber if busy, here the customer is having his hair cut.

Step 8: Else there are no free seats

Lock(V)(access seats) but release the lock or the seats, customers leaves without a haircut.

Step 9: Stop the program.

PROGRAM

//Main program Name SleepingBarber.java

class Semaphore extends Object

{

private int count;

public Semaphore(int startingCount)

{

count=startingCount;

}

public void down()

{

synchronized(this)

{

while(count<=0)

{

try

{

wait();

}

catch(InterruptedException ex)

{ }

}

count--;

}

}

public void up()

{

synchronized(this)

{

count++;

if(count==1)

{

notify();

}

}

}

}

public class SleepingBarber extends Thread

{

public static Semaphore customers=new Semaphore(0);

public static Semaphore barbers=new Semaphore(0);

public static Semaphore mutex=new Semaphore(1);

public static int waiting=0;

public static final int CHAIRS=5;

class Barber extends Thread

{

private int myNumber;

public Barber(int i)

{

myNumber=i;

}

public void run()

{

while(true)

{

customers.down();

mutex.down();

waiting=waiting-1;

barbers.up();

mutex.up();

cut_hair();

}

}

public void cut_hair()

{

System.out.println("Barber"+myNumber+"is cutting hair");

try

{

sleep(7500);

}

catch(InterruptedException ex)

{ }

}

}

private class Customer extends Thread

{

private int myNumber;

public Customer(int i)

{

myNumber=i;

}

public void run()

{

mutex.down();

if(waiting<CHAIRS)

{

waiting=waiting+1;

customers.up();

mutex.up();

barbers.down();

get_haircut();

}

else

{

mutex.up();

}

}

public void get_haircut()

{

System.out.println("Customers"+myNumber+"is getting his haircut");

try

{

sleep(10000);

}

catch(InterruptedException ex)

{ }

}

}

public static void main(String args[])

{

SleepingBarber holder=new SleepingBarber();

holder.start();

}

public void run()

{

final int BARBERS=3;

Barber aBarber;

Customer aCustomer;

for(int i=0;i<BARBERS;i++)

{

aBarber=new Barber(i);

aBarber.start();

}

int customerNumber=0;

while(true)

{

aCustomer=new Customer(customerNumber++);

aCustomer.start();

try

{

sleep(1000);

}

catch(InterruptedException ex){};

}

}

}

//Sub Program SleepingBarberD.java

class Semaphore extends Object

{

private int count;

public Semaphore(int startingCount)

{

count=startingCount;

}

public void down()

{

synchronized(this)

{

while(count<=0)

{

try

{

wait();

}

catch(InterruptedException ex)

{}

}

count--;

}

}

public void up()

{

synchronized(this)

{

count++;

if(count==1)

{

notify();

}

}

}

}

public class SleepingBarberD extends Thread

{

public static Semaphore customers=new Semaphore(0);

public static Semaphore barbers=new Semaphore(0);

public static Semaphore mutex=new Semaphore(1);

public static int waiting=0;

public static final int CHAIRS=5;

class Barber extends Thread

{

private int myNumber;

public Barber(int i)

{

myNumber=i;

}

public void run()

{

while(true)

{

customers.down();

mutex.down();

waiting=waiting-1;

barbers.up();

mutex.up();

cut_hair();

}

}

public void cut_hair()

{

System.out.println("Barber"+myNumber+"is cutting hair");

try

{

sleep(7500);

}

catch(InterruptedException ex)

{}

}

}

private class Customer extends Thread

{

private int myNumber;

public Customer(int i)

{

myNumber=i;

}

public void run()

{

mutex.down();

if(waiting<CHAIRS)

{

waiting=waiting+1;

customers.up();

mutex.up();

try

{

sleep(1000);

}

catch(InterruptedException ex)

{}

try

{

sleep(1000);

}

catch(InterruptedException ex)

{}

try

{

sleep(1000);

}

catch(InterruptedException ex)

{}

barbers.down();

get_haircut();

}

else

{

mutex.up();

}

}

public void get_haircut()

{

System.out.println("Customers"+myNumber+"is getting his haircut");

try

{

sleep(10000);

}

catch(InterruptedException ex)

{}

}

}

public static void main(String args[])

{

SleepingBarberD holder=new SleepingBarberD();

holder.start();

}

public void run()

{

final int BARBERS=3;

Barber aBarber;

Customer aCustomer;

for(int i=0;i<BARBERS;i++)

{

aBarber=new Barber(i);

aBarber.start();

}

int customerNumber=0;

while(true)

{

aCustomer=new Customer(customerNumber++);

aCustomer.start();

try

{

sleep(1000);

}

catch(InterruptedException ex){}

}

}

}

OUTPUT

Barber0is cutting hair

Customers0is getting his haircut

Barber1is cutting hair

Customers1is getting his haircut

Barber2is cutting hair

Customers2is getting his haircut

Barber0is cutting hair

Customers3is getting his haircut

Barber1is cutting hair

Customers4is getting his haircut

Barber2is cutting hair

Customers5is getting his haircut

Barber0is cutting hair

Customers6is getting his haircut

Barber1is cutting hair

Customers7is getting his haircut

Barber2is cutting hair

Customers8is getting his haircut

Barber0is cutting hair

Customers9is getting his haircut

Barber1is cutting hair

Customers10is getting his haircut

Barber2is cutting hair

Customers15is getting his haircut

Barber0is cutting hair

Customers16is getting his haircut

Barber1is cutting hair

Customers17is getting his haircut

RESULT

Thus the java program that simulates multiclass multithread - multiple sleeping barbers using semaphores is written and the output is executed successfully.

Ex.no:4 NETWORK OPERATING SYSTEMS

Date:

AIM

To write a program for activating LAN connections between two systems.

ALGORITHM

Step 1: Start the program.

Step 2: Declare the IP address and MAC address to open a connection between corresponding systems.

Step 3: Type ipconfig/all in command prompt, we get the IP address and MAC address of own system.

Step 4: Type the MAC address as different formats.

Step 5: Type the source code.

Step 6: Compile the program and execute.

Step 7: Run the program by declaring IP address and MAC address of destination.

PROGRAM

import java.io.*;

import java.net.*;

public class WakeOnLan

{

public static final int PORT=9;

public static void main(String[ ] args)

{

if(args.length!=2)

{

System.out.println("usuage:javaWakeOnLan1<broadcast-ip> <mac-address>");

System.out.println ("Example:java WakeOnLan192.168.1.61 00-1F-D0-B3-9c-72");

System.out.println ("Example:java WakeOnLan 192.168.1.60 00-1F-D0-B3-9c-72");

System.exit(1);

}

String ipStr=args[0];

String macStr=args[1];

try

{

byte[ ] macBytes=getMacBytes(macStr);

byte[] bytes=new byte[6+16*macBytes.length];

for(int i=0;i<6;i++)

{

bytes[i]=(byte)0xff;

}

for(int i=6;i<bytes.length;i+=macBytes.length)

{

System.arraycopy(macBytes, 0,bytes,i,macBytes.length);

}

InetAddress address = InetAddress.getByName(ipStr);

DatagramPacket packet = new DatagramPacket(bytes,bytes.length,address,PORT);

DatagramSocket socket= new DatagramSocket();

socket.send(packet);

socket.close();

System.out.println("wake on lan packet sent");

}

catch(Exception e)

{

System.out.println("failed to send wake on lan packet:+e");

System.exit(1);

}

}

private static byte[] getMacBytes(String macStr) throws IllegalArgumentException

{

byte[] bytes=new byte[6];

String[] hex=macStr.split("(\\:|\\-)");

if(hex.length!=6)

{

throw new IllegalArgumentException("Invalid MAC address");

}

try

{

for(int i=0;i<6;i++)

{

bytes[i]=(byte)Integer.parseInt(hex[i],16);

}

}

catch(NumberFormatException e)

{

throw new IllegalArgumentException("invalid hex digit in MAC address");

}

return bytes;

}

}

OUTPUT

F:\Java\jdk1.7.0\bin>java WakeOnLan 192.168.1.61 00-1F-D0-B3-9c-72

Wake on lan packet sent

RESULT

Thus the program for activating LAN connections between two systems is written and the output is executed successfully.

Ex.no:5REAL TIME OS

Date:

AIM

To write a program using Real time operating system for implementing an alarm clock.

ALGORITHM

Step 1: Start the program.

Step 2: Display init is used for initialization and shall be called from the main function of the program before the processes are created.

Step 3: Display alarm time ashows the current time and shall be called when a new alarm

time is set.

Step 4: Display time is used to display the current time and shall be called by the clock

process.

Step 5: Erase the alarm time, erases the displayed alarm time and shall be called when the

user acknowledges an alarm.

Step 6: Display alarm text is used to show an alarm activation and the user is informed that

the alarm has been activated.

Step 7: when the alarm is activated the first time and when the alarm is activated repeatedly.

Step 8: Erase alarm text,erase the information displayed by displays the alarm text.

Step 9: Stop the program.

PROGRAM

#include<stdio.h>

#include<conio.h>

#include<dos.h>

struct clk

{

int hh,mm,ss;

}c1,c2;

void clock(int *h1,int *m1,int *s1)

{

*s1=*s1+1;

if(*s1==60)

{

*s1=0; *m1=*m1+1;

if(*m1==60)

{

*m1=0;*h1=*h1+1;

if(*h1==24)

*h1=0;

}

}

}

void timer(int *h,int *m,int *s)

{

if((*s)!=0)

{

*s=*s-1;

}

else if((*s)==0)

{

if(*m!=0)

{

*s=59;*m=*m-1;

}

else if(*m==0)

{

if(*h!=0)

{

*m=59;*h=*h-1;

}

}

}

}

void alarm()

{

int i;

while(!kbhit())

{

for(i=0;i<2;i++)

{

sound(50);

delay(100);

nosound();

delay(200);

}

delay(500);

}

}

void main()

{

char ch;

struct time t;

clrscr();

printf("\nPress:-\n\tA: for alarm Clock\n\tT: for Timer\n");

printf("\Enter your Choice:");

ch=getche();

switch (ch)

{

case 'A':

case 'a':

{

printf("\n\n\n24 hr Format(HH:MM:SS)");

gettime(&t);

c1.hh=t.ti_hour; c1.mm=t.ti_min; c1.ss=t.ti_sec;

printf("\nEnter alarm time : ");

scanf("%d%d%d",&c2.hh,&c2.mm,&c2.ss);