Sie sind auf Seite 1von 57

S.

No Particulars Date of Date of Teacher’s


Practical Submission Signature &
Remarks
1 Write a program to implement
FCFS CPU scheduling algorithm
2 Write a program to implement
SJF CPU scheduling algorithm
3 Write a program to implement
Priority CPU scheduling
algorithm
4 Write a program to implement
Round Robbin CPU scheduling
algorithm
5 Write a program to compare
various CPU scheduling
algorithms over different
scheduling criteria
6 Write a program to implement
classical inter process
communication
problem(Producer Consumer)
7 Write a program to implement
classical inter process
communication
problem(Reader Writer)
8 Write a program to implement
classical inter process
communication
problem(Dining-Philosophers)
9 Write a program to implement
various page replacement
algorithm
10 Write a program to implement
various disk and drum
scheduling algorithm
11 Write a program to implement
Banker’s algorithm
12 Write a program to implement
remote procedure call (RPC)
13 Write a Device Drivers for any
device and peripherals
1. A Program to implement FCFS CPU
scheduling algorithm
#include<stdio.h>

int main()
{
int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;
printf("Enter total number of processes(maximum 20):");
scanf("%d",&n);

printf("\nEnter Process Burst Time\n");


for(i=0;i<n;i++)
{
printf("P[%d]:",i+1);
scanf("%d",&bt[i]);
}

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);

return 0;
}
Output
2. A Program to implement SJF CPU
scheduling algorithm

#include<stdio.h>

void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);

printf("\nEnter Burst Time:\n");


for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero


//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
Output
3. A Program to implement Priority CPU
Scheduling algorithm

#include<stdio.h>

int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n);

printf("\nEnter Burst Time and Priority\n");


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}

for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero

for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);

return 0;
}
Output
4. A Program to implement Round
Robin CPU scheduling algorithm
#include<stdio.h>

int main()
{

int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process Process Number %d
:",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-
bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}
Output
5. Program to compare various CPU Scheduling algorithms over different
criteria
#include<stdio.h>

int n, bt[20], at[20], pr[20];

void FCFS(){

int wt[20],tat[20],avwt=0,avtat=0,i,j;

wt[0]=0; //waiting time for first process is 0

//calculating waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}

printf("\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time");

//calculating turnaround time


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d",i+1,bt[i],wt[i],tat[i]);
}

avwt/=i;
avtat/=i;
printf("\n\nAverage Waiting Time:%d",avwt);
printf("\nAverage Turnaround Time:%d",avtat);
}

void SJF(){
int p[20],wt[20],tat[20],i,j,total=0,pos,temp;
float avg_wt,avg_tat;
for(i=0;i<n;i++)
{
p[i]=i+1; //contains process number
}

//sorting burst time in ascending order using selection sort


for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=(float)total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=(float)total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

void Priority(){
int p[20], wt[20],tat[20],i,j,total=0,pos,temp,avg_wt,avg_tat;
for(i=0;i<n;i++)
{
p[i]=i+1; //contains process number
}

//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}

temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero

//calculate waiting time


for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];

total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");


for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}

avg_tat=total/n; //average turnaround time


printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
}

void RoundRobin(){
int count,j,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,rt[10];
remain=n;
for(count=0;count<n;count++)
{
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);


printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
}

int main()
{

printf("Enter Total Process:\t ");


scanf("%d",&n);

for(int count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time, and priority for process number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
scanf("%d",&pr[count]);
}

printf("\n%s\n", "------------Using FCFS----------");


FCFS();

printf("\n%s\n", "------------Using SJF----------");


SJF();

printf("\n%s\n", "-------------Using Priority------");


Priority();

printf("\n%s\n", "------------Using Round Robin---------");


RoundRobin();
int test;
scanf("%d", &test);

return 0;
}
OUPUT: Compare Scheduling algorithms
6. Write a program to implement classical inter process
communication problem (producer consumer)
#include<stdio.h>
#include<stdlib.h>

int mutex=1,full=0,empty=3,x=0;

