OS Lab File
OS Lab File
OS Lab File
Using C.
#include<stdio.h>
int main()
int at[10],at2[10],bt[100],ex[100],seq[100],re[100],wt[100],tat[100];
int n,i,j,start,pos,max=0,min,idle=0,k=0;
float av1=0,av2=0;
printf("*****INPUT*****\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&at[i]);
at2[i]=at[i];
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
start=at[0];
for(i=1;i<n;i++)
if(start>at[i])
start=at[i];
}
printf("*****OUTPUT*****\n");
for(i=0;i<n;i++)
if(max<at[i])
max=at[i];
max=max+1;
for(i=0;i<n;i++,k++)
{ min=max;
for(j=0;j<n;j++){
if(at[j]!=-1)
if(at[j]<min)
min=at[j];
pos=j;
printf("[P%d] ",pos);
seq[k]=pos;
if(start<at[pos]){
re[pos]=start;
idle+=at[pos]-start;
start=at[pos];
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
else{
re[pos]=start;
start+=bt[pos];
at[pos]=-1;
ex[pos]=start;
printf("\n");
for(i=0;i<n;i++)
tat[i]=ex[i]-at2[i];
wt[i]=tat[i]-bt[i];
for(i=0;i<n;i++)
printf("P%d %d %d %d %d\n",i,at2[i],bt[i],wt[i],tat[i]);
for(i=0;i<n;i++)
av1+=tat[i];
av2+=wt[i];
Output:-
*****INPUT*****
35054
43213
*****OUTPUT*****
Sequence of execution is
P0 3 4 0 4
P1 5 3 5 8
P2 0 2 0 2
P3 5 1 8 9
P4 4 3 3 6
#include<stdio.h>
int main()
scanf("%d", &limit);
x = limit;
printf("Arrival Time:\t");
scanf("%d", &arrival_time[i]);
printf("Burst Time:\t");
scanf("%d", &burst_time[i]);
temp[i] = burst_time[i];
scanf("%d", &time_quantum);
for(total = 0, i = 0; x != 0;)
temp[i] = 0;
counter = 1;
}
x--;
counter = 0;
if(i == limit - 1)
i = 0;
i++;
else
i = 0;
return 0;
Output:-
Lab 3a:- FIFO Page Replacement Algorithm
#include<stdio.h>
int main()
int i,j,n,a[50],frame[10],no,k,avail,count=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&no);
for(i=0;i<no;i++)
frame[i]= -1;
j=0;
for(i=1;i<=n;i++)
printf("%d\t\t",a[i]);
avail=0;
for(k=0;k<no;k++)
if(frame[k]==a[i])
avail=1;
if (avail==0)
frame[j]=a[i];
j=(j+1)%no;
count++;
for(k=0;k<no;k++)
printf("%d\t",frame[k]);
printf("\n\n");
return 0;
Output:-
20
7 7 -1 -1
0 7 0 -1
1 7 0 1
2 2 0 1
3 2 3 1
4 2 3 4
0 0 3 4
2 0 2 4
1 0 2 1
7 7 2 1
0 7 0 1
1
Page Fault Is 11
--------------------------------
#include<conio.h>
#define high 37
void main()
int fframe[10],used[10],index;
int count,n1,k,nf,np=0,page[high],tmp;
int flag=0,pf=0;
clrscr();
scanf("%d",&nf);
for(i=0;count<nf;count++)
fframe[count]=-1;
for(count=0;count<high;count++)
scanf("%d",&tmp);
if(tmp==-999) break;
page[count]=tmp;
np++;
for(count=0;count<np;count++)
flag=0;
for(n1=0;n1<nf;n1++)
if(fframe[n1]==page[count])
{
printf("\n\t");
flag=1;break;
if(flag==0)
for(n1=0;n1<nf;n1++) used[n1]=0;
for(n1=0,tmp=count-1;n1<nf-1;n1++,tmp--)
for(k=0;k<nf;k++)
if(fframe[k]==page[tmp])
used[k]=1;
for(n1=0;n1<nf;n1++)
if(used[n1]==0)
index=n1;
fframe[index]=page[count];
printf("\nFault: ");
pf++;
for(k=0;k<nf;k++)
printf("%d\t",fframe[k]);
} // lru algorithm in c
getch();
Output:-
lru page replacement algorithm in c
Enter no. of frames Enter pages :
2321
5245
3 2 5 -999
-1 -1 2 Fault:
-1 -1 2 Fault:
-1 3 2 Fault:
1 3 2 Fault:
132
1 5 2 Fault:
4 5 2 Fault:
452
4 5 3 Fault:
2 5 3 Fault:
253
253
Lab 3c:- Implementation of Optimal Page Replacement
algorithm
#include<stdio.h>
#include<conio.h>
main()
int fr[5],i,j,k,t[5],p=1,flag=0,page[25],psz,nf,t1,u[5];
clrscr();
scanf("%d",&nf);
scanf("%d",&psz);
scanf("%d",&page[i]);
fr[i]=-1;
if(full(fr,nf)==1)
break;
else
flag=0;
if(page[i]==fr[j])
flag=1;
printf(" \t%d:\t",page[i]);
break;
if(flag==0)
fr[p]=page[i];
printf(" \t%d:\t",page[i]);
p++;
printf(" %d ",fr[j]);
printf("\n");
p=0;
flag=0;
if(page[i]==fr[j])
flag=1;
break;
if(flag==0)
p++;
for(j=1; j<=nf; j++)
if(fr[j]==page[k])
u[j]=k;
break;
else
u[j]=21;
t[j]=u[j];
if(t[j]<t[k])
t1=t[j];
t[j]=t[k];
t[k]=t1;
if(t[1]==u[j])
{
fr[j]=page[i];
u[j]=i;
printf("page fault\t");
else
printf(" \t");
printf("%d:\t",page[i]);
printf(" %d ",fr[j]);
printf("\n");
// getch();
int k;
if(a[k]==-1)
return 0;
return 1;
}
Output:-
enter the number of frames:5
1: 1 -1 -1 -1 -1
2: 1 2 -1 -1 -1
#include <stdio.h>
int main()
scanf("%d", &p);
running[i] = 1;
count++;
scanf("%d", &r);
scanf("%d", &maxres[i]);
scanf("%d", &curr[i][j]);
}
scanf("%d", &maxclaim[i][j]);
printf("\t%d", maxres[i]);
printf("\t%d", curr[i][j]);
printf("\n");
printf("\t%d", maxclaim[i][j]);
printf("\n");
}
alloc[j] += curr[i][j];
printf("\nAllocated resources:");
printf("\t%d", alloc[i]);
printf("\nAvailable resources:");
printf("\t%d", avl[i]);
printf("\n");
while (count != 0) {
safe = 0;
if (running[i]) {
exec = 1;
break;
if (exec) {
running[i] = 0;
count--;
safe = 1;
avl[j] += curr[i][j];
break;
if (!safe) {
break;
} else {
printf("\nAvailable vector:");
printf("\t%d", avl[i]);
printf("\n");
}
}
Output:-
2011
0121
4003
0210
1030
3214
0252
5105
1530
3033
2 0 1 1
0 1 2 1
4 0 0 3
0 2 1 0
1 0 3 0
3 2 1 4
0 2 5 2
5 1 0 5
1 5 3 0
3 0 3 3
Allocated resources: 7 3 7 5
Available resources: 1 2 2 2
Process3 is executing
Available vector: 5 2 2 5
Process1 is executing
Available vector: 7 2 3 6
Process2 is executing
Available vector: 7 3 5 7
Process4 is executing
Available vector: 7 5 6 7
Process5 is executing
Available vector: 8 5 9 7