Beruflich Dokumente
Kultur Dokumente
AIM:
To write a C program in UNIX to design a multiprocessor operating
system using semaphores.
ALGORITHM:
Using pthreadcreate(), creates three process:Pa , Pb , Pc.
1
PROGRAM:
#include<pthread.h>
#include<semaphore.h>
#include<math.h>
#include<stdio.h>
sem_t mutex,full,empty,empty1,full1,mutex2;
void *a();
void *b();
void *c();
int main()
{
char *arg;
pthread_t pa,pb,pc;
sem_init(&mutex,0,1);
sem_init(&empty,0,5);
sem_init(&full,0,0);
sem_init(&empty1,0,1);
sem_init(&full1,0,0);
sem_init(&mutex2,0,1);
pthread_create(&pa,NULL,a,arg);
pthread_create(&pb,NULL,b,arg);
pthread_create(&pc,NULL,c,arg);
pthread_join(pa,NULL);
}
void *a()
{
int i=0;
while(i<10)
{
//sem_wait(&mutex2);
sem_wait(&empty);
sem_wait(&mutex2);
sem_wait(&mutex);
printf("A");
sem_post(&mutex);
sem_post(&full);
sem_post(&mutex2);
i++;
2
}
}
void *b()
{
int i=0;
while(i<10)
{
sem_wait(&full);
sem_wait(&empty1);
sem_wait(&mutex2);
sem_wait(&mutex);
printf("B");
sem_post(&mutex);
sem_post(&full1);
sem_post(&empty);
i++;
}
}
void *c()
{
int i=0;
while(i<10)
{
sem_wait(&full1);
sem_wait(&full);
sem_wait(&mutex);
printf("C");
sem_post(&mutex);
sem_post(&empty1);
sem_post(&empty);
sem_post(&mutex2);
}
}
RESULT:
Thus the program for implementing multiprocessor operating system using
Semaphore was executed successfully.
3
EX.NO:2 MULTIPROCESSOR OPERATING SYSTEMS –
DATE: MULTITHREADING
AIM:
To write a Java program to design a multiprocessor operating system using
multithreading.
ALGORITHM:
Import necessary header files
Create three Smoker threads(tobacco, paper, and matches) that 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 unblocked smoker removes the two ingredients from the table, makes a
cigarette, and smokes it for a random amount of time and the cycle repeats.
4
PROGRAM:
import java.util.*;
public class Cigarette
{
static 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;
}
5
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;
}
static 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);
6
break;
}}}}
static 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 static void main(String[] args)
{
Table table = new Table();
Agent agent = new Agent(table, "Agent");
Smoker smo1 = new Smoker(table, " Smoker 1", Table.Paper);
Smoker smo2 = new Smoker(table, " Smoker 2", Table.Matches);
Smoker smo3 = new Smoker(table, " Smoker 3", Table.Tobacco);
agent.start();
smo1.start();
smo2.start();
smo3.start();
}}
7
RESULT:
Thus the multiprocessor operating system for multithreading has been
implemented and executed successfully.
8
EX.NO:3 MUTIPLE SLEEPING BARBERS -
DATE: MULTIPROCESSOR OPERATING SYSTEMS
AIM:
To write a multi-class multithreded java program that simulates multiple
sleeping barbers.
ALGORITHM:
Create an array for the customers.
Use sleep( ) to control the speed of customers.
Allow the customer to enter the shop if there exist an empty chair.
The existence of the empty chair can be checked using isFull( ).
After barbering is done the barber can go to sleep using the semaphores.
9
PROGRAM:
public class SleepingBarber1
{
public static void main(String args[])
{
//Here is the main function
System.out.println("This is a barber shop with 5 chairs for waiting.");
System.out.println("Here is 7 customers want to cut hair.");
System.out.println("First,the barber is sleeping for there is no customer.");
Database server = new Database();
Barber[] barberArray = new Barber[NUM_OF_CUST];
for (int i = 0; i < NUM_OF_CUST; i++)
{
barberArray[i] = new Barber(i, server);
barberArray[i].start();
try { //this try is for controling the speed of everyone who go into the barber shop
barberArray[i].sleep(1000-20*i);
}
catch (InterruptedException f) {
}}}
private static final int NUM_OF_CUST = 7;
}
class Database
{
public Database()
{
waitCount = 0;
maxCustomer = 5;
mutex = new Semaphore(1);
db = new Semaphore(maxCustomer + 1);
}
public static void napping() {
int sleepTime = (int) (NAP_TIME * Math.random() );
try { Thread.sleep(sleepTime*100); }
catch(InterruptedException e) {}
}
public int startWait()
{
db.P();
10
++waitCount;//the number of customers add one
System.out.println("There are " + waitCount + " chairs occupied.");
if (waitCount < maxCustomer)
{ mutex.P();}
waitCount--;
db.V();
return waitCount;
}
11
System.out.println("customer-" + barberNum + " enter shop.");
c = server.startWait();
if(c >= server.maxCustomer)
{
System.out.println("customer-" + barberNum + "have to go away for no chairs.");
isFull = true;
}
if(c < server.maxCustomer)
{
System.out.println("customer-" + barberNum + " is barbering. There are " + c + " chairs
occupied.");
Database.napping();
System.out.print("customer-" + barberNum + " is done barbering. ");
c = server.endWait();
}
if(isFull = true)
break;;
}}
private Database server;
private int barberNum;
private boolean isFull;
}
final class Semaphore//create the semaphore for this program
{
public Semaphore()
{
value = 0;
}
public Semaphore(int v)
{
value = v;
}
public synchronized void P()
{
while (value <= 0)
{
try {
wait();
}
catch (InterruptedException e) { }
}
12
value --;
}
public synchronized void V() {
++value;
notify();
}
private int value;
}
RESULT:
Thus the multi-class multithreaded java program that simulates multiple sleeping
barbers was done and executed successfully.
13
EX.NO:4 NETWORK OPERATING SYSTEMS
DATE:
AIM:
To study about the Network Operating System.
PROCEDURE:
HUB:
A network hub or repeater hub is a device for connecting multiple twisted pair
or fiber optic Ethernet devices together and making them act as a single network
segment.
The need for hosts to be able to detect collisions limits the number of hubs
and the total size of a network built using hubs. Some hubs have special stack
ports allowing them to be combined in a way that allows more hubs than simple
chaining through Ethernet cables, but even so, a large Fast Ethernet network is
likely to require switches to avoid the chaining limits of hubs.
14
ROUTER:
A router, is a computer used to forward data among computer networks
beyond directly connected devices. (The directly connected devices are said to be
in a LAN, where data are forwarded using Network switches.)
SWITCH:
15
EXPLORING LOCAL AREA NETWORK CONFIGURATION OPTIONS:
Step:1double click network connections
16
SHARING RESOURCES:
STEP:1 Right click on any of the local drive -> sharing and security
17
VERIFYING TCP/IP SETTINGS:
18
RESULT:
Thus the Network Operating System has been studied.
19
EX.NO:5 REAL TIME OPERATING SYSTEMS
DATE:
AIM:
To write a C program to implement an alarm clock in real time application.
ALGORITHM:
Enter your choice for alarm or timer.
Enter ‘A’ to set the alarm time in the given format.
Alarm sound will hear at the time set.
Enter ‘T’ to get the timer.
20
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 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();
21
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 reached\n\n\t\t\t\tPress any to Exit.");
alarm();
exit(0);
}
break;
default:
{
printf("\n\tInvalid Input\n\n\tPlease restart the program");
delay(2500);exit(0);
}
}
}
RESULT:
Thus the real time application was implemented using C.
22
EX.NO:6 DATABASE OPERATING SYSTEMS
DATE:
AIM:
To perform transaction and concurrency operations in database operating systems.
ALGORITHM:
Sign in to your account.
If you are the new user,create a new account using create( ).
Perform deposit,withdraw and balance checking operations.
Transaction can also be performed by entering the amount and account number
details.
Sign out after performing the transactions.
23
PROGRAM:
import java.io.*;
import java.util.Random;
class server extends Thread
{
String[] acc_no={"AC1"};
static int[] amt={1000};
public void recvmsg(String ac_no,String msg,int val)
{
String m=msg;
String ano=ac_no;
int v=val,bal;
System.out.println("Before Transaction");
System.out.println("ACCOUNT NO.:"+acc_no[0]);
System.out.println("BALANCE :"+amt[0]);
if(m=="withdraw")
{
try{
Thread.sleep(1000);
bal=amt[0];
Thread.sleep(50);
bal=bal-v;
Thread.sleep(90);
amt[0]=bal;
Thread.sleep(30);
}
catch(Exception e)
{
}
}
if(m=="deposit")
{
try{
Thread.sleep(20);
24
amt[0]=amt[0]+v;
Thread.sleep(120);
}
catch(Exception e)
{
}
}
System.out.println("ACCOUNT NO.:"+acc_no[0]);
System.out.println("BALANCE :"+amt[0]);
}
}
class customer extends Thread
{
server s=new server();
String a2,m2;
int amt;
public customer(String acc,String mess,int amount)
{
a2=acc;
m2=mess;
amt=amount;
System.out.println("ACCOUNT NO.:"+acc);
System.out.println("MESSAGE :"+mess);
System.out.println("AMOUNT :"+amount);
}
public void run()
{
try{
Thread.sleep(1000);
s.recvmsg(a2,m2,amt);
}
catch(Exception e)
{
25
}
}
}
class resolve
{
public static void main(String args[])
{
customer c1,c2;
c1=new customer("AC1","withdraw",500);
c2=new customer("AC1","deposit",200);
System.out.println("withdraw 500");
System.out.println("deposit 200");
c1.start();
c2.start();
}
}
RESULT:
Thus the database operating system has been implemented and executed successfully.
26
EX.NO:7 DISTRIBUTED OPERATING SYSTEMS
DATE:
AIM:
To design a RMI Lottery Application.
ALGORITHM:
To create a RMI application, the first step is to design an interface. Create the
interface program “Lottery.java”
Open another MS-Dos window, type java LotteryServer to run the server
program
Open the third MS-Dos window, type java LotteryClient n, to run the client
program, where n is the set of lottery numbers to be generated.
27
PROGRAM:
Lottery.java
LotteryImpl.java
import java.util.*;
28
LotteryServer.java
import java.rmi.Naming;
public class LotteryServer
{
public LotteryServer()
{
try
{
Lottery c = new LotteryImpl();
Naming.rebind("rmi://localhost:1099/LotteryService", c);
}
catch (Exception e)
{
System.out.println("Trouble: " + e);
}
}
public static void main(String args[])
{
new LotteryServer();
}
}
LotteryClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
29
lottery c = (lottery)
Naming.lookup("rmi://localhost/LotteryService");
c.generate(num1);
}
catch (MalformedURLException murle)
{
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re)
{
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe)
{
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae)
{
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
RESULT:
A RMI lottery application is designed.
30