int main()
{
int n;
void producer();
void consumer();
int wait(int);
int signal(int);
printf("\n1.Producer\n2.Consumer\n3.Exit");
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!");
break;
case 3:
exit(0);
break;
}
}

return 0;
}

int wait(int s)
{
return (--s);
}

int signal(int s)
{
return(++s);
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nProducer produces the item %d",x);
mutex=signal(mutex);
}

void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",x);
x--;
mutex=signal(mutex);
}
7. A program to implement classical inter process communication
problem (Reader Writers)

#include<stdio.h>
#include<pthread.h>
#include<semaphore.h>

sem_t mutex,writeblock;
int data = 0,rcount = 0;

void *reader(void *arg)


{
int f;
f = ((int)arg);
sem_wait(&mutex);
rcount = rcount + 1;
if(rcount==1)
sem_wait(&writeblock);
sem_post(&mutex);
printf("Data read by the reader%d is %d\n",f,data);
sleep(1);
sem_wait(&mutex);
rcount = rcount - 1;
if(rcount==0)
sem_post(&writeblock);
sem_post(&mutex);
}

void *writer(void *arg)


{
int f;
f = ((int) arg);
sem_wait(&writeblock);
data++;
printf("Data writen by the writer%d is %d\n",f,data);
sleep(1);
sem_post(&writeblock);
}

main()
{
int i,b;
pthread_t rtid[5],wtid[5];
sem_init(&mutex,0,1);
sem_init(&writeblock,0,1);
for(i=0;i<=2;i++)
{
pthread_create(&wtid[i],NULL,writer,(void *)i);
pthread_create(&rtid[i],NULL,reader,(void *)i);
}
for(i=0;i<=2;i++)
{
pthread_join(wtid[i],NULL);
pthread_join(rtid[i],NULL);
}
}
8. Program to implement inter-process communication problem(Dining
Philosophers)

#include<stdio.h>

#define n 4

int compltedPhilo = 0,i;

struct fork{

int taken;

}ForkAvil[n];

struct philosp{

int left;

int right;

}Philostatus[n];

void goForDinner(int philID){ //same like threads concept here cases implemented

if(Philostatus[philID].left==10 && Philostatus[philID].right==10)

printf("Philosopher %d completed his dinner\n",philID+1);

//if already completed dinner

else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){

//if just taken two forks

printf("Philosopher %d completed his dinner\n",philID+1);

Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner


by assigning value 10

int otherFork = philID-1;

if(otherFork== -1)

otherFork=(n-1);

ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks


printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);

compltedPhilo++;

else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for


right fork

if(philID==(n-1)){

if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER


TRYING IN reverse DIRECTION

ForkAvil[philID].taken = Philostatus[philID].right = 1;

printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);

}else{

printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);

}else{ //except last philosopher case

int dupphilID = philID;

philID-=1;

if(philID== -1)

philID=(n-1);

if(ForkAvil[philID].taken == 0){

ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;

printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);

}else{

printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);

else if(Philostatus[philID].left==0){ //nothing taken yet

if(philID==(n-1)){

if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER


TRYING IN reverse DIRECTION
ForkAvil[philID-1].taken = Philostatus[philID].left = 1;

printf("Fork %d taken by philosopher %d\n",philID,philID+1);

}else{

printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);

}else{ //except last philosopher case

if(ForkAvil[philID].taken == 0){

ForkAvil[philID].taken = Philostatus[philID].left = 1;

printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);

}else{

printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);

}else{}

int main(){

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

ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;

while(compltedPhilo<n){

/* Observe here carefully, while loop will run until all philosophers complete dinner

Actually problem of deadlock occur only thy try to take at same time

This for loop will say that they are trying at same time. And remaining status will print by go for
dinner function

*/

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

goForDinner(i);

printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);

return 0;

}
OUPUT: Dining Philosophers Problem
9. Program to compare various page replacement algorithms
#include<stdio.h>

void FIFO();

void LRU();

void OPTIMAL();

int main()

int ch;

do

printf("\n\n\t1.FIFO\n\t2.LRU\n\t3.Optimal\n\t4.Exit\n\tEnter Choice : ");

