0% found this document useful (0 votes)
37 views34 pages

Operating System Lab: ETCS-352

The document appears to be a student's lab report for an operating systems course. It contains 4 experiments related to CPU scheduling algorithms: first come first serve, shortest job first, priority scheduling, and round robin. For each experiment, the student provides the code for implementing the scheduling algorithm, example output, and a brief explanation. The experiments cover common CPU scheduling techniques studied in operating systems courses.

Uploaded by

Piyush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views34 pages

Operating System Lab: ETCS-352

The document appears to be a student's lab report for an operating systems course. It contains 4 experiments related to CPU scheduling algorithms: first come first serve, shortest job first, priority scheduling, and round robin. For each experiment, the student provides the code for implementing the scheduling algorithm, example output, and a brief explanation. The experiments cover common CPU scheduling techniques studied in operating systems courses.

Uploaded by

Piyush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Piyush Gupta

41714802716

OPERATING SYSTEM LAB


ETCS-352

Ms. Savita Piyush Gupta


41714802716
6th Sem

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi – 110085
Piyush Gupta
41714802716
Piyush Gupta
41714802716

INDEX
Piyush Gupta
41714802716

Exp. Experiment Name Date of Date of Remarks Marks


no performance checking (10)
Piyush Gupta
41714802716
Piyush Gupta
41714802716

Exp. Experiment Name Date of Date of Remarks Marks


no performance checking (10)
Piyush Gupta
41714802716
Piyush Gupta
41714802716
Experiment 1
AIM : Write a program to implement CPU scheduling for first come first serve.

echo enter number of processes


read num
echo enter burst time and arrival time of processes
i=1
declare -a b # burst time
declare -a w # waiting time
declare -a a # arrival time
declare -a t # turn around time
b[0]=0
t[0]=0
a[0]=0
w[0]=0
while [ $i -le $num ]
do
echo "p[$i] : "
read b[$i]
read a[$i]
((w[i]= a[i-1] + t[i-1] - a[i]))

if [ ${w[$i]} -le 0 ]
then
((w[i]=0))
((t[i]=b[i]))
fi

if [ ${w[$i]} -gt 0 ]
then
((t[i]=b[i]+w[i]))
fi

((i++))
done
i=1
while [ $i -le $num ]
do
echo "p[$i] : waiting time = ${w[$i]} , compeletion time = ${t[$i]}"
((i++))
done
Piyush Gupta
41714802716
OUTPUT :
nitin@nitin-G41MT-S2:~/Desktop$ ./fcfs.sh
enter number of processes
5
enter burst time and arrival time of processes
p[1] :
3
2
p[2] :
5
3
p[3] :
1
3
p[4] :
1
5
p[5] :
7
10
p[1] : waiting time = 0 , compeletion time = 3
p[2] : waiting time = 2 , compeletion time = 7
p[3] : waiting time = 7 , compeletion time = 8
p[4] : waiting time = 6 , compeletion time = 7
p[5] : waiting time = 2 , compeletion time = 9
Piyush Gupta
41714802716
Experiment 2
AIM : Write a program to implement CPU scheduling for shortest job first.

echo "enter number of jobs"


read num

echo "enter burst time and arrival time of jobs"

declare -a a # arrival time


declare -a b # burst time
declare -a c # job status flag array
declare -a s # job sequence
declare -a w # waiting time
declare -a t # turn around time

short=1000
ind=0
i=1
j=1
total=0
k=0
while [ $i -le $num ]
do
echo "p[$i] : "
read b[$i]
read a[$i]
c[$i]=0
w[$i]=0
((total=total+b[i]))
((i++))
done

i=0

while [ $total -gt 0 ]


