\Operating System Lab Manual(Rvk)

56
CS9227 OPERATING SYSTEM LAB MULTIPROCESSOR OPERATING SYSTEMS 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 TOTAL : 45 PERIODS MULTI P ROCE S SOR OP E RAT I NG S Y S T E M( AB Cs) A IM: To write a java program for multiprocessor operating system using semaphores. A LGORI T HM:

description

Laboratory Manual

Transcript of \Operating System Lab Manual(Rvk)

Page 1: \Operating System Lab Manual(Rvk)

CS9227

OPERATING SYSTEM LAB

MULTIPROCESSOR OPERATING SYSTEMS

PROGRAM 1 – Semaphores - Multiprocessor operating systemsAssume 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 processesare 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 theoutput string cannot exceed the number of A's which have been output up to thatpoint.Examples AACB -- invalid, violates a)ABACAC -- invalid, violates b)AABCABC -- invalid, violates c)AABCAAABC -- valid AAAABCBC -- validAB -- valid

TOTAL : 45 PERIODS

MULTI P ROCE S SOR OP E RAT I NG S Y S T E M( AB Cs)

A IM: To write a java program for multiprocessor operating system

usingsemaphores.

A LGORI T HM:

Page 2: \Operating System Lab Manual(Rvk)

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.

Page 3: \Operating System Lab Manual(Rvk)

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 andQ2:=FALSE.Step 9: B’s and C’s must be alternate in the output String i.e) after the first B isdisplayed, 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 isdisplayed.Step 11: The total number of B’s and C’s which have been displayed at any givenpoint in the output string cannot exceed. Step 12: Utilize only semaphores, the processes are synchronized so that the outputis satisfied. Step 13: Execute the program.

P ROGRA M :

A BCs. j a v a

import java.io.Serializable;import java.util.Date;import java.util.Random;

class BinarySemaphore{

private boolean locked = false; BinarySemaphore() {} // constructorsBinarySemaphore(boolean initial) {

locked = initial; }BinarySemaphore(int initial){

locked = (initial == 0);}

Page 4: \Operating System Lab Manual(Rvk)

public synchronized void P(){

Page 5: \Operating System Lab Manual(Rvk)

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{

Page 6: \Operating System Lab Manual(Rvk)

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);

Page 7: \Operating System Lab Manual(Rvk)

} 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

Page 8: \Operating System Lab Manual(Rvk)

= 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();

Page 9: \Operating System Lab Manual(Rvk)

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);

}}

OUT P U T:

D:\Java\jdk1.6.0\bin>javac ABCs.javaD:\Java\jdk1.6.0\bin>java ABCs

ABACABACABACAAABCABACBACABACABACABACABACABACABACAAAABCAAABCABCAA

RESUL T :

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

Page 10: \Operating System Lab Manual(Rvk)

PROGRAM 2 – Multithreading - Multiprocessor operating systemsThe Cigarette Smokers Problem

Consider a simulation with three smoker threads and one agent thread. Eachsmoker continuously makes a cigarette and smokes it. But to make a cigarette, asmoker needs three ingredients: tobacco, paper, and matches. One of the smokerthreads has only paper, another has only tobacco, and the third has only matches.The agent thread has an infinite supply of all three materials. The three smokerthreads are initially blocked. The agent places two randomly chosen (different)ingredients on the table and unblocks the one smoker who has the remainingingredient. The agent then blocks. The unblocked smoker removes the twoingredients from the table, makes a cigarette, and smokes it for a random amount oftime, unblocking the agent on completion of smoking the cigarette. The agent thenputs out another random two of the three ingredients, and the cycle repeats.

Write a multi-class multithreaded Java program that uses a monitor tosynchronize the agent thread and the three smoker threads. Do not mechanicallytranslate semaphore code into monitor code! The agent thread executes in anagent object created from an agent class. Each smoker thread executes in a smokerobject. All smoker objects are created from one smoker class whose constructor isused to specify the ingredient possessed by the smoker object. A driver class with amain method constructs the objects and starts the threads. Use a single monitorobject instantiated from a class Control for synchronization. Each of the fourthreads invokes a synchronized monitor method for its synchronization. Nosemaphores are allowed. No synchronized blocks are allowed, only synchronizedmethods. No busy waiting is allowed. No calls to nap inside a synchronized

Page 11: \Operating System Lab Manual(Rvk)