scanf("%d",&ch);

switch(ch)

case 1:

FIFO();

break;

case 2:

LRU();

break;

case 3:

OPTIMAL();

break;

}while(ch!=4);

void FIFO()

int frame[3]={-1,-1,-1},ref[20],cnt=0,i,j,no,flag;

float ratio,hitcnt=0.00;
printf("\n\tEnter length of reference string : ");

scanf("%d",&no);

printf("\n\tEnter reference String with giving space ....\n\t");

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

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

//printf("\n\tExecution is started here .....");

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

flag=0;

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

if(frame[j]==ref[i])

printf("\n\tPage Hit ");

hitcnt++;

flag=1;

break;

if(flag==0)

printf("\n\tPage Miss");

printf("\tBefore :\t");

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

printf(" %d",frame[j]);

frame[cnt]=ref[i];

cnt++;

if(cnt>=3)

cnt=0;

printf("\tAfter :\t");

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

printf(" %d",frame[j]);
}

ratio=hitcnt/no;

printf("\n\n\tHit ratio = %f ",ratio);

void LRU()

int frame[3]={-1,-1,-1},used[3]={-1,-1,-1},cnt=0,ref[20],i,j,flag,no,index,value;

float ratio,hitcnt=0;

printf("\n\tEnter length of reference string : ");

scanf("%d",&no);

printf("\n\tEnter reference String with giving space \n\t");

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

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

//printf("\n\tExecution is started here ");

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

flag=0;

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

if(frame[j]==ref[i])

printf("\n\tPage Hit ");

hitcnt++;

flag=1;

used[j]=cnt;

break;

if(flag==0)

printf("\n\tPage Miss");

printf("\tBefore :");
for(j=0;j<3;j++)

printf(" %d",frame[j]);

//selection of victim for replacement

index=0;

value=used[0];

if(cnt!=0) {

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

if(value>used[j]&&value!=used[j])

index=j;

value=used[j];

//printf("\tVictim is %d ",index);

frame[index]=ref[i];

used[index]=cnt;

printf("\tAfter :");

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

printf(" %d",frame[j]);

cnt++;

ratio=hitcnt/no;

printf("\n\n\tHit ratio = %f ",ratio);

void OPTIMAL()

int frame[3]={-1,-1,-1},used[3]={-1,-1,-1},cnt=0,ref[20],i,j,flag,no,val1,val2,val3,index;

float ratio,hitcnt=0;

printf("\n\tEnter length of reference string : ");

scanf("%d",&no);
printf("\n\tEnter reference String with giving space \n\t");

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

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

//printf("\n\tExecution is started here ");

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

flag=0;

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

if(frame[j]==ref[i])

flag=1;

printf("\n\tPage Hit");

hitcnt++;

break;

if(flag==0)

printf("\n\tPage Miss");

if(cnt<3)

frame[cnt]=ref[i];

printf("\tStatus :");

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

printf(" %d",frame[j]);

cnt++;

else

printf("\tBefore :");

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

printf(" %d",frame[j]);
//selection of victim

val1=frame[0];

flag=0;

for(j=i;j<no;j++)

if(ref[j]==val1)

val1=j;

flag=1;

break;

if(flag==0)

val1=no;

val2=frame[1];

flag=0;

for(j=i;j<no;j++)

if(ref[j]==val2)

val2=j;

flag=1;

break;

if(flag==0)

val2=no;

val3=frame[2];

flag=0;

for(j=i;j<no;j++)

if(ref[j]==val3)

val3=j;

flag=1;

break;
}

if(flag==0)

val3=no;

if(val1<val2)

if(val3<val2)

index=1;

else

index=2;

else

if(val3<val1)

index=0;

else

index=2;

frame[index]=ref[i];

printf("\tAfter :");

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

printf(" %d",frame[j]);

ratio=hitcnt/no;

printf("\n\n\tHit ratio = %f ",ratio);

}
OUPUT: Page Replacement Algorithms
10. Program to implement and compare various Disk and Drum
Scheduling algorithms
#include <iostream>