do
short=1000
j=1
ind=-1
while [ $j -le $num ]
do
if [ ${c[$j]} -eq 0 ]
then
if [ ${a[$j]} -le $i ]
then
if [ ${b[$j]} -lt $short ]
then
((short=b[j]))
((ind=j))
Piyush Gupta
41714802716
fi
fi
fi
((j++))
done
if [ $ind -gt -1 ]
then
((total=total-1))
((b[ind]=b[ind]-1))
if [ ${b[$ind]} -eq 0 ]
then
c[ind]=1
fi
fi
((s[k]=ind))
((k++))
((i++))
done

((total=i-1))
i=1
average_w=0
average_t=0

while [ $i -le $num ]


do
last=${a[$i]}
j=$last
while [ $j -le $total ]
do
if ((s[j]==i))
then
((w[i]=w[i]+j-last))
((last=j+1))
fi
((j++))
done
echo $last
((t[i]=last-a[i]))
((average_w= average_w+w[i]))
((average_t= average_t+t[i]))
((i++))
done
((average_w=average_w/num))
((average_t=average_t/num))
echo ${s[@]}
echo average waiting time : $average_w
echo average turn around time : $average_t
echo indiviual waiting time : ${w[@]}
Piyush Gupta
41714802716
echo indiviual turn around time : ${t[@]}

OUTPUT :
nitin@nitin-G41MT-S2:~/Desktop/bash$ ./sjf.sh
enter number of jobs
6
enter burst time and arrival time of jobs
p[1] :
1
2
p[2] :
1
3
p[3] :
3
3
p[4] :
1
4
p[5] :
2
6
p[6] :
1
12
-1 -1 1 2 4 3 3 3 5 5 -1 -1 6
average waiting time : 0
average turn around time : 2
indiviual waiting time : 0 0 2 0 2 0
indiviual turn around time : 1 1 5 1 4 1
Piyush Gupta
41714802716
Experiment 3
AIM : Write a program to perform priority scheduling.

echo "enter number of jobs"


read num

echo "enter burst time , arrival time and priority of jobs"

declare -a a # arrival time


declare -a b # burst time
declare -a c # job status flag array
declare -a s # job sequence
declare -a w # waiting time
declare -a t # turn around time
declare -a p # priority
short=1000
ind=0
i=1
j=1
total=0
k=0
while [ $i -le $num ]
do
echo "p[$i] : "
read b[$i]
read a[$i]
read p[$i]
c[$i]=0
w[$i]=0
((total=total+b[i]))
((i++))
done
i=0
while [ $total -gt 0 ]
do
pi=-1
j=1
ind=-1
while [ $j -le $num ]
do
if [ ${c[$j]} -eq 0 ]
then
if [ ${a[$j]} -le $i ]
then
if [ ${p[$j]} -gt $pi ]
then
((pi=p[j]))
((ind=j))
fi
Piyush Gupta
41714802716
fi
fi
((j++))
done
if [ $ind -gt -1 ]
then
((total=total-1))
((b[ind]=b[ind]-1))
if [ ${b[$ind]} -eq 0 ]
then
c[ind]=1
fi
fi
((s[k]=ind))
((k++))
((i++))
done
((total=i-1))
i=1
average_w=0
average_t=0
while [ $i -le $num ]
do
last=${a[$i]}
j=$last
while [ $j -le $total ]
do
if ((s[j]==i))
then
((w[i]=w[i]+j-last))
((last=j+1))
fi
((j++))
done
echo $last
((t[i]=last-a[i]))
((average_w= average_w+w[i]))
((average_t= average_t+t[i]))
((i++))
done
((average_w=average_w/num))
((average_t=average_t/num))
echo ${s[@]}
echo average waiting time : $average_w
echo average turn around time : $average_t
echo indiviual waiting time : ${w[@]}
echo indiviual turn around time : ${t[@]}
Piyush Gupta
41714802716
OUTPUT :
nitin@nitin-G41MT-S2:~/Desktop/bash$ ./ps.sh
enter number of jobs
5
enter burst time , arrival time and priority of jobs
p[1] :
4
0
3
p[2] :
3
3
1
p[3] :
7
8
4
p[4] :
1
10
5
p[5] :
2
11
0
1 1 1 1 2 2 2 -1 3 3 4 3 3 3 3 3 5 5
average waiting time : 1
average turn around time : 4
indiviual waiting time : 0 1 1 0 5
indiviual turn around time : 4 4 8 1 7
Piyush Gupta
41714802716
Experiment 4
AIM : Write a program to implement CPU scheduling for Round Robin.

