0% found this document useful (0 votes)
17 views46 pages

Lab Manual Operating System

Lab manual prepare for operating system

Uploaded by

smita31070
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views46 pages

Lab Manual Operating System

Lab manual prepare for operating system

Uploaded by

smita31070
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 46

LAB MANUAL

OPERATING SYSTEM

DEPARTMENT
OF
INFORMATION TECHNOLOGY
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE
VILLAGE LIMBODAGARI, SAWER ROAD INDORE
JUNE – 2015
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE
INFORMATION TECHNOLOGY
OPERATING SYSTEM

EXPERIMENT PAGE
OBJCTIVE OF EXPERIMENTS
NO. NO.

1. Program to implement FCFS CPU scheduling algorithm. 1


2. .Program to implement SJF CPU scheduling algorithm. 4
3. Program to implement Priority CPU Scheduling algorithm. 8
4. Program to implement Round Robin CPU scheduling algorithm. 12
16
Program to implement classical inter process communication
5.
problem (producer consumer).
19
Program to implement classical inter process communication
6.
problem (Reader Writers).
22
Program to implement classical inter process communication
7.
problem (Dining Philosophers).
26
Write a program to implement & Compare various page
8. replacement algorithms.

9. Write a program to implement Banker’s algorithms. 39


10. Write a program to implement Remote Procedure Call (RPC). 42
MALWA INSTITUTE OF SCIENCE AND TECHNOLOGY, INDORE

Experiment No: 01

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement FCFS CPU scheduling algorithm.

SOFTWARE : Turbo C++

THEORY:

First-come, first-served (FCFS) scheduling algorithm is the simplest algorithm. With this
algorithm, processes are assigned the CPU in the order they request it. Basically, there is a single
queue of ready processes. Relative importance of jobs measured only by arrival time (poor
choice). The implementation of the FCFS policy is easily managed with a FIFO queue. When a
process enters the ready queue, its PCB is linked onto the tail of the queue.

PROCEDURE:

Program
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char pn[10][10],t[10];
int arr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
int totwt=0,tottat=0;
//clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
1
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
}
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nPName Arrtime Burtime WaitTime Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage Waiting time:%f",(float)totwt/n);
printf("\nAverage Turn Around Time:%f",(float)tottat/n);
getch();
return 0;
}

2
RESULT

Enter the number of processes: 3


Enter the Process Name, Arrival Time & Burst Time: p1 2 4
Enter the Process Name, Arrival Time & Burst Time: p2 3 5
Enter the Process Name, Arrival Time & Burst Time: p3 1 6
PName Arrtime Burtime WaitTime Strart TAT Finish
p3 1 6 0 1 6 7
p1 2 4 5 7 9 11
p2 3 5 8 11 13 16
Average Waiting Time: 4.3333333
Average Turn Around Time: 9.33333333

QUESTIONS:-

Q.1What is CPU scheduling?

Q.2What is turnaround time?

Q.3What is waiting time?

3
Experiment No: 02

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement SJF CPU scheduling algorithm

SOFTWARE : Turbo C++

THEORY

A different approach to CPU scheduling is the shortest-job-first (SJF) scheduling algorithm. This
algorithm associates with each process thelength of the process's next CPU burst.When the CPU
is available, it is assigned to the process that has the smallest next CPU burst. If the next CPU
bursts of two processes are the same, FCFS scheduling is used.The SJF scheduling algorithm
gives the minimum average waiting time for a given set of processes.Moving a short process
before a long one decreases the waiting time of the short process more than it increases the
waiting time of the long process.The real difficulty with the SJF algorithm is knowing the length
of the next CPU request. For long-term (job) scheduling in a batch system, we can use as the
length the process time limit that a user specifies when he submits the job.Although the SJF
algorithm is optimal

PROCEDURE:

Program

#include<stdio.h>

#include<process.h>

void main()

char p[10][5],temp[5];

int et[5];

int tot=0,wt[10],pt[10],i,j,n,temp1;

4
float avg=0;

clrscr();

printf("enter no of processes:");

scanf("%d",&n);

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

printf("enter process%d name:\n",i+1);

scanf("%s",&p[i]);

