OS Experiments New - 1554792526

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

2017

OPERATING SYSTEM
EXPERIMENTS
CHAMELI DEVI GROUP OF INSTITUTION
5 SEMESTER - RGPV
Following Programs are compiled using ‘gcc’ compiler.
Practical: 1

Write a program to implement FCFS CPU scheduling algorithm.

Algorithm:

Step 1: Start the Program.

Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.

Step 3: Read the arrival time (AT).

Step 4: Read the burst time for all process.

Step 5: Calculate the waiting time and turnaround time and display it.

Step 6: Calculate the average waiting time and average turnaround time and display it.

Step 7: Stop the program.

Source Code:

[Step-1]

#include<iostream>

using namespace std;

int main()

{ [Step-2]

int n;

cout<<"Enter no. of Processes: ";

cin>>n;

int p[n],at[n],bt[n],ct[n],tat[n],wt[n]; //dynamic array allocation (less memory wastage)

float avg_wt,avg_tat,total_tat = 0,total_wt = 0;

[Step-3 & Step-4]

cout<<"Enter Arrival Time and Burst Time:\n";

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

cout<<"Process "<<i+1<<": ";

cin>>at[i]>>bt[i];

//calculating completion time required in step-5.


cout<<"Complition Time of processes are:\n";

ct[-1] = 0;

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

ct[i] = bt[i] + ct[i-1];

cout<<"P "<<i+1<<": "<<ct[i]<<endl;

[Step-5]

cout<<"Turnaround Time of processes are:\n";

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

tat[i] = ct[i] - at[i];

cout<<"P "<<i+1<<": "<<tat[i]<<endl;

cout<<"Waiting Time of processes are:\n";

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

wt[i] = tat[i] - bt[i];

cout<<"P "<<i+1<<": "<<wt[i]<<endl;

[Step-6]

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

total_tat = tat[i] + total_tat;

total_wt = wt[i] + total_wt;

avg_wt = total_wt/n;

cout<<"\nAverage Waiting Time is: "<<avg_wt;

avg_tat = total_tat/n;

cout<<"\nAverage Turnaround Time is: "<<avg_tat;

}
OUTPUT:
Practical : 2

Write a program to implement SJF CPU scheduling algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.

Step 3: Read Burst Time for all process.

Step 4: Sort the process according to minimum burst time.

Step 5: Calculate waiting time.

Step 6: Calculate turnaround time and display it.

Step 7: Stop the program.

Source Code:

[Step-1]

#include<iostream>

using namespace std;

int main()

[Step-2]

Int n;

cout<<"Enter number of process:";

cin>>n;

int bt[20],p[20],wt[20],tat[20],total=0,pos,temp;

float avg_wt,avg_tat;

[Step-3]

cout<<"\nEnter Burst Time:\n";

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

cout<<" P"<<i+1<<": ";

cin>>bt[i];

p[i]=i+1; //contains process number


}

[Step-4]

//sorting burst time in ascending order using selection sort (to put shortest burst time first)

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

pos=i;

for(int 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

[Step-5]

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

wt[i]=0;

for(int j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

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

total=0;
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";

[Step-6]

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];

cout<<"\n P"<<p[i]<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];

cout<<"\n\nAverage Waiting Time="<<avg_wt;

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

cout<<"\nAverage Turnaround Time=\n"<<avg_tat;

OUTPUT:
Practical : 3

Write a program to implement Priority CPU scheduling algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.

Step 3: Read Burst Time for all process.

Step 4: Read Priority of all process.

Step 5: Sort the process according to the priority.

Step 6: Calculate waiting time.

Step 7: Calculate turnaround time and display it.

Step 8: Stop the program.

Source Code:

[Step-1]

#include<iostream>

using namespace std;

int main()

{ [Step-2]

int n;

cout<<"Enter Total Number of Process:";

cin>>n;

int bt[n],p[n],wt[n],tat[n],pr[n],i,j,pos,temp;

float avg_wt,avg_tat,total=0;

[Step-3 & Step-4]

cout<<"\nEnter Burst Time and Priority\n";

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

cout<<"\nP["<<i+1<<"]\n";

cout<<"Burst Time:";

cin>>bt[i];
cout<<"Priority:";

cin>>pr[i];

p[i]=i+1; //contains process number

[Step-5]

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

[Step-6]

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;

cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";

[Step-7]

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];

cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];

avg_tat=total/n; //average turnaround time

cout<<"\n\nAverage Waiting Time="<<avg_wt;

cout<<"\nAverage Turnaround Time="<<avg_tat;

return 0;

OUTPUT:
Practical : 4

Write a program to implement Round Robin CPU scheduling algorithm.

Algorithm:

Step 1: Start the program.

Step 2: Input no. of process ‘n’ and declare array of ‘n’ size.

Step 3: Input Time Quantum.

Step 4: Read Arrival Time and Burst Time for all process.

Step 5: Calculate waiting time.

Step 6: Calculate turnaround time and display it.

Step 7: Stop the program.

Source Code:

[Step-1]

#include<iostream>

using namespace std;

int main()

int count,j,n,time,remain,flag=0,tq;

[Step-2]

cout<<"Enter Total Process: ";

cin>>n;

remain=n;

int wait_time=0,turnaround_time=0,at[n],bt[n],rt[n];

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

{ [Step-4]

cout<<"Enter Arrival Time and Burst Time for Process No. "<<count+1<<" :";

cin>>at[count];

cin>>bt[count];

rt[count]=bt[count]; //set (rt) remaining time = burst time

// we make changes in remaining time and burst time remains unchanged.

}
[Step-3]

cout<<"Enter Time Quantum:\t";

cin>>tq;

cout<<"\n\nProcess\t|Turnaround Time|Waiting Time\n\n";

for(time=0,count=0;remain!=0;)

if(rt[count]<=tq && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=tq;

time+=tq;

if(rt[count]==0 && flag==1)

remain--;

cout<<"P["<<count+1<<"]\t|\t"<<time-at[count]<<"\t|\t"<<time-at[count]-
bt[count]<<"\n";

[Step-5 & Step-6]

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;

cout<<"\nAverage Waiting Time: "<<wait_time*1.0/n;

cout<<"Avg Turnaround Time : "<<turnaround_time*1.0/n;

// multiple by ‘1.0’ to convert it into floating point values.

OUTPUT:
Practical : 5
Write a program to compare various CPU Scheduling Algorithms over different Scheduling Criteria.
Algorithm:
Step 1: Start the program.
Step 2: Define various CPU scheduling functions.
Step 3: Input no. of process ‘n’ and declare array of ‘n’ size.
Step 4: Using switch case select CPU scheduling.
Step 5: End program.

Source Code :
[Step-1]
#include<iostream>

using namespace std;

[Step-2]

void fcfs(int n) //refer experiment - 1

void sjf(int n) //refer experiment - 2

void priority(int n) //refer experiment - 3

void round_robin(int n) //refer experiment - 4

int main()

{ [Step-3]

Int n,s;

cout<<”Enter number of process : ”;

cin>>n;

cout<<”Choose the type CPU scheduling :\n”;

cout<<”1 for FCFS scheduling.\n”;

cout<<”2 for SJF scheduling.\n”;

cout<<”3 for Priority scheduling.\n”;

cout<<”4 for Round Robin scheduling.\n -> ”;

[Step-4]

cin>>s;

switch(s)

case ‘1’ :

fcfs(int n);
break;

case ‘2’ :

sjf(int n);

break;

case ‘3’ :

priority(int n);

break;

case ‘4’ :

round_robin(int n);

break;

OUTPUT:
Practical : 6

Write a program to implement classical inter process communication problem (producer consumer).

[By using the concept of Semaphore]

Algorithm:

Step 1: Start the program.

Step 2: Initialize loop.

Step 3: Function producer generate a piece of data (item), put it into buffer and start again.

Step 4: Function consumer consuming the data (i.e. remove item from buffer), one piece at a time.

Step 5: Function exit return 0, which will terminate loop.

Step 6: End program.

Source Code:

[Step-1]

#include<stdio.h>

#include<conio.h>

int mutex=1,full=0,empty=3,x=0; //(mutex) for mutual exclusion

int main()

int n;

void producer();

void consumer();

int wait(int);

int signal(int);

printf("1.Producer\n2.Consumer\n3.Exit\n");

[Step-2]

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!!\n");

break;

case 3: [Step-5]

exit(0); //it returns 0, which will terminate while loop.

break;

return 0;

int wait(int s)

return (--s);

int signal(int s)

return(++s);

[Step-3]

void producer()

mutex=wait(mutex);
full=signal(full);

empty=wait(empty);

x++;

printf("Producer produces the item %d\n",x);

mutex=signal(mutex);

[Step-4]

void consumer()

mutex=wait(mutex);

full=wait(full);

empty=signal(empty);

printf("Consumer consumes item %d\n",x);

x--;

mutex=signal(mutex);

OUTPUT:
Practical: 10
Write a program to implement & Compare various Disk & Drum scheduling Algorithms
Aim is to provide total head movement in different disk scheduling algorithms namely FCFS, SSTF,
LOOK, C-LOOK, SCAN, C-SCAN.

Algorithm:
Step 1: Start the program.
Step 2: Input Disk limit.
Step 3: Input total no. of requests.
Step 4: Input initial head location and its current direction of movement.
Step 5: Sort requests in ascending order.
Step 6: Apply different scheduling approaches (FCFS, SSTF, LOOK, C-LOOK, SCAN, C-SCAN).
Step 7: Stop the program.

Source Code:

[Step-1]

#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()
{ [Step-2]
cout<<"\nEnter maximum disk limit : ";
cin>>max;
}
void receive_request()
{ [Step-3]
enter_request_number:
cout<<"\nEnter number of requests (max. 100): ";
cin>>number_of_request;
if(number_of_request>100)
goto enter_request_number;

[Step-4]
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--;
}
}
[Step-5]
qsort(sorted_request+1,number_of_request,sizeof(int),compare);

}
[Step-6]
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;
}
[Step-6]
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;
}
[Step-6]
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;
}
[Step-6]
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;
}