echo enter number of processes


read num
echo "enter burst time and arrival time of jobs in increasing order of arrival time"
declare -a w # waiting time
declare -a a # arrival time
declare -a b # burst time
declare -a ob
declare -a t # turn around time
declare -a s # sequence of execution of jobs
declare -a queue
front=-1
rear=-1
out=0
enqueue(){
if [ $rear -eq -1 ]
then
rear=0
front=0
((queue[rear]=$1))
return 0
fi
if [ $(($((rear+1))%num)) -eq $front ]
then
echo overflow
return 0
fi
((rear=$((rear+1))%num))
((queue[rear]=$1))
return 0
}
dequeue(){
if [ $front -eq $rear ]
then
if [ $front -eq -1 ]
then
echo underflow
return 0
fi
((out= queue[front]))
((queue[front]=-1))
front=-1
rear=-1
return $out
fi
((out= queue[front]))
((queue[front]=-1))
Piyush Gupta
41714802716
((front=$((front+1))%num))
return $out
}
i=1
j=2
total=$num
k=0
exe=0
qua=0
while [ $i -le $num ]
do
echo "p[$i] : "
read b[$i]
read a[$i]
ob[$i]=${b[$i]}
t[$i]=0
w[$i]=0
((i++))
done
echo enter value of quantum
read qua
enqueue 1
for ((i=a[1];total!=0;))
do
dequeue
((exe= $?))
if [ $exe -gt 0 ]
then
if [ ${b[$exe]} -gt $qua ]
then

while [ $j -le $num ] && [ ${a[$j]} -le $((i+qua)) ]


do
enqueue $j
((j+=1))
done
enqueue $exe
((b[exe]-=qua))
((i+=qua))
s[k]="p${exe}"
((k+=1))
else
while [ $j -le $num ] && [ ${a[$j]} -le $((i+b[exe])) ]
do
enqueue $j
((j+=1))
done
((t[exe]=i+b[exe]-a[exe]))
((w[exe]=t[exe]-ob[exe]))
Piyush Gupta
41714802716
((i+=b[exe]))
((b[exe]=0))
((total-=1))
s[k]="p${exe}"
((k+=1))
fi
else
((i=a[j]))
enqueue j
((j++))
fi
done

echo waiting times : ${w[@]}


echo turnaround times : ${t[@]}
echo sequence of execution : ${s[@]}

OUTPUT :
nitin@nitin-G41MT-S2:~/Desktop$ ./rr.sh
enter number of processes
5
enter burst time and arrival time of jobs in increasing order of arrival time
p[1] :
5
1
p[2] :
2
3
p[3] :
1
4
p[4] :
6
8
p[5] :
9
9
enter value of quantum
2
waiting times : 3 0 3 5 6
turnaround times : 8 2 4 11 15
sequence of execution : p1 p2 p1 p3 p1 p4 p5 p4 p5 p4 p5 p5 p5
Piyush Gupta
41714802716
EXPERIMENT 5

AIM: Write a program for page replacement policy using a) LRU b) FIFO c) Optimal.

#include<stdio.h>

int n=13,nf=4;

int in[]={7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2};

int p[4];

int hit=0;

int i,j,k;

int
pgfaultcnt=0;

