Sie sind auf Seite 1von 30

EX.

NO:1 MULTIPROCESSOR OPERATING SYSTEMS-


DATE: SEMAPHORES

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.

 Initialize semaphores using sem_init() method.

 Sem_wait() is used to lock the semaphore referenced by sem.

 Sem_post() is used to unlock the semaphore.

 Using the appropriate semaphore functions. Execute the three processes,


such that the given condition is satisfied.

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

public int endWait()


{
db.P();
if (waitCount == 0)
{
System.out.println("The barber begin to sleep .");
}
mutex.V();
db.V();
System.out.println("There are " + waitCount + " chairs occupied.");
return waitCount;
}
private int waitCount;
public int maxCustomer;
Semaphore mutex; // controls access to readerCount
Semaphore db; // controls access to the database
private static final int NAP_TIME = 25;
}
class Barber extends Thread
{
public Barber(int r, Database db)
{
barberNum = r;
server = db;
isFull = false;
}
public void run()
{
int c;
while (true)//people is begin the detail behavior
{
Database.napping();

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.

A network hub is a fairly unsophisticated broadcast device. Hubs do not


manage any of the traffic that comes through them, and any packet entering any
port is broadcast out on all other ports. Since every packet is being sent out
through all other ports, packet collisions result—which greatly impedes the
smooth flow of traffic.

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

More technically, a router is a networking device whose software and


hardware are customized to the tasks of routing and forwarding information. A
router differs from an ordinary computer in that it needs special hardware, called
interface cards, to connect to remote devices through either copper cables or
Optical fiber cable. These interface cards are in fact small computers that are
specialized to convert electric signals from one form to another, with embedded
CPU or ASIC, or both. In the case of optical fiber, the interface cards (also called
ports) convert between optical signals and electrical signals.

Routers connect two or more logical subnets, which do not share a


common network address. The subnets in the router do not necessarily map one-
to-one to the physical interfaces of the router. switching.

SWITCH:

A switch is an electrical component that can break an electrical circuit,


interrupting the current or diverting it from one conductor to another.

A switch may be directly manipulated by a human as a control signal to a


system, such as a computer keyboard button, or to control power flow in a circuit,
such as a light switch. Automatically-operated switches can be used to control the
motions of machines,.

For example, a thermostat is an automatically-operated switch used to


control a heating process. A switch that is operated by another electrical circuit is
called a relay. Large switches may be remotely operated by a motor drive
mechanism. Some switches are used to isolate electric power from a system,
providing a visible point of isolation that can be pad-locked if necessary to
prevent accidental operation of a machine during maintenance, or to prevent
electric shock.

15
EXPLORING LOCAL AREA NETWORK CONFIGURATION OPTIONS:
Step:1double click network connections

Step:2 right on local area connections.->properties

STEP:3 CLICK CONFIGURE

16
SHARING RESOURCES:

STEP:1 Right click on any of the local drive -> sharing and security

17
VERIFYING TCP/IP SETTINGS:

STEP : 1 Double click on the network connections->right click on properties

->select internet protocol(TCP/IP) -->click properties

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”

 To implement the interface, create the implementation program


“LotteryImpl.java”

 Compile the interface and its implementation.

 To do this, type, rmic LotteryImpl


 It should produce following 4 new files:
 LotteryImpl.class
 Lottery.class
 Lottery_Stub.class
 Lottery_Skel.class
 Create a RMI server, LotteryServer.java

 Create a RMI client, LotteryClient.java

 Type javac LotteryServer.java and javac LotteryClient.java to compile the client


and server programs.
 start rmi registry by typing start rmiregistry

 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

public interface Lottery extends java.rmi.Remote


{
public void generate(long n) throws java.rmi.RemoteException;
}

LotteryImpl.java

import java.util.*;

public class LotteryImpl extends java.rmi.server.UnicastRemoteObject implements


lottery
{
Random rand=new Random();
int num,i;

public LotteryImpl() throws java.rmi.RemoteException


{
super();
}

public void generate(long n) throws java.rmi.RemoteException


{
for(i=0;i<n;i++)
{
num=rand.nextInt(10000000);
System.out.println("Lottery "+(i+1)+" : "+num);
}
}
}

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;

public class LotteryClient


{
public static void main(String[] args)
{
int num1 = Integer.parseInt(args[0]);
try
{

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

Das könnte Ihnen auch gefallen