#include <cstdlib>

#include <conio.h>

#include <math.h>

using namespace std;

int compare (const void * a, const void * b)

if ( *(int*)a < *(int*)b ) return -1;

if ( *(int*)a == *(int*)b ) return 0;

if ( *(int*)a > *(int*)b ) return 1;

void shift(int a[],const int size)

for(int i=1;i<=size;i++)

a[i-1]=a[i];

class disk

private :

int request[101];

int sorted_request[101];

int number_of_request;

int max;

int direction;

public :

disk()

{
cout<<"\nEnter maximum disk limit : ";

cin>>max;

void receive_request()

enter_request_number:

cout<<"\nEnter number of requests (max. 100): ";

cin>>number_of_request;

if(number_of_request>100)

goto enter_request_number;

current_location:

cout<<"\nEnter current location : ";

cin>>request[0];

sorted_request[0]=request[0];

if(request[0]>max||request[0]<0)

goto current_location;

current_direction:

cout<<"\nEnter current direction(0:left / 1:right) : ";

cin>>direction;

if(direction!=0&&direction!=1)

goto current_direction;

for(int i=1;i<=number_of_request;i++)

cout<<"\nEnter request number "<<i<<" : ";

cin>>request[i];

sorted_request[i]=request[i];
if(request[i]>max||request[i]<0)

cout<<"\nInvalid request !! Enter again!!";

i--;

qsort(sorted_request+1,number_of_request,sizeof(int),compare);

int fcfs()

int head_movement=0;

for(int i=1;i<=number_of_request;i++)

head_movement+=abs(request[i]-request[i-1]);

return head_movement;

int sstf()

int head_movement=0,flag=0,nor=number_of_request;

int request[101];

request[0]=sorted_request[0];

for(int i=1;i<=number_of_request;i++)

if(sorted_request[i]>sorted_request[0]&&flag==0)

flag=i;

request[i]=sorted_request[i];

while(nor)

if(flag==0)
{

head_movement+=request[0]-request[nor];

request[0]=request[nor];

else if(flag==1)

head_movement+=abs(request[nor]-request[0]);

break;

else if((request[flag]-request[0])>(request[0]-request[flag-1]))

head_movement+=request[0]-request[flag-1];

request[0]=request[flag-1];

flag--;

shift(request+flag,nor-flag);

else

head_movement+=request[flag]-request[0];

request[0]=request[flag];

shift(request+flag,nor-flag);

nor--;

return head_movement;

int SCAN()

int head_movement=0,flag=0;

for(int i=1;i<=number_of_request;i++)
if(sorted_request[i]>sorted_request[0]&&flag==0)

flag=i;

if(direction==1)

if(flag==1)

head_movement+=sorted_request[number_of_request]-sorted_request[0];

else

head_movement+=max-sorted_request[0];

head_movement+=max-sorted_request[1];

else

if(flag==0)

head_movement+=abs(sorted_request[number_of_request]-sorted_request[0]);

else

head_movement+=sorted_request[0];

head_movement+=sorted_request[number_of_request];

return head_movement;

int CSCAN()

int head_movement=0,flag=0;
for(int i=1;i<=number_of_request;i++)

if(sorted_request[i]>sorted_request[0]&&flag==0)

flag=i;

if(flag==1)

head_movement+=sorted_request[number_of_request]-sorted_request[0];

else

head_movement+=max-sorted_request[0];

head_movement+=max;

head_movement+=max-sorted_request[flag-1];

return head_movement;

int LOOK()

int head_movement=0,flag=0;

for(int i=1;i<=number_of_request;i++)

if(sorted_request[i]>sorted_request[0]&&flag==0)

flag=i;

if(direction==1)

if(flag==1)

head_movement+=sorted_request[number_of_request]-sorted_request[0];

else
{

head_movement+=sorted_request[number_of_request]-sorted_request[0];

head_movement+=sorted_request[number_of_request]-sorted_request[1];

else

if(flag==0)

head_movement+=abs(sorted_request[number_of_request]-sorted_request[0]);

else

head_movement+=sorted_request[1];

head_movement+=sorted_request[number_of_request]-sorted_request[1];

return head_movement;

int CLOOK()

int head_movement=0,flag=0;

for(int i=1;i<=number_of_request;i++)

if(sorted_request[i]>sorted_request[0]&&flag==0)

flag=i;

if(flag==1)

head_movement+=sorted_request[number_of_request]-sorted_request[0];

else

head_movement+=sorted_request[number_of_request]-sorted_request[0];
head_movement+=sorted_request[number_of_request]-sorted_request[1];

head_movement+=sorted_request[flag-1]-sorted_request[1];

return head_movement;

~disk(){}

};

int main()

disk hdd;

hdd.receive_request();

cout<<"Total head movement in ";

cout<<"FCFS is "<<hdd.fcfs()<<endl;

cout<<"Total head movement in ";

cout<<"SSTF is "<<hdd.sstf()<<endl;

cout<<"Total head movement in ";

cout<<"SCAN is "<<hdd.SCAN()<<endl;

cout<<"Total head movement in ";

cout<<"CSCAN is "<<hdd.CSCAN()<<endl;

cout<<"Total head movement in ";

cout<<"LOOK is "<<hdd.LOOK()<<endl;

cout<<"Total head movement in ";

cout<<"CLOOK is "<<hdd.CLOOK()<<endl;

int k;

cin.ignore(255);

cin >> k;

return 0;

}
OUPUT: Disk and Drum Scheduling algorithms
11.Program to implement Banker’s algorithm
#include <stdio.h>
int main()
{
int count = 0, m, n, process, temp, resource;
int allocation_table[5] = {0, 0, 0, 0, 0};
int available[5], current[5][5], maximum_claim[5][5];
int maximum_resources[5], running[5], safe_state = 0;
printf("\nEnter The Total Number Of Processes:\t");
scanf("%d", &process);
for(m = 0; m < process; m++)
{
running[m] = 1;
count++;
}
printf("\nEnter The Total Number Of Resources To Allocate:\t");
scanf("%d", &resource);
printf("\nEnter The Claim Vector:\t");
for(m = 0; m < resource; m++)
{
scanf("%d", &maximum_resources[m]);
}
printf("\nEnter Allocated Resource Table:\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
scanf("%d", &current[m][n]);
}
}
printf("\nEnter The Maximum Claim Table:\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
scanf("%d", &maximum_claim[m][n]);
}
}
printf("\nThe Claim Vector \n");
for(m = 0; m < resource; m++)
{
printf("\t%d ", maximum_resources[m]);
}
printf("\n The Allocated Resource Table\n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
printf("\t%d", current[m][n]);
}
printf("\n");
}
printf("\nThe Maximum Claim Table \n");
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
printf("\t%d", maximum_claim[m][n]);
}
printf("\n");
}
for(m = 0; m < process; m++)
{
for(n = 0; n < resource; n++)
{
allocation_table[n] = allocation_table[n] + current[m][n];
}
}
printf("\nAllocated Resources \n");
for(m = 0; m < resource; m++)
{
printf("\t%d", allocation_table[m]);
}
for(m = 0; m < resource; m++)
{
available[m] = maximum_resources[m] - allocation_table[m];
}
printf("\nAvailable Resources:");
for(m = 0; m < resource; m++)
{
printf("\t%d", available[m]);
}
printf("\n");
while(count != 0)
{
safe_state = 0;
for(m = 0; m < process; m++)
{
if(running[m])
{
temp = 1;
for(n = 0; n < resource; n++)
{
if(maximum_claim[m][n] - current[m][n] > available[n])
{
temp = 0;
break;
}
}
if(temp)
{
printf("\nProcess %d Is In Execution \n", m + 1);
running[m] = 0;
count--;
safe_state = 1;
for(n = 0; n < resource; n++)
{
available[n] = available[n] + current[m][n];
}
break;
}
}
}
if(!safe_state)
{
printf("\nThe Processes Are In An Unsafe State \n");
break;
}
else
{
printf("\nThe Process Is In A Safe State \n");
printf("\nAvailable Vector\n");
for(m = 0; m < resource; m++)
{
printf("\t%d", available[m]);
}
printf("\n");
}
}
return 0;
}
OUPUT: Banker’s algorithm
12.Program to implement Remote Procedure Call(RPC)