void
initialize(){

pgfaultcnt=0;

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

p[i]=9999;

int isHit(int data){

hit=0;

for(j=0; j<nf; j++){

if(p[j]==data){

hit=1;

break;

return hit;

int getHitIndex(int data){


Piyush Gupta
41714802716

int hitind;

for(k=0; k<nf; k++){

if(p[k]==data){

hitind=k;

break;

return hitind;

void dispPages(){

for (k=0; k<nf; k++){

if(p[k]!=9999)

printf(" %d",p[k]);

void dispPgFaultCnt(){

printf("\nTotal no of page faults:%d",pgfaultcnt);

void fifo(){

initialize();

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

printf("\nFor %d :",in[i]);

if(isHit(in[i])==0){

for(k=0; k<nf-1; k++)

p[k]=p[k+1];

p[k]=in[i];
Piyush Gupta
41714802716

pgfaultcnt++;
dispPages();

else

printf("No page fault");

dispPgFaultCnt();

void optimal(){

initialize();

int near[50];

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

printf("\nFor %d :",in[i]);

if(isHit(in[i])==0){

for(j=0; j<nf; j++){

int pg=p[j];

int found=0;

for(k=i; k<n; k++){

if(pg==in[k]){

near[j]=k;

found=1;

break;

else

found=0;

}
Piyush Gupta
41714802716
if(!found)

near[j]=9999;

int max=-9999;

int repindex;

for(j=0; j<nf; j++){

if(near[j]>max){

max=near[j];

repindex=j;

p[repindex]=in[i];

pgfaultcnt++;

dispPages();

else

printf("No page fault");

dispPgFaultCnt();

void lru(){

initialize();

int least[50];

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

printf("\nFor %d :",in[i]);

if(isHit(in[i])==0){
Piyush Gupta
41714802716

for(j=0; j<nf; j++){

int pg=p[j];

int found=0;

for(k=i-1; k>=0; k--){

if(pg==in[k]){

least[j]=k;
found=1;

break;

else

found=0;

if(!found)

least[j]=-9999;

int min=9999;

int repindex;

for(j=0; j<nf; j++){

if(least[j]<min){

min=least[j];

repindex=j;

p[repindex]=in[i];

pgfaultcnt++;

dispPages();
Piyush Gupta
41714802716
}

else

printf("No page fault!");

dispPgFaultCnt();

int main(){

printf("FIFO page replacement algorithm"); fifo();

printf("\n\nOptimal page replacement algorithm"); optimal();

printf("\n\nLRU page replacement algorithm");

lru();

return 0;

Output:
Piyush Gupta
41714802716
Piyush Gupta
41714802716
EXPERIMENT 6

AIM: Write a program to implement first fit, best fit, worst fit algorithm for memory management.

#include<stdio.h>

int main(){

int alloc[10],flag[10],i,j;

int np=3;

int nb=3;

int p[] = {100,150,200};

int b[] = {300,350,200};

int c[] = {300,350,200};

int d[] = {300,350,200};

printf("First Fit");

for(i=0;i<np;i++){

for(j=0;j<nb;j++){

if(p[i]<=b[j]){

alloc[j]=p[i];

printf("\nAlloc[%d]",alloc[j]);

printf("\nProcess%dof size %dis allocated block:%dof size:%d",i,p[i],j,b[j]);

flag[i]=0,b[j]=0;

break;

}else flag[i]=1;

for(i=0;i<np;i++){

if(flag[i]!=0)

printf("\n\nProcess %d of size %d is not allocated",i,p[i]);


Piyush Gupta
41714802716

printf("\n\nBest Fit\n");

for(i=0;i<nb;i++){

for(j=i+1;j<nb;j++){

if(c[i]>c[j]){

int temp=c[i];c[i]=c[j];

c[j]=temp;

printf("\nAfter sorting block sizes:");

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

printf("\nBlock %d:%d",i,c[i]);

for(i=0;i<np;i++){

for(j=0;j<nb;j++){

if(p[i]<=c[j]){

alloc[j]=p[i];

printf("\nAlloc[%d]",alloc[j]);

printf("\nProcess%dof size %dis allocated block:%dof size:%d",i,p[i],j,b[j]);

flag[i]=0,c[j]=0;

break;

}else flag[i]=1;

for(i=0;i<np;i++){

if(flag[i]!=0)
Piyush Gupta
41714802716

printf("\n\nProcess %d of size %d is not allocated",i,p[i]); }

printf("\n\nWorst Fit\n");

for(i=0;i<nb;i++){

for(j=i+1;j<nb;j++){

if(d[i]<d[j]){

int temp=d[i];d[i]=d[j];d[j]=temp;

printf("\nAfter sorting block sizes:");

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

printf("\nBlock %d:%d",i,d[i]);

for(i=0;i<np;i++){

for(j=0;j<nb;j++){

if(p[i]<=d[j]){

alloc[j]=p[i];printf("\nAlloc[%d]",alloc[j]);

printf("\nProcess%dof size %dis allocated block:%dof size:%d",i,p[i],j,b[j]);

flag[i]=0;d[j]=0;break;

}else flag[i]=1;

for(i=0;i<np;i++){

if(flag[i]!=0)

printf("\n\nProcess %d of size %d is not allocated",i,p[i]);

}
Piyush Gupta
41714802716
return 0;

Output:
Piyush Gupta
41714802716

EXPERIMENT 7

AIM: Write a program to implement reader/writer problem using semaphore.

PROGRAM:

#include<stdio.h>

int main() {

int buffer[10], bufsize, in, out, produce, consume, choice=0; in = 0; out = 0;

bufsize = 10;

while(choice !=3) {

printf("\n1. Produce \t 2. Consume \t3. Exit");

printf("\nEnter your choice: =");

scanf("%d", &choice);

switch(choice) {

case 1: if((in+1)%bufsize==out)

printf("\nBuffer is Full");

else {

printf("\nEnter the value: ");

scanf("%d", &produce); buffer[in] = produce; in = (in+1)%bufsize;

break;

case 2: if(in == out)

printf("\nBuffer is Empty");

else {

consume = buffer[out];
Piyush Gupta
41714802716

printf("\nThe consumed value is %d", consume);

out = (out+1)%bufsize;

break;

return 0;

Output:
Piyush Gupta
41714802716

EXPERIMENT 8

AIM: Write a program to implement Banker’s algorithm for deadlock avoidance.

PROGRAM:

#include<iostream.h>

const int P = 5,R = 3;

void calculateNeed(int need[P][R], int maxm[P][R], int allot[P][R]) { for (int i = 0 ; i < P ; i++)

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

need[i][j] = maxm[i][j] - allot[i][j];

bool isSafe(int processes[], int avail[], int maxm[][R],int allot[][R]){ int need[P][R];

calculateNeed(need, maxm, allot);

bool finish[P] = {0};

int safeSeq[P];

int work[R];
Piyush Gupta
41714802716
for (int i = 0; i < R ; i++)

work[i] = avail[i];

int count = 0;

while (count < P){

bool found = false;

for (int p = 0; p < P; p++) {

if (finish[p] == 0) {

int j;

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] += allot[p][k];

safeSeq[count++] = p;

finish[p] = 1;

found = true;

}}}

if (found == false) {

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

return false;

}}

cout << "System is in safe state.\nSafe sequence is: "; for (int i = 0; i < P ;
i++)

cout << safeSeq[i] << " ";

return true;

}
Piyush Gupta
41714802716

int main() {

int processes[] = {0, 1, 2, 3, 4};

int avail[] = {3, 3, 2};

int maxm[][R] = {{7, 5, 3},{3, 2, 2},{9, 0, 2},{2, 2, 2},{4, 3, 3}}; int allot[][R] = {{0, 1, 0},{2, 0, 0},{3, 0,
2},{2, 1, 1},{0, 0, 2}};

isSafe(processes, avail, maxm, allot);

return 0;

OUTPUT:

You might also like