printf("enter process time");

scanf("%d",&pt[i]);

for(i=0;i<n-1;i++)

for(j=i+1;j<n;j++)

if(pt[i]>pt[j])

temp1=pt[i];

pt[i]=pt[j];

pt[j]=temp1;

strcpy(temp,p[i]);

strcpy(p[i],p[j]);strcpy(p[j],temp);

5
wt[0]=0;

for(i=1;i<n;i++)

wt[i]=wt[i-1]+et[i-1];

tot=tot+wt[i];

avg=(float)tot/n;

printf("p_name\t P_time\t w_time\n");

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

printf("%s\t%d\t%d\n",p[i],et[i],wt[i]);

printf("total waiting time=%d\n avg waiting time=%f",tot,avg);

} getch()

6
RESULT

no of processes: 5
enter process1 name: aaa
enter process time: 4
enter process2 name: bbb
enter process time: 3
enter process3 name: ccc
enter process time: 2
enter process4 name: ddd
enter process time: 5
enter process5 name: eee
enter process time: 1
p_name P_time w_time
eee 1 0
ccc 2 1
bbb 3 3
aaa 4 6
ddd 5 10
total waiting time=20
avg waiting time=4.0

QUESTIONS:-

Q.1What is the use of CPU scheduling?

Q.2What is burst time?

Q.3What is SJF CPU scheduling?

7
Experiment No: 03

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement Priority CPU Scheduling algorithm.

SOFTWARE : Turbo C++

THEORY: A priority is associated with each process, and the CPU is allocated to the process
with the highest priority. Equal-priority process are scheduled in FCFS scheduling.

Priorities are generally some fixed range of numbers such as 0 to 7.Some system represents
low number to represent low priority , others use low numbers to represent higher priority. Here
we use lpw numbers to represent high priority.

Every process gets assigned a priority. The process with the highest priority that requests cpu
time gets it always. Interactive processes could get assigned a high priority, so they get a high
response time. Processes that do computing etc get a lower priority. This algorithm is an
approximation of SJF, because interactive processes will be shorted (eg. they read a key entered
and then block again) and are given a short response time

PROCEDURE

Program:

#include<iostream.h>
#include<conio.h>
class process
{
public :
int w_time,ta_time,b_time,priority,pid;
}*p;
void sort(int n)
{
process tmp;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(p[i].priority<p[j].priority)
{

8
tmp=p[i];
p[i]=p[j];
p[j]=tmp;
}
}
}
}
void main()
{
clrscr();
int n,av_wtime=0,av_tatime=0;
cout<<"enter no processes \n";
cin>>n;p=new process[n];
for(int i=0; i<n; i++)
{
cout<<"Enter burst time for process "<<i+1<<"\n";
cin>>p[i].b_time;
cout<<"Enter priority of process "<<i+1<<"\n";
cin>>p[i].priority;
p[i].pid=i+1;
}
sort(n);
p[0].ta_time=p[0].b_time;
for(i=0; i<n; i++)
{
for(int j=0; j<n; j++)
p[i].w_time+=p[j].b_time;
p[i].ta_time=p[i].w_time+p[i].b_time;
av_wtime+=p[i].w_time;
av_tatime+=p[i].ta_time;
}
av_wtime=av_wtime/n;
av_tatime=av_tatime/n;
clrscr();
cout<<"**SJF CPU sheduling**\n";
gotoxy(1,5);
cout<<"P_no";
gotoxy(14,5);
cout<<"priority";
gotoxy(30,5);
cout<<"burst time";
gotoxy(42,5);
cout<<"waiting time";
gotoxy(57,5);
cout<<"turn around time";

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


{
gotoxy(1,6+i);
cout<<p[i].pid;
9
gotoxy(14,6+i);
cout<<p[i].priority;
gotoxy(30,6+i);
cout<<p[i].b_time;
gotoxy(42,6+i);
cout<<p[i].w_time;
gotoxy(57,6+i);
cout<<p[i].ta_time;
}
cout<<"\n\n\n";
cout<<"Average waiting time "<<av_wtime;
cout<<"average turn around time "<<av_tatime;
getch();
}