#include"rpc/rpc.h"

#include"square.h"

#include"stdio.h"

#include"stdlib.h"

#include"math.h"

square_out *squareproc_1_svc(square_in *inp,struct svc_req *rqstp)

static square_out out;

out.res1= inp->arg1 * inp->arg1;

return(&out);

// CLIENT FILENAME: client.c

#include"errno.h"

#include"rpc/rpc.h"

#include"square.h"

#include"stdio.h"

#include"stdlib.h"

#include"math.h"

int main(int argc,char **argv)

CLIENT *cl;

square_in in;

square_out *outp;

f(argc!=3)

printf("\n\n error:insufficient arguments!!!");


exit(-1);

cl=clnt_create(argv[1],SQUARE_PROG,SQUARE_VERS,"tcp");

in.arg1=atol(argv[2]);

if(cl==NULL)

printf("\nerror:%s",strerror(errno));

exit(-1);

if((outp=squareproc_1(&in,cl))==NULL)

printf("\nerror :%s",clnt_sperror(cl,argv[1]));

exit(-1);

printf("\n\n result is : %ld",outp->res1);

exit(0);

// .h FILENAME: square.h

struct square_in

/*input arg*/

long arg1;

};

struct square_out
{

/*op result*/

long res1;

};

program SQUARE_PROG