methodare allowed (do not nap while holding the monitor object's lock, that is, while insidea synchronized method or while inside a method called by a synchronized method).

CI G R E TTE SM O K E RS PRO B LEM

A IM: To Write a Java program for multiclass multithread that uses a

monitor tosynchronize the agents thread and three smokers thread.

A LGORI T HM:

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

Page 12: \Operating System Lab Manual(Rvk)

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 forsynchronization. No smokers, semaphores, synchronized blocks are allowed, allowsonly Synchronized methods.Step 9: No smokers semaphores, synchronized blocks are allowed allows onlysynchronized 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 tworandomly chosen ingredients on the table and unblocks the smoker who hasremaining ingredients agents then block

Do forever {P(lock);randNum=rand(1,3);if(randNum==1){V(smoker_match);} }

Step 13: Execute the Program.

Page 13: \Operating System Lab Manual(Rvk)

P ROGRA M :

A g e nt.j a va import java.util.*;

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);

Page 14: \Operating System Lab Manual(Rvk)

break;

}

}

}

}

S m ok e r .ja v a

import java.util.*;

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

Page 15: \Operating System Lab Manual(Rvk)

{

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){}

}

}

}

Ta b le . java import java.util.*;

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;

Page 16: \Operating System Lab Manual(Rvk)

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();

Page 17: \Operating System Lab Manual(Rvk)

}

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;

}

}

Ta b leCS . java import java.util.*;

Page 18: \Operating System Lab Manual(Rvk)

public class TableCS extends Table

{

TableCS Table;

}

cig a rett e .ja v a

import java.util.*;

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();

}

}

Page 19: \Operating System Lab Manual(Rvk)

OUT P U T:

D:\Java\jdk1.6.0\bin>javac Cigrate.java

D:\Java\jdk1.6.0\bin>java Cigrate

Agent:Puttingtobaccopaper

Smoker2:Gettingpapermatches-No!

Smoker1:Gettingtobaccomatches-No!

Smoker3:I Got What I Need!!!

Smoker3:Rolling!!

Smoker3:Smoking!!

Smoker3:Done Smoking!!

Smoker3:I Got What I Need!!!

Smoker1:Gettingtobaccomatches-Yes!

Smoker3:Rolling!!

Smoker1:Gettingtobaccomatches-No!

Smoker2:Gettingpapermatches-Yes!

Smoker2:Gettingpapermatches-No!

Agent:Puttingtobaccomatches

Smoker2:Gettingpapermatches-Yes!

Smoker2:Gettingpapermatches-No!

Smoker1:Gettingtobaccomatches-Yes!

Smoker1:Gettingtobaccomatches-No!

Smoker3:Smoking!!

Page 20: \Operating System Lab Manual(Rvk)

Smoker3:Done Smoking!!

Smoker3:Gettingtobaccopaper-No!

Smoker1:Gettingtobaccomatches-Yes!

Smoker1:Gettingtobaccomatches-No!

Smoker2:Gettingpapermatches-Yes!

Smoker2:Gettingpapermatches-No!

Agent:Puttingtobaccopaper

Smoker2:Gettingpapermatches-Yes!

Smoker2:Gettingpapermatches-No!

Smoker1:Gettingtobaccomatches-Yes!

Smoker1:Gettingtobaccomatches-No!

Smoker3:Gettingtobaccopaper-Yes!

Smoker3:I Got What I Need!!!

Smoker3:Rolling!!

Smoker3:Smoking!!

Smoker3:Done Smoking!!

Smoker1:Gettingtobaccomatches-Yes!

Smoker1:Gettingtobaccomatches-No!

Smoker2:Gettingpapermatches-

Page 21: \Operating System Lab Manual(Rvk)

Yes!

Smoker2:Gettingpapermatches-No!

Smoker3:Gettingtobaccopaper-No!

Agent:Puttingpapermatches

Page 22: \Operating System Lab Manual(Rvk)

Smoker3:Gettingtobaccopaper-Yes!

Smoker3:Gettingtobaccopaper-No!

Smoker2:Gettingpapermatches-Yes!

Smoker2:Gettingpapermatches-No!

Smoker1:Gettingtobaccomatches-Yes!

Smoker1:Gettingtobaccomatches-No!

RESULT:

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

Page 23: \Operating System Lab Manual(Rvk)

PROGRAM 3 – Multiple sleeping barbers - Multiprocessor operatingsystems

Write a multi-class multithreaded Java program that simulates multiplesleeping barbers, all in one barbershop that has a finite number of chairs in thewaiting room. Each customer is instantiated from a single Customer class, eachbarber is instantiated from a single Barber class.