RESULT
Enter no of process -- 3
Enter burst time for process P1 -- 10
Enter priority of process P1 -- 30
Enter burst time for process P2 -- 5
Enter priority of process P2 -- 40
Enter burst time for process P3 -- 20
Enter priority of process P3 -- 60
******************************************************************************
*******
************************* SJF CPU
SCHEDULING**************************************
******************************************************************************
*******

P_no priority burst time waiting time turn around time


1 30 10 -14765 114755
2 40 5 -29830 -29825
3 60 20 11952 11972
******************************************************************************
*******
Average waiting time -- 10881
Average turn around time -- 10869

10
QUESTIONS:-
Q.1What is the use of CPU scheduling
Q.2What is burst time?
Q.3What is SJF CPU scheduling?

11
Experiment No: 04

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement Round Robin CPU scheduling algorithm.

SOFTWARE : Turbo C++

THEORY Round-robin scheduling is really the easiest way of scheduling. All process form a
circular array and the scheduler gives control to each process at a time. It is off course very easy
to implement and causes almost no overhead, when compared to all other algorithms. But
response time is very low for the processes that need it. Off course it is not the algorithm we
want, but it can be used eventually. This algorithm is not the best at all for general-purpose OSs,
but it is useful for bath-processing OSs, in which all jobs have the same priority, and in which
response time is of minor or no importance. And this priority leads us to the next way of
scheduling. Each process gets a small unit of CPU time (time quantum), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added to the end of the
ready queue.
If there are n processes in the ready queue and the time quantum is q, then each process gets
1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-
1)q time units.
Performanceq large Þ FIFO
q small Þ q must be large with respect to context switch, otherwise overhead is toohigh

Example of RR with Time Quantum = 4

Process Burst Time

P1 24

P2 3

P3 3

The-Gantt-chart-is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30
Typically, higher average turnaround than SJF, but better response

12
PROCEDURE :

Program

#include<iostream.h>
#include<conio.h>
const n=50;
\void main()
{
clrscr();
int p[n],quan,i,nm,sum=0,r1[n],temp[n];
cout<<"enter the no of process";
cin>>nm;
for(i=1;i<=nm;i++)
{
cout<<"enter the cpu brust time of p"<<i<<"=";
cin>>p[i];
sum=sum+p[i];
temp[i]=p[i];
}
cout<<"enter the quantum";
cin>>quan;
cout<<"\n\n grant chart";
int r=0;
cout<<0;
do
{
for(int j=1;j<=nm;j++)
{
if(p[j]>=quan)

13
{
p[j]=p[j]-quan;
r=r+quan;
for(int k=1;k<=quan;k++)
cout<<"-";
if(k==quan/2)
{
cout<<"p"<<j;
}
}
cout<<r;
sum=sum-quan;
r1[j]=r;
}
Else
if(p[j]<quan &sum!=0)
{
if(p[j]==0)
{
}else{r=r+p[j];for(int k=1;k<=p[j];k++)
{
cout<<"-";
if(k==p[j]/2)
{
cout<<"p"<<j;
}
}
cout<<r;
sum=sum-p[j];

14
p[j]=0;
r1[j]=r;
}
}
}
}while(sum>0);
float temp1=0,avg;
for(i=1;i<=nm;i++)
{
r1[i]=r1[i]-temp[i];
temp1=temp1+r1[i];
}
avg=temp1/nm;
cout<<endl<<"avg wt---->" <<avg;
getch();
}

RESULT
Enter no of process -- 3
Enter CPU burst time of P1 -- 20
Enter CPU burst time of P2 -- 5
Enter CPU burst time of P3 -- 60
Enter the quantum 10
Grant chart
0—P1—10—P2—15—P3-- 25—P1--35—P3—45—P3—55—P3—65—P3—75—P3—85
Average wating time – 16.666666

QUESTION

Q.1What is Round Robin CPU scheduling?

Q.2What is the advantage of Round robin CPU scheduling?


15
Experiment No: 05

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement classical inter process communication

problem(producer consumer).

SOFTWARE : Turbo C++

THEORY :