version SQUARE_VERS

square_out SQUAREPROC(square_in)=1; /*proc no=1*/

}=1; /*version no*/

}=0x31230000;/*prog no*/
OUPUT: Remote Procedure Call
13.Program to implement a driver for a device

#include <wdm.h>

#include "example.h"

#define _X86_

#pragma alloc_text(INIT, DriverEntry)

#pragma alloc_text(PAGE, Example_Unload)

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath)

NTSTATUS NtStatus = STATUS_SUCCESS;

UINT uiIndex = 0;

PDEVICE_OBJECT pDeviceObject = NULL;

UNICODE_STRING usDriverName, usDosDeviceName;

DbgPrint("DriverEntry Called \r\n");

RtlInitUnicodeString(&usDriverName, L"\\Device\\Example");

RtlInitUnicodeString(&usDosDeviceName, L"\\DosDevices\\Example");

NtStatus = IoCreateDevice(pDriverObject, 0, &usDriverName, FILE_DEVICE_UNKNOWN,


FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);

if(NtStatus == STATUS_SUCCESS)

for(uiIndex = 0; uiIndex < IRP_MJ_MAXIMUM_FUNCTION; uiIndex++)

pDriverObject->MajorFunction[uiIndex] = Example_UnSupportedFunction;

pDriverObject->MajorFunction[IRP_MJ_CLOSE] = Example_Close;

pDriverObject->MajorFunction[IRP_MJ_CREATE] = Example_Create;

pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = Example_IoControl;
pDriverObject->MajorFunction[IRP_MJ_READ] = Example_Read;

pDriverObject->MajorFunction[IRP_MJ_WRITE] = USE_WRITE_FUNCTION;

pDriverObject->DriverUnload = Example_Unload;

pDeviceObject->Flags |= IO_TYPE;

pDeviceObject->Flags &= (~DO_DEVICE_INITIALIZING);

IoCreateSymbolicLink(&usDosDeviceName, &usDriverName);

return NtStatus;

VOID Example_Unload(PDRIVER_OBJECT DriverObject)

UNICODE_STRING usDosDeviceName;

DbgPrint("Example_Unload Called \r\n");

RtlInitUnicodeString(&usDosDeviceName, L"\\DosDevices\\Example");

IoDeleteSymbolicLink(&usDosDeviceName);

IoDeleteDevice(DriverObject->DeviceObject);

Das könnte Ihnen auch gefallen