Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

OS LAB MANUAL

1.Write a C program for Round robin algorithm in C?

#include<stdio.h>
int main()
{
int i, n, total = 0, x, counter = 0, tq;
int wt = 0, tat = 0, at[10], bt[10], temp[10];
float awt, atat;
printf("\nEnter Total Number of Processes:\t");
scanf("%d", &n);
x = n;
for(i = 0; i < n; i++)
{
printf("\nEnter Details of Process[%d]\n", i + 1);
printf("Arrival Time:\t");
scanf("%d", &at[i]);
printf("Burst Time:\t");
scanf("%d", &bt[i]);
temp[i] = bt[i];
}
printf("\nEnter Time Quantum:\t");
scanf("%d", &tq);
printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(total = 0, i = 0; x != 0;)
{
if(temp[i] <= tq && temp[i] > 0)
{
total = total + temp[i];
temp[i] = 0;
counter = 1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - tq;
total = total + tq;
}
if(temp[i] == 0 && counter == 1)
{
x--;
printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, bt[i], total - at[i],
total - at[i] - bt[i]);

1
wt = wt + total - at[i] - bt[i];
tat = tat + total - at[i];
counter = 0;
}
if(i == n - 1)
{
i = 0;
}
else if(at[i + 1] <= total)
{
i++;
}
else
{
i = 0;
}
}
awt = wt * 1.0 / n;
atat = tat * 1.0 / n;
printf("\n\nAverage Waiting Time:\t%f", awt);
printf("\nAvg Turnaround Time:\t%f\n", atat);
return 0;
}

INPUT AND OUTPUT

Enter Details of Process[2]


Arrival Time: 0
Burst Time: 8

Enter Details of Process[2]


Arrival Time: 1
Burst Time: 5

Enter Details of Process[3]


Arrival Time: 2
Burst Time: 10

Enter Details of Process[4]


Arrival Time: 3
Burst Time: 11

2
Enter Time Quantum: 6

Process ID Burst Time Turnaround Time Waiting Time

Process[2] 5 10 5
Process[1] 8 25 17
Process[3] 10 27 17
Process[4] 11 31 20

Average Waiting Time: 14.750000


Avg Turnaround Time: 23.250000

2. Write a C program for SJF 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("\n Enter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
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];

3
p[pos]=temp;
}
wt[0]=0;
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;
total=0;
printf("\n Process\t Burst Time \t Waiting Time \tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
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;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

INPUT AND OUTPUT

Enter number of process:4

Enter Burst Time:


p1:4
p2:8
p3:3
p4:7

Process Burst Time Waiting Time Turnaround Time


p3 3 0 3
p1 4 3 7
p4 7 7 14
p2 8 14 22

Average Waiting Time=6.000000

4
Average Turnaround Time=11.500000

3. Write a C program for FCFS 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("\n Enter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
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;
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;
total=0;

5
printf("\n Process\t Burst Time \t Waiting Time \tTurnaround Time");
for(i=0;i<n;i++){
tat[i]=bt[i]+wt[i];
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;
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

INPUT AND OUTPUT:

Enter number of process:4

Enter Burst Time:


p1:9
p2:5
p3:12
p4:17

Process Burst Time Waiting Time Turnaround Time


p2 5 0 5
p1 9 5 14
p3 12 14 26
p4 17 26 43

Average Waiting Time=11.250000


Average Turnaround Time=22.000000

4. Write a C program for priority CPU Scheduling algorithm ?


#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avgwt,avgtat;
printf("\nEnter Total Number of Process:");
scanf("%d",&n);

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


for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);

6
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1;
}

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;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avgwt=total/n;
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];

7
printf("\nP[%d]\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avgtat=total/n;
printf("\n\nAverage Waiting Time=%d",avgwt);
printf("\nAverage Turnaround Time=%d\n",avgtat);

INPUT AND OUTPUT:

Enter Total number of process:4


Enter Burst Time and Priority
P[1]
Burst Time:5
Priority:3

P[2]
Burst Time:7
Priority:1

P[3]
Burst Time:10
Priority:2

P[4]
Burst Time:15
Priority:7

Process Burst Time Waiting Time Turnaround Time


P[2] 7 0 7
P[3] 10 7 17
P[1] 5 17 22
P[4] 15 22 37

Average Waiting Time=11


Average Turnaround Time=20

5. Write a C program to demonstrate Sequential file?

#include<stdio.h>
#include<conio.h>
struct fileTable

8
{
char name[20];
int nob, blocks[30];

}ft[30];

void main()
{
int i, j, n; char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[j]);
}

printf("\nEnter the file name to be searched -- ");


scanf("%s",s);
for(i=0;i<n;i++)
if(strcmp(s, ft[i].name)==0)
break;

if(i==n)
printf("\nFile Not Found");
else
{
printf("\nFILE NAME NO OF BLOCKS BLOCKS OCCUPIED");
printf("\n %s\t\t%d\t",ft[i].name,ft[i].nob); for(j=0;j<ft[i].nob;j++)
printf("%d, ",ft[i].blocks[j]);
}
getch();
}

INPUT AND OUTPUT:

Enter no of files :3

9
Enter file name 1 :A
Enter no of blocks in file 1 :4
Enter the blocks of the file :101 102 103 104

Enter file name 2 :B


Enter no of blocks in file 2 :5
Enter the blocks of the file :111 112 113 114 115

Enter file name 3 :C