Inter-process communication (IPC) is the activity of sharing data across multiple and commonly
specialized processes using communication. Typically, applications using IPC are categorized
as clients and servers, where the client requests data and the server responds to client requests.
[1]
Many applications are both clients and servers, as commonly seen in distributed computing.
Methods for achieving IPC are divided into categories which vary based on software
requirements, such as performance and modularity requirements, and system circumstances, such
as network bandwidth and latency.[1]

PROCEDURE :

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//#include<process.h>
#define max 5
int n,produced,consumed,buffer[max];
int result,i;
int producer(int consumed,int result)
{
if((consumed=1)&&(i>=0))
{
result=result+1;
i++;
buffer[i]=result;
produced=1;
printf("Produced bufffer=%d\n",buffer[i]);
consumed=0;
return(produced,result);
}
else
printf("Buffer still to be consumed\n");
16
}
int consumer(int produced,int result)
{
if((produced==1)&&(i>0))
{
consumed=1;
printf("Consumed buffer=%d\n",buffer[i]);
produced=0;
i--;
return(consumed,result);
}
else
printf("Buffer still to be produced");
}
int main()
{
clrscr();
produced=0;
consumed=1;
result=0;
i=0;
while(1)
{
printf("Enter the choice\n");
printf("1.Producer 2.Consumer 3.Exit\n");
scanf("%d",&n);
if(n==3)
exit(0);
else if(n==1)
producer(consumed,buffer[i]);
else if(n==2)
consumer(produced,buffer[i]);
else
exit(0);
}}

RESULT
Enter the choice
1.Producer 2.Consumer 3.Exit
1
Produced Buffer = 1
Enter the choice
1.Producer 2.Consumer 3.Exit
1
Produced Buffer = 2
Enter the choice
1.Producer 2.Consumer 3.Exit
2
Consumed Buffer = 2
Enter the choice
17
1.Producer 2.Consumer 3.Exit
2
Consumed Buffer = 1
Enter the choice
1.Producer 2.Consumer 3.Exit
2
Buffer still to be produced
Enter the choice: 1.Producer 2.Consumer 3.Exit

QUESTIONS:-

1 What do you mean by semaphore?


2 What do you mean by deadlocks?
3. Explain ways to avoid deadlock

18
Experiment No: 06

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement classical inter process communication

problem(Reader Writers).

SOFTWARE : Turbo C++

THEORY: Let's suppose a writer comes in:

Signals mutex 2 and increments writer count to account for the extra writer (itself) Since it is the
only process that can change writer count (as it is holding mutex 2), it can safely test whether it
is the only writer (writer count==1), if true, it signals mutex r to protect readers from coming in
-- other writers (writer count > 1) can enjoy the mutex rbeing signaled already.

The writer then signals mutex w to protect its changes from other (concurrent) writers.

Last writer (write count==1) releases mutex r to let readers perform their tasks.

2) Let's suppose a reader comes in:

Signals mutex 3 to protect the readers' setup logic from other readers; then signals mutex r to
protect from other writers (remember, r is signaled while writers are operating); then signals
mutex 1 to protect read count (from other readers that might be exiting) and if it is the first reader
(reade0r count == 1), signals mutex w to protect from writers (now excludes writers from
performing their operations).

Reading can be done parallel, so no protection is needed from other readers while reading
(remember, mutex w is being held at this point, so no intereference from writers)

Then the last reader resets the write mutex (w) to allow write

19
PROCEDURE