[Step-6]
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;
}
[Step-6]
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;
_getch();
}

OUTPUT:
Practical : 11

Write a program to implement Banker’s Algo.

Algorithm:

Step 1: Start the program.

Step 2: Input no. of process(n) and resource(r).

Step 3: Define allocation, max matrix by ‘n’ and available array by ‘r’.

Step 4: Input Allocation , Max and Available.

Step 5: Calculate “Need” matrix.

Step 6: Assign work[r] = available[r].

Step 7: Calculate Safe sequence and display it.

Step 8: Stop the program.

Source Code:

[Step-1]

#include<iostream>

using namespace std;

int main()

cout<<"Banker's Algo.\n";

int n,r;

[Step-2]

cout<<"Enter no. of Processes: ";

cin>>n;

cout<<"Enter no. of Resources: ";

cin>>r;

[Step-3]

int allocation[n][r],max[n][r],available[r],need[n][r],work[r];

int i,j,safe[n];

bool finish[n] ;

[Step-4]

cout<<"Insert Allocation values:\n";


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

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

cin>>allocation[i][j];

cout<<"Insert Maximum Resource required:\n";

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

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

cin>>max[i][j];

cout<<"Insert Available Resource:\n";

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

cin>>available[j];

[Step-5]

cout<<"Need Matrix is :\n";

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

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

need[i][j]=max[i][j]-allocation[i][j];

cout<<need[i][j]<<" ";

}
cout<<endl;

[Step-6]

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

work[i]=available[i];

[Step-7]

int count = 0;

while (count < n)

bool found = false;

for (int p = 0; p < n; p++)

if (finish[p] == 0)

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

if (need[p][j] > work[j])

break;

if (j == r)

for (int k = 0 ; k < r ; k++)

work[k] += allocation[p][k];

// Add this process to safe sequence.

safe[count++] = p;

// Mark this p as finished

finish[p] = 1;
found = true;

if (found == false)

cout << "System is not in safe state";

return false;

cout << "System is in safe state.\nSafesequence is: ";

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

cout << safe[i] << " ";

OUTPUT:

You might also like