SL E EPING B A R B E R S PROBLEM

A IM: To write a multiclass multithread java program that simulates

multiplesleeping barbers.

A LGORI T HM:

Step1: The Barber (Thread/Process) while(true){ run in an infinite loopP(customers)//tries to acquire a customer if name is

available,he goes to sleepStep 2: P(access state) at this time he has been awakened, want to modify thenumber 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 downon 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

Page 24: \Operating System Lab Manual(Rvk)

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 theseats, customers leaves without a haircut.Step 9: Stop the program.

Page 25: \Operating System Lab Manual(Rvk)

P ROGRA M :

Sl e epi n gB a rb e r . ja v a

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();}

}}

}

Page 26: \Operating System Lab Manual(Rvk)

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) { }

}}

Page 27: \Operating System Lab Manual(Rvk)

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[]){

Page 28: \Operating System Lab Manual(Rvk)

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){};

}}

}

Sl e epi n gB a rb e rD.j a va

class Semaphore extends Object {

private int count;public Semaphore(int startingCount){

count=startingCount;}public void down(){

synchronized(this)

Page 29: \Operating System Lab Manual(Rvk)

{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){

Page 30: \Operating System Lab Manual(Rvk)

myNumber=i;

Page 31: \Operating System Lab Manual(Rvk)

}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;

Page 32: \Operating System Lab Manual(Rvk)

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 hishaircut");

try {

sleep(10000); } catch(InterruptedException ex) {}

Page 33: \Operating System Lab Manual(Rvk)

}}

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){}

}}

}

OUT P U T:

D:\Java\jdk1.6.0\bin> javac SleepingBarber.java

D:\Java\jdk1.6.0\bin> java SleepingBarber

Page 34: \Operating System Lab Manual(Rvk)

Barber0is cutting hair

Customers0is getting his haircut

Customers1is getting his haircut

Barber1is cutting hair

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

Page 35: \Operating System Lab Manual(Rvk)

Barber1is cutting hair

Customers10is getting his haircut

Barber2is cutting hair

Customers16is getting his haircut

Page 36: \Operating System Lab Manual(Rvk)

Barber0is cutting hair

Customers17is getting his haircut

Customers18is getting his haircut

Barber1is cutting hair

Barber2is cutting hair

Customers23is getting his haircut

Barber0is cutting hair

Customers24is getting his haircut

Barber1is cutting hair

Customers25is getting his haircut

Barber2is cutting hair

Customers31is getting his haircut

RESULT:

Thus the java program that simulates multiclass multithread - multiplesleeping barbers using semaphores is written and the output is executedsuccessfully

Page 37: \Operating System Lab Manual(Rvk)

NETWORK OPERATING SYSTEMS

PROGRAM 4 – Network operating systemsEstablish a Lab setup for the following network operating systems based programsbased on the skills in networking on your own. E.g. for identifying networkinghardware, identifying different kinds of network cabling and network interfacecards can be done.

Exercises1. Identifying Local Area Network Hardware2. Exploring Local Area Network Configuration Options3. Verifying TCP/IP Settings4. Sharing Resources5. Testing LAN Connections

NET W ORK OPER A TING SYSTEM

A im: To write a program for activating LAN connections between two

systems.

A lg o r i t hm :

Step 1: Start the program.Step 2: Declare the IP address and MAC address to open a connection betweencorresponding systems.Step 3: Type ipconfig/all in command prompt, we get the IP address and MACaddress 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.

P ROGRA M :

W a keOn L an.j a v a

Page 38: \Operating System Lab Manual(Rvk)

import java.io.*;import java.net.*;

Page 39: \Operating System Lab Manual(Rvk)

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 WakeOnLan1

172.15.169.700-15-58-AC-0C-20");System.out.println ("Example:java WakeOnLan1

172.15.169.800-15-58-AC-0C-27"); 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);DatagramPacketpacket

=newDatagramPacket(bytes,bytes.length,

address, PORT);DatagramSocket socket= new DatagramSocket();

Page 40: \Operating System Lab Manual(Rvk)

socket.send(packet);socket.close();

Page 41: \Operating System Lab Manual(Rvk)

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)

throwsIllegalArgumentException

{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;

}}

Page 42: \Operating System Lab Manual(Rvk)

OUT P U T:

D:\Java\jdk1.6.0\bin>javac WakeOnLan.java

D:\Java\jdk1.6.0\bin>java WakeOnLan 172.15.169.7 00-15-58-AC-0C-20