#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define MAXTHREAD 10 /* define # readers */
void access_database(); /* prototypes */
void non_access_database();
void* reader(void*);
void* writer(void*);
sem_t q; /* establish que */
int rc = 0; /* number of processes reading or wanting to */
int wc = 0;
int write_request = 0;
int main()
{
pthread_t readers[MAXTHREAD],writerTh;
int index;
int ids[MAXTHREAD]; /* readers and initialize mutex, q and db-set them to 1 */
sem_init (&q,0,1);
for(index = 0; index < MAXTHREAD; index ++)
{
ids[index]=index+1; /* create readers and error check */
if(pthread_create(&readers[index],0,reader,&ids[index])!=0){
perror("Cannot create reader!");
exit(1);
}
} if(pthread_create(&writerTh,0,writer,0)!=0)
{ perror("Cannot create writer"); /* create writers and error check */
exit(1);
}
pthread_join(writerTh,0);
sem_destroy (&q);
return 0;
}
void* reader(void*arg) /* readers function to read */
{
int index = *(int*)arg;
int can_read;
while(1){
can_read = 1;
sem_wait(&q); if(wc == 0 && write_request == 0) rc++; else
can_read = 0;
sem_post(&q);
if(can_read)
{
access_database();
printf("Thread %d reading\n", index);
20
sleep(index);
sem_wait(&q);
rc--;
sem_post(&q);
}
sched_yield();
}
return 0;
}
;
void* writer(void*arg) /* writer's function to write */
{
int can_write;
while(1){
can_write = 1;
non_access_database();

sem_wait (&q);
if(rc == 0) wc++;
else { can_write = 0; write_request = 1; }
sem_post(&q);
if(can_write) {
access_database();
printf("Writer is now writing...Number of readers: %d\n",rc);
sleep(3);
sem_wait(&q);
wc--;
write_request = 0;
sem_post(&q);
}
sched_yield();
}
return 0;
}
void access_database()
{
}
void non_access_database()
{
}

RESULT:
Writer is now writing...Number of readers: 3

QUESTIONS:-
1. What do you mean by paging
2. What do you mean by segmentation

21
Experiment No: 07

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Program to implement classical inter process communication

problem(Dining Philosophers).

SOFTWARE : Turbo C++

THEORY: llustration of the dining philosophers problemFive silent philosophers sit at a round
table with bowls of spaghetti Forks are placed between each pair of adjacent philosophers. (An
alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).

Each philosopher must alternately think and eat. However, a philosopher can only eat spaghetti
when he has both left and right forks. Each fork can be held by only one philosopher and so a
philosopher can use the fork only if it is not being used by another philosopher. After he finishes
eating, he needs to put down both forks so they become available to others. A philosopher can
take the fork on his right or the one on his left as they become available, but cannot start eating
before getting both of them.

Eating is not limited by the remaining amounts of spaghetti or stomach space; an infinite supply
is assumed.The problem is how to design a discipline of behavior (a concurrent algorithm) such
that each philosopher will not starve; i.e., can forever continue to alternate between eating and
thinking, assuming that any philosopher cannot know when others may want to eat or think.

Program:

#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<dir.h>

char fn2[20];

main()
{
int c;
clrscr();
do{
22
printf("\n\t\tMain Menu\n-------------------------------\n");
printf("1.Copy a File\n2.Move a File\n3.Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
copy_file();
break;

case 2:
move_file();
break;

case 3:
exit(0);
}
}while(c<=3);
getch();
return 0;
}

copy_file()
{
FILE *f1,*f2;
char ch,s[10],fn1[20];
int a;
printf("\nAre u see the privious files(1/0)?");
scanf("%d",&a);
if(a==1)
print_file();
printf("Enter the source file name:");
scanf("%s",&fn1);
printf("Enter the Destination file name:");
scanf("%s",&fn2);
f1=fopen(fn1,"r");
if(f1==NULL)
printf("Can't open the file");
else {
f2=fopen(fn2,"w");
while((ch=getc(f1))!=EOF)
putc(ch,f2);
printf("One File Copied");
fclose(f2);
}
23
fclose(f1);
return 0;
}

move_file()
{
FILE *f1,*f2;
char ch,s[10],fn1[20];
int a;
printf("\nAre u see the privious files(1/0)?");
scanf("%d",&a);
if(a==1)
print_file();
printf("Enter the source file name:");
scanf("%s",&fn1);
printf("Enter the Destination file name:");
scanf("%s",&fn2);

f1=fopen(fn1,"r");
if(f1==NULL)
printf("Can't open the file");
else {
f2=fopen(fn2,"w");
while((ch=getc(f1))!=EOF)
putc(ch,f2);
printf("One File moved");
fclose(f2);
remove(fn1);
}
fclose(f1);
return 0;
}

