Upload
subha-chinnasamy
View
260
Download
1
Tags:
Embed Size (px)
DESCRIPTION
business
Citation preview
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
MULTIPROCESSOR OPERATING SYSTEM(ABCs)
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:
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:
D:\Java\jdk1.6.0\bin>javac ABCs.java
D:\Java\jdk1.6.0\bin>java ABCs
ABACABACABACAAABCABACBACABACABACABACABACABACABACAAAABC
AAABCABCAA
RESULT:
Thus the java program for multiprocessor operating system using
semaphores is written and the output is executed successfully.
PROGRAM 2 – Multithreading - Multiprocessor operating systems
The Cigarette Smokers Problem
Consider a simulation with three smoker threads and one agent thread. Each
smoker continuously makes a cigarette and smokes it. But to make a cigarette, a
smoker needs three ingredients: tobacco, paper, and matches. One of the smoker
threads 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 smoker
threads are initially blocked. The agent places two randomly chosen (different)
ingredients on the table and unblocks the one smoker who has the remaining
ingredient. The agent then blocks. The unblocked smoker removes the two
ingredients from the table, makes a cigarette, and smokes it for a random amount of
time, unblocking the agent on completion of smoking the cigarette. The agent then
puts out another random two of the three ingredients, and the cycle repeats.
Write a multi-class multithreaded Java program that uses a monitor to
synchronize the agent thread and the three smoker threads. Do not mechanically
translate semaphore code into monitor code! The agent thread executes in an
agent object created from an agent class. Each smoker thread executes in a smoker
object. All smoker objects are created from one smoker class whose constructor is
used to specify the ingredient possessed by the smoker object. A driver class with a
main method constructs the objects and starts the threads. Use a single monitor
object instantiated from a class Control for synchronization. Each of the four
threads invokes a synchronized monitor method for its synchronization. No
semaphores are allowed. No synchronized blocks are allowed, only synchronized
methods. No busy waiting is allowed. No calls to nap inside a synchronized method
are allowed (do not nap while holding the monitor object's lock, that is, while inside
a synchronized method or while inside a method called by a synchronized method).
CIGRETTE SMOKERS PROBLEM
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:
Agent.java
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);
break;
}
}
}
}
Smoker .java
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
{
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){}
}
}
}
Table.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;
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;
}
}
TableCS.java
import java.util.*;
public class TableCS extends Table
{
TableCS Table;
}
cigarette.java
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();
}
}
OUTPUT:
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!!
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-Yes!
Smoker2:Gettingpapermatches-No!
Smoker3:Gettingtobaccopaper-No!
Agent:Puttingpapermatches
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 that
uses a monitor to synchronize the agent, thread and the three smoker threads are
written and the output is executed successfully.
PROGRAM 3 – Multiple sleeping barbers - Multiprocessor operating
systems
Write a multi-class multithreaded Java program that simulates multiple
sleeping barbers, all in one barbershop that has a finite number of chairs in the
waiting room. Each customer is instantiated from a single Customer class, each
barber is instantiated from a single Barber class.
SLEEPING BARBERS PROBLEM
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:
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){};
}
}
}
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:
D:\Java\jdk1.6.0\bin> javac SleepingBarber.java
D:\Java\jdk1.6.0\bin> java SleepingBarber
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
Barber1is cutting hair
Customers10is getting his haircut
Barber2is cutting hair
Customers16is getting his haircut
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 - multiple
sleeping barbers using semaphores is written and the output is executed
successfully
NETWORK OPERATING SYSTEMS
PROGRAM 4 – Network operating systems
Establish a Lab setup for the following network operating systems based programs
based on the skills in networking on your own. E.g. for identifying networking
hardware, identifying different kinds of network cabling and network interface
cards can be done.
Exercises
1. Identifying Local Area Network Hardware
2. Exploring Local Area Network Configuration Options
3. Verifying TCP/IP Settings
4. Sharing Resources
5. Testing LAN Connections
NETWORK OPERATING SYSTEM
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:
WakeOnLan.java
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 WakeOnLan1 172.15.169.7
00-15-58-AC-0C-20");
System.out.println ("Example:java WakeOnLan1 172.15.169.8
00-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();
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:
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
RESULT:
Thus the program for activating LAN connections between two systems is written and the output is executed successfully.
REAL TIME OPERATING SYSTEMS
PROGRAM 5 – Real time operating systems
A real-time program implementing an alarm clock shall be developed. [Alarm
clock, using C and Simple_OS]
The program shall fulfill the following requirements:
Clock with alarm functionality shall be implemented, It shall be possible to
set 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 be
acknowledged. Acknowledgement of an alarm shall lead to the alarm being
disabled, the alarm is enabled again when a new alarm time is set, an alarm which
is not acknowledged shall be repeated every 10 seconds. The program shall
communicate with a graphical user interface, where the current time shall be
displayed, 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 from
the graphical user interface.
REAL TIME OS
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 a shows 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:
Alarm.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;
}
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':
{
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");
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);
}
}
}
OUTPUT:
Press:-
A: for alarm Clock
T: for Timer
Enter your Choice:A
24 hr Format(HH:MM:SS)
Enter alarm time : 22:30:50
Alarm time: 22:20:50
Current Time: 22:19:53
Alarm time reached
Press any to Exit.
Press:-
A: for alarm Clock
T: 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 Clock
T: for Timer
Enter your Choice:2
Invalid Input
Please restart the program
RESULT:
Thus the program using Real time operating system for implementing an
alarm clock.is written and the output is executed successfully.