Enter no of blocks in file 3 :2
Enter the blocks of the file :121 122

Enter the file name to be searched -- A

FILE NAME NO OF BLOCKS BLOCKS OCCUPIED


A 4 101, 102, 103, 104,

6. Write a C program to demonstrate Indexed file?

#include<stdio.h>
#include<conio.h>
struct fileTable
{
char name[20];
int nob, blocks[30][30],ib[20];
}ft[30];

void main()
{
int i, j, n; char s[20];
clrscr();
printf("Enter no of files :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter file name %d :",i+1);
scanf("%s",ft[i].name);
printf("\n Enter the index block %d :",i+1);
scanf("%d",&ft[i].ib[i]);
printf("Enter no of blocks in file %d :",i+1);
scanf("%d",&ft[i].nob);
printf("Enter the blocks of the file :");

10
for(j=0;j<ft[i].nob;j++)
scanf("%d",&ft[i].blocks[i][j]);
}
printf("\nFILE NAME INDEX LENGTH");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d\t",ft[i].name,ft[i].ib[i],ft[i].nob);
printf("\nBLOCKS OCCPIED IN FILE IS");
for(i=0;i<n;i++)
{
printf("\nFILE NO %d",i+1);
for(j=0;j<ft[i].nob;j++)
printf("\t%d-->%d",ft[i].ib[i],ft[i].blocks[i][j]);
printf("\n");
}
getch();
}

INPUT AND OUTPUT:


Enter no of files :2

Enter file name 1 :A

Enter the index block 1 :23


Enter no of blocks in file 1 :2
Enter the blocks of the file :12 13

Enter file name 2 :B

Enter the index block 2 :24


Enter no of blocks in file 2 :2
Enter the blocks of the file :15 16

FILE NAME INDEX LENGTH


A 23 2
B 24 2
BLOCKS OCCPIED IN FILE IS
FILE NO 1 23-->12 23-->13

FILE NO 2 24-->15 24-->16

7.Write a program to demonstrate Linked List ?

#include<stdio.h>

11
#include<conio.h>

struct file
{
char fname[10];
int start,size,block[10];
}f[10];

void main()
{
int i,j,n;
clrscr();
printf("Enter no. of files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter file name:");
scanf("%s",&f[i].fname);
printf("Enter starting block:");
scanf("%d",&f[i].start);
f[i].block[0]=f[i].start;
printf("Enter no.of blocks:");
scanf("%d",&f[i].size);
printf("Enter block numbers:");
for(j=1;j<=f[i].size;j++)
{
scanf("%d",&f[i].block[j]);
}
}
printf("File\tstart\tsize\tblock\n");
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t",f[i].fname,f[i].start,f[i].size); for(j=0;j<f[i].size;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);
printf("\n");
}
getch();
}
INPUT AND OUTPUT:

Enter no. of files:2

12
Enter file name:A
Enter starting block:20
Enter no.of blocks:6
Enter block numbers:15
16
17
18
19
20
Enter file name:B
Enter starting block:30
Enter no.of blocks:2
Enter block numbers:20 12
File start size block
A 20 6 20--->15--->16--->17--->18--->19--->20
B 30 2 30--->20--->12

8. Write a program to demonstrate Multiple Programming with fixed Number of Tasks (MFT)
algorithm?

#include<stdio.h>
#include<conio.h>
void main()
{
int tot,i,s[20],n,size,p[20],m,ifr=0;
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter memory for OS:");
scanf("%d",&m);
tot=tot-m;
printf("Enter no.of partitions to be divided:");
scanf("%d",&n);
size=tot/n;
for(i=0;i<n;i++)
{
printf("Enter Page Size");
scanf("%d",&s[i]);
if(s[i]<=size)
{
ifr=ifr+size-s[i];
printf("process %d is allocated\n",p[i]);
}

13
else
printf("process %d is blocked",p[i]);
}
printf("Internal fragmentation is %d",ifr);
getch();
}

INPUT AND OUTPUT:

Enter total memory size:50


Enter memory for OS:10
Enter no.of partitions to be divided:4
Enter Page Size10
process 80 is allocated
Enter Page Size9
process 27 is allocated
Enter Page Size9
process 355 is allocated
Enter Page Size10
process 0 is allocated
Internal fragmentation is 2

9. Write a program to demonstrate Multiple Programming with Varible Number of Tasks


(MVT) algorithm?

#include<stdio.h>
#include<conio.h>
void main()
{
int i,m,n,tot,s[20];
clrscr();
printf("Enter total memory size:");
scanf("%d",&tot);
printf("Enter no. of processes:");
scanf("%d",&n);
printf("Enter memory for OS:");
scanf("%d",&m);
for(i=0;i<n;i++)
{
printf("Enter size of process %d:",i+1);
scanf("%d",&s[i]);
}
tot=tot-m;

14
for(i=0;i<n;i++)
{
if(tot>=s[i])
{
printf("Process %d is allocated \n",i+1);
tot=tot-s[i];
}
else
printf("process p%d is blocked\n",i+1);
}
printf("External Fragmentation is=%d",tot);
getch();
}

INPUT AND OUTPUT:

Enter total memory size:50


Enter no. of processes:4
Enter memory for OS:10
Enter size of process 1:9
Enter size of process 2:9
Enter size of process 3:8
Enter size of process 4:10
Process 1 is allocated
Process 2 is allocated
Process 3 is allocated
Process 4 is allocated
External Fragmentation is=4

15

You might also like