print_file()
{
struct ffblk ffblk;
int d,p=0;
char ch;
d=findfirst("*.*",&ffblk,0);
while(!d)
{
printf("%s\n",ffblk.ff_name);
d=findnext(&ffblk);
p=p+1;
24
if(p>=20)
{
printf("Press any key to continue");
getchar();
p=0;
}
}
return 0;
}

RESULT:

Philos name Right fork Left fork


-------------------------------------------------------------------
0 0 1
1 1 2
2 2 3
3 3 4
4 4 0
--------------------------------------------------------------------
Enter the Two Eating Philosophers number:1 3
Round 1
--------------------------
Philosophers 1 is eating with rhf=1 and lhf=2.
Philosophers 3 is eating with rhf=3 and lhf=4.
Round 2
------------------------
Philosophers 0 is eating with rhf=0 and lhf=1.
Philosophers 2 is eating with rhf=2 and lhf=3.
Round 4
------------------------
Philosophers 2 is eating with rhf=2 and lhf=3.
Philosophers 4 is eating with rhf=4 and lhf=0.

Philos name Right fork Left fork


-------------------------------------------------------------------
0 0 1
1 1 2
2 2 3
3 3 4
4 4 0
--------------------------------------------------------------------

QUESTIONS:-
1. How starvation can be avoided in Dining Philosophers
25
Experiment No: 08

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Write a program to implement & Compare various page replacement

algorithm.

SOFTWARE : Turbo C++

THEORY

THEORY:

FIFO: The operating system maintains a list of all pages currently in memory, with the page at
the head of the list the oldest one and the page at the tail the most recent arrival. On a page fault,
the page at the head is removed and the new page added to the tail of the list. When applied to
stores, FIFO might remove mustache wax, but it might also remove flour, salt, or butter. When
applied to computers the same problem arises. For this reason, FIFO in its pure form is rarely
used.

LRU: The least recently used (LRU) page replacement algorithm keeps track of page usage over
a short period of time. LRU works on the idea that pages that have been most heavily used in the
past few instructions are most likely to be used heavily in the next few instructions too. While
LRU can provide near-optimal performance in theory (almost as good as Adaptive Replacement
Cache), it is rather expensive to implement in practice. There are a few implementation methods
for this algorithm that try to reduce the cost yet keep as much of the performance as possible.

Optimal Page Replacement: The optimal page algorithm simply says that the page with the
highest label should be removed. If one page will not be used for 8 million instructions and
another page will not be used for 6 million instructions, removing the former pushes the page
fault that will fetch it back as far into the future as possible. Computers, like people, try to put off
unpleasant events for as long as they can.

PROCEDURE:

Program:

#include<stdio.h>
26
int n,pg[30],fr[10];

void fifo();

void optimal();

void lru();

void main()

int i,ch;

printf("\nEnter total number of pages:");

scanf("%d",&n);

printf("\nEnter sequence:");

for(i=0;i<n;i++) //accepting sequence

scanf("%d",&pg[i]);

do

printf("\n\tMENU\n");

printf("\n1)FIFO");

printf("\n2)OPTIMAL");

printf("\n3)LRU");

printf("\n4)Exit");

printf("\nEnter your choice:");

scanf("%d",&ch);

switch(ch)

case 1: fifo();

break;

case 2: optimal();

27
break;

case 3: lru();

break;

}while(ch!=4);

getchar();

void fifo()

int i,f,r,s,count,flag,num,psize;

f=0;

r=0;

s=0;

flag=0;

count=0;

printf("\nEnter size of page frame:");

scanf("%d",&psize);

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

fr[i]=-1;

while(s<n)

flag=0;

28
num=pg[s];

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

if(num==fr[i])

s++;

flag=1;

break;

if(flag==0)

if(r<psize)

fr[r]=pg[s];

r++;

s++;

count++;

else

if(f<psize)

fr[f]=pg[s];

s++;

f++;

count++;

29
}

else

f=0;

printf("\n");

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

printf("%d\t",fr[i]);

printf("\nPage Faults=%d",count);

getchar();

void optimal()

int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;

fault=0;

dist=0;

k=0;

printf("\nEnter frame size:");

scanf("%d",&f);

//initilizing distance and frame array

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

count[i]=0;

fr[i]=-1;

30
}

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

flag=0;

