Professional Documents
Culture Documents
Os Lab - 1246517 - 2023 - 05 - 30 - 22 - 04
Os Lab - 1246517 - 2023 - 05 - 30 - 22 - 04
#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;
}
2
Enter Time Quantum: 6
Process[2] 5 10 5
Process[1] 8 25 17
Process[3] 10 27 17
Process[4] 11 31 20
#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);
}
4
Average Turnaround Time=11.500000
#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);
}
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);
P[2]
Burst Time:7
Priority:1
P[3]
Burst Time:10
Priority:2
P[4]
Burst Time:15
Priority:7
#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]);
}
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();
}
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
#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();
}
#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:
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();
}
#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();
}
15