Wake on LAN packet sent

RESU L T : Thus the program for activating LAN connections between two systems is written and the

output is executed successfully.

Page 43: \Operating System Lab Manual(Rvk)

REAL TIME OPERATING SYSTEMSPROGRAM 5 – Real time operating systems

A real-time program implementing an alarm clock shall be developed. [Alarmclock, using C and Simple_OS] The program shall fulfill the following requirements:

Clock with alarm functionality shall be implemented, It shall be possible toset the time, It shall be possible to set the alarm time, the alarm shall be ENABLED

when the alarm time is set, the alarm shall be ACTIVATED when the alarm is enabled,and when the current time is equal to the alarm time, an activated alarm must beacknowledged. Acknowledgement of an alarm shall lead to the alarm beingDISABLED, THE alarm is enabled again when a new alarm time is set, an alarm whichis not acknowledged shall be repeated every 10 seconds. The program shallcommunicate with a graphical user interface, where the current time shall bedisplayed, and where the alarm time shall be displayed when the alarm is enabled.It shall be possible to terminate the program, using a command which is sent fromthe graphical user interface.

RE A L TIM E OS A IM: To write a program using Real time operating system for implementing an alarmclock.

A LGORI T HM: Step 1: Start the program.Step 2: Display init is used for initialization and shall be called from the mainfunction of the program before the processes are created.Step 3: Display alarm time a shows the current time and shall be called when a newalarm time is set.Step 4: Display time is used to display the current time and shall be called

Page 44: \Operating System Lab Manual(Rvk)

by theclock process.Step 5: Erase the alarm time, erases the displayed alarm time and shall be calledwhen the user acknowledges an alarm.Step 6: Display alarm text is used to show an alarm activation and the user isinformed that the alarm has been activated.Step 7: when the alarm is activated the first time and when the alarm is activatedrepeatedly.Step 8: Erase alarm text,erase the information displayed by displays the alarm text.Step 9: Stop the program.

Page 45: \Operating System Lab Manual(Rvk)

P ROGRA M :

A lar m . c

#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;

}

Page 46: \Operating System Lab Manual(Rvk)

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(5000);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':

{

Page 47: \Operating System Lab Manual(Rvk)

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);if(c2.hh>24||c2.mm>60||c2.ss>60){ printf("\n\n\tERROR: Invalid time.\n\tRestart the program.");

delay(2500);exit(0);}

while((c1.ss!=c2.ss)||(c1.hh!=c2.hh)||(c1.mm!=c2.mm)){

clrscr();printf("\n\nAlarm time:

%02d:%02d:%02d\n",c2.hh,c2.mm,c2.ss);printf("\nCurrent Time:

%02d:%02d:%02d",c1.hh,c1.mm,c1.ss); clock(&c1.hh,&c1.mm,&c1.ss);delay(1000); };

clrscr();printf("\n\n\n\n\t\t\t\tAlarm time me reached

\n\n\t\t\t\tPress any to Exit.");alarm();

exit(0);}

break; case 'T': case 't':

{ printf("\n\n\nEnter time for timer (HH:MM:SS): "); scanf("%d:%d:%d",&c1.hh,&c1.mm,&c1.ss);while(c1.hh>0||c1.mm>0||c1.ss>0){

clrscr();printf("The Current Time:\n");

Page 48: \Operating System Lab Manual(Rvk)

printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t"); printf("%02d:%02d:%02d",c1.hh,c1.mm,c1.ss);timer(&c1.hh,&c1.mm,&c1.ss);delay(1000);

} clrscr();printf("Program Written by: Anshu Krishna\n");printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t"); printf("00:00:00");alarm();exit(0);

} break;

default:{

printf("\n\tInvalid Input\n\n\tPlease restart the program");delay(2500);exit(0);

} }

}

OUT P U T :

Press:-A: for alarm ClockT: for Timer

Enter your Choice:A

24 hr Format(HH:MM:SS)Enter alarm time : 22:30:50

Alarm time: 22:20:50Current Time: 22:19:53

Alarm time reached

Page 49: \Operating System Lab Manual(Rvk)

Press any to Exit.

Press:-A: for alarm ClockT: for Timer

Enter your Choice: T

Enter time for timer (HH:MM:SS): 22:25:20

The Current Time:22:25:06

Press:-A: for alarm ClockT: for Timer

Enter your Choice:2Invalid Input

Please restart the program

RESUL T :Thus the program using Real time operating system for

implementing analarm clock.is written and the output is executed successfully.