temp=pg[i];

for(j=0;j<f;j++)

if(temp==fr[j])

flag=1;

break;

if((flag==0)&&(k<f))

fault++;

fr[k]=temp;

k++;

else if((flag==0)&&(k==f))

fault++;

for(cnt=0;cnt<f;cnt++)

current=fr[cnt];

for(c=i;c<n;c++)

31
if(current!=pg[c])

count[cnt]++;

else

break;

max=0;

for(m=0;m<f;m++)

if(count[m]>max)

max=count[m];

p=m;

fr[p]=temp;

printf("\n");

for(x=0;x<f;x++)

printf("%d\t",fr[x]);

printf("\nTotal number of faults=%d",fault);

getchar();

32
void lru()

int count[10],i,j,k,fault,f,flag,temp,current,c,dist,max,m,cnt,p,x;

fault=0;

dist=0;

k=0;

printf("\nEnter frame size:");

scanf("%d",&f);

//initilizing distance and frame array

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

count[i]=0;

fr[i]=-1;

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

flag=0;

temp=pg[i];

for(j=0;j<f;j++)

if(temp==fr[j])

flag=1;

break;

if((flag==0)&&(k<f))

33
{

fault++;

fr[k]=temp;

k++;

else if((flag==0)&&(k==f))

fault++;

for(cnt=0;cnt<f;cnt++)

current=fr[cnt];

for(c=i;c>0;c--)

if(current!=pg[c])

count[cnt]++;

else

break;

max=0;

for(m=0;m<f;m++)

if(count[m]>max)

max=count[m];

p=m;

34
}

fr[p]=temp;

printf("\n");

for(x=0;x<f;x++)

printf("%d\t",fr[x]);

printf("\nTotal number of faults=%d",fault);

getchar();

RESULT

----------Output for PAGE REPLACEMENT ALGORITHM----------

Enter total number of pages:16

Enter sequence:0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7

MENU

1)FIFO

2)OPTIMAL

3)LRU

4)Exit

Enter your choice:1

Enter size of page frame:3

0 -1 -1

0 1 -1

0 1 2

35
3 1 2

3 0 2

3 0 1

3 0 1

2 0 1

2 3 1

2 3 0

2 3 0

1 3 0

1 2 0

1 2 3

1 2 3

4 2 3

4 5 3

4 5 6

4 5 6

7 5 6

Page Faults=16

MENU

1)FIFO

2)OPTIMAL

3)LRU

4)Exit

Enter your choice:2

Enter frame size:3

0 -1 -1

0 1 -1
36
0 1 2

0 1 3

0 1 3

0 1 3

0 2 3

0 2 3

0 2 3

1 2 3

1 2 3

1 2 3

4 2 3

5 2 3

6 2 3

7 2 3

Total number of faults=10

MENU

1)FIFO

2)OPTIMAL

3)LRU

4)Exit

Enter your choice:3

Enter frame size:3

0 -1 -1

0 1 -1

0 1 2

3 1 2

3 0 2
37
1 0 2

1 0 2

1 3 2

0 3 2

1 3 2

1 3 2

1 3 2

4 3 2

4 3 5

6 3 5

6 7 5

Total number of faults=13

QUESTIONS:-

1. How does FIFO algorithm works

2. What are advantages of FIFO algorithm

3. How does LRU page replacement works

38
Experiment No:9

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Write a program to implement Banker’s algorithms

SOFTWARE : Turbo C++

THEORY

Bankers algorithm is used to schedule processes according to the resources they need. It is very
helpful in Deadlock Handling. Bankers algorithm produce a safe sequence as a output if all the
resources can be executed and return error if no safe sequence of the processes available.
When a new process enters a system, it must declare the maximum number of instances of each
resource type that it may ever claim; clearly, that number may not exceed the total number of
resources in the system. Also, when a process gets all its requested resources it must return them
in a finite amount of time.

PROCEDURE:

Program:

#include<conio.h>
void main()
{
int k=0,output[10],d=0,t=0,ins[5],i,avail[5],allocated[10][5],need[10][5],MAX[10]
[5],pno,P[10],j,rz, count=0;
clrscr();
printf("\n Enter the number of resources : ");
scanf("%d", &rz);
printf("\n enter the max instances of each resources\n");
for(i=0;i<rz;i++)
{ avail[i]=0;
printf("%c= ",(i+97));
scanf("%d",&ins[i]);
}
printf("\n Enter the number of processes : ");
39
scanf("%d", &pno);
printf("\n Enter the allocation matrix \n ");

for(i=0;i<rz;i++)
printf(" %c",(i+97));
printf("\n");
for(i=0;i <pno;i++)
{ P[i]=i;
printf("P[%d] ",P[i]);
for(j=0;j<rz;j++)
{
scanf("%d",&allocated[i][j]);
avail[j]+=allocated[i][j];
}
}

printf("\nEnter the MAX matrix \n ");


for(i=0;i<rz;i++)
{ printf(" %c",(i+97));
avail[i]=ins[i]-avail[i];
}
printf("\n");
for(i=0;i <pno;i++)
{
printf("P[%d] ",i);
for(j=0;j<rz;j++)
scanf("%d", &MAX[i][j]);
}

printf("\n");
A: d=-1;
for(i=0;i <pno;i++)
{ count=0; t=P[i];
for(j=0;j<rz;j++)
{
need[t][j] = MAX[t][j]-allocated[t][j];
if(need[t][j]<=avail[j])
count++;
}
if(count==rz)
{
output[k++]=P[i];
for(j=0;j<rz;j++)
avail[j]+=allocated[t][j];
40
}
else
P[++d]=P[i];
}

if(d!=-1)
{ pno=d+1;
goto A;
}
printf("\t <");
for(i=0;i<k;i++)
printf(" P[%d] ",output[i]);
printf(">");
getch();
}

RESULT

QUESTIONS:-

1. What is bankers algorithm and how does it works?

41
Experiment No: 10

Date of conduction: / / 20…..

Date of submission: / /20…....

OBJECTIVE:- Write a program to implement Remote Procedure Call (RPC).

SOFTWARE :- TURBO C++

THEORY:

RPC is a powerful technique for constructing distributed, client-server based applications. It is


based on extending the notion of conventional, or local procedure calling, so that the called
procedure need not exist in the same address space as the calling procedure. The two processes
may be on the same system, or they may be on different systems with a network connecting
them. By using RPC, programmers of distributed applications avoid the details of the interface
with the network. The transport independence of RPC isolates the application from the physical
and logical elements of the data communications mechanism and allows the application to use a
variety of transports.

PROCEDURE:

An RPC is analogous to a function call. Like a function call, when an RPC is made, the calling
arguments are passed to the remote procedure and the caller waits for a response to be returned
from the remote procedure. Figure A shows the flow of activity that takes place during an RPC
call between two networked systems. The client makes a procedure call that sends a request to
the server and waits. The thread is blocked from processing until either a reply is received, or it
times out. When the request arrives, the server calls a dispatch routine that performs the
requested service, and sends the reply to the client. After the RPC call is completed, the client
program continues. RPC specifically supports network applications.

42
PROGRAM:

#include <rpc/rpc.h>
#include <rpcsvc/rusers.h>
#include <stdio.h>

/*
* a program that calls the
* rusers() service
*/

main(int argc,char **argv)

{
int num;
if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n",
argv[0]);
exit(1);
}

if ((num = rnusers(argv[1])) < 0) {


fprintf(stderr, "error: rusers\n");
exit(1);
}

fprintf(stderr, "%d users on %s\n", num, argv[1] );


exit(0);
}

43
Compile the program with:

cc program.c -lrpcsvc -lnsl

The Client Side

There is just one function on the client side of the simplified interface rpc_call().

It has nine parameters:

int
rpc_call (char *host /* Name of server host */,
u_long prognum /* Server program number */,
u_long versnum /* Server version number */,
xdrproc_t inproc /* XDR filter to encode arg */,
char *in /* Pointer to argument */,
xdr_proc_t outproc /* Filter to decode result */,
char *out /* Address to store result */,
char *nettype /* For transport selection */);

QUESTIONS:-

1. What is remote procedure call?

2. What is IDL in remote procedure call?

44

You might also like