1)
Simulate the following CPU scheduling algorithms
a)
FCFS
b)
SJF
c)
Priority
d)
Round Robin
a)
FCFS:
A program to simulate the FCFS CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
charpn[10][10],t[10];
intarr[10],bur[10],star[10],finish[10],tat[10],wt[10],i,j,n,temp;
inttotwt=0,tottat=0;
//clrscr();
printf("Enter
the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter
the Process Name, Arrival Time & Burst Time:");
scanf("%s%d%d",&pn[i],&arr[i],&bur[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
temp=bur[i];
bur[i]=bur[j];
bur[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
star[i]=arr[i];
else
star[i]=finish[i-1];
wt[i]=star[i]-arr[i];
finish[i]=star[i]+bur[i];
tat[i]=finish[i]-arr[i];
}
printf("\nPNameArrtimeBurtimeWaitTime
Start TAT Finish");
for(i=0;i<n;i++)
{
printf("\n%s\t%3d\t%3d\t%3d\t%3d\t%6d\t%6d",pn[i],arr[i],bur[i],wt[i],star[i],tat[i],finish[i]);
totwt+=wt[i];
tottat+=tat[i];
}
printf("\nAverage
Waiting time:%f",(float)totwt/n);
printf("\nAverage
Turn Around Time:%f",(float)tottat/n);
getch();
return 0;
}
OUTPUT:
Input:
Enter the number
of processes: 3
Enter the
Process Name, Arrival Time & Burst Time: p1 2 4
Enter the
Process Name, Arrival Time & Burst Time: p2 3 5
Enter the
Process Name, Arrival Time & Burst Time: p3 1 6
Output:
PNameArrtimeBurtimeWaitTimeSrart
TAT Finish
p3 1 6 0 1 6 7
p1 2 4 5 7 9 11
p2 3 5 8 11 13
16
Average Waiting
Time: 4.3333333
Average Turn
Around Time: 9.33333333
A
program to simulate the SJF CPU scheduling algorithm
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int
et[20],at[10],n,i,j,temp,st[10],ft[10],wt[10],ta[10];
inttotwt=0,totta=0;
floatawt,ata;
charpn[10][10],t[10];
clrscr();
printf("Enter
the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter
process name, arrival time & execution time:");
flushall();
scanf("%s%d%d",pn[i],&at[i],&et[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(et[i]<et[j])
{
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
st[i]=at[i];
else
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],wt[i],ta[i]);
printf("\nAverage
waiting time is:%f",awt);
printf("\nAverageturnaroundtime
is:%f",ata);
getch();
}
OUTPUT:
Input:
Enter the number
of processes: 3
Enter the
Process Name, Arrival Time & Burst Time: 1 4 6
Enter the
Process Name, Arrival Time & Burst Time: 2 5 15
Enter the
Process Name, Arrival Time & Burst Time: 3 6 11
Output:
Pnamearrivaltimeexecutiontimewaitingtimetatime
1 4 6 0 6
3 6 11 4 15
2 5 15 16 31
Average Waiting
Time: 6.6667
Average Turn
Around Time: 17.3333
c)
Priority:
AIM: A
program to simulate the priority CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int
et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];
inttotwt=0,totta=0;
floatawt,ata;
charpn[10][10],t[10];
clrscr();
printf("Enter
the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter
process name,arrivaltime,execution time & priority:");
flushall();
scanf("%s%d%d%d",pn[i],&at[i],&et[i],&p[i]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=at[i];
at[i]=at[j];
at[j]=temp;
temp=et[i];
et[i]=et[j];
et[j]=temp;
strcpy(t,pn[i]);
strcpy(pn[i],pn[j]);
strcpy(pn[j],t);
}
}
for(i=0;i<n;i++)
{
if(i==0)
{
st[i]=at[i];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
else
{
st[i]=ft[i-1];
wt[i]=st[i]-at[i];
ft[i]=st[i]+et[i];
ta[i]=ft[i]-at[i];
}
totwt+=wt[i];
totta+=ta[i];
}
awt=(float)totwt/n;
ata=(float)totta/n;
printf("\nPname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime");
for(i=0;i<n;i++)
printf("\n%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d",pn[i],at[i],et[i],p[i],wt[i],ta[i]);
printf("\nAverage
waiting time is:%f",awt);
printf("\nAverageturnaroundtime
is:%f",ata);
getch();
}
OUTPUT:
Input:
Enter the number
of processes: 3
Enter the
Process Name, Arrival Time, execution time & priority: 1 2 3 1
Enter the
Process Name, Arrival Time, execution time & priority: 2 4 5 2
Enter the
Process Name, Arrival Time, execution time & priority: 3 5 6 3
Output:
Pnamearrivaltimeexecutiontime
priority waitingtimetatime
1 2 3 1 0 3
2 4 5 2 1 6
3 5 6 3 5 11
Average Waiting
Time: 2.0000
Average Turn
Around Time: 6.6667
d)
Round Robin:
AIM: A
program to simulate the Round Robin CPU scheduling algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
et[30],ts,n,i,x=0,tot=0;
charpn[10][10];
clrscr();
printf("Enter
the no of processes:");
scanf("%d",&n);
printf("Enter
the time quantum:");
scanf("%d",&ts);
for(i=0;i<n;i++)
{
printf("enter
process name & estimated time:");
scanf("%s
%d",pn[i],&et[i]);
}
printf("The
processes are:");
for(i=0;i<n;i++)
printf("process
%d: %s\n",i+1,pn[i]);
for(i=0;i<n;i++)
tot=tot+et[i];
while(x!=tot)
{
for(i=0;i<n;i++)
{
if(et[i]>ts)
{
x=x+ts;
printf("\n
%s -> %d",pn[i],ts);
et[i]=et[i]-ts;
}
else
if((et[i]<=ts)&&et[i]!=0)
{
x=x+et[i];
printf("\n
%s -> %d",pn[i],et[i]);
et[i]=0;}
}
}
printf("\n
Total Estimated Time:%d",x);
getch();
}
OUTPUT:
Input:
Enter the no of
processes: 2
Enter the time
quantum: 3
Enter the
process name & estimated time: p1 12
Enter the
process name & estimated time: p2 15
Output:
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p1 -> 3
p2 -> 3
p2 -> 3
Total Estimated
Time: 27
2)
Simulate the MVT and MFT.
MVT:
AIM: A program
to simulate the MVT.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
m=0,m1=0,m2=0,p,count=0,i;
clrscr();
printf("enter
the memory capacity:");
scanf("%d",&m);
printf("enter
the no of processes:");
scanf("%d",&p);
for(i=0;i<p;i++)
{
printf("\nenter
memory req for process%d: ",i+1);
scanf("%d",&m1);
count=count+m1;
if(m1<=m)
{
if(count==m)
printf("there
is no further memory remaining:");
printf("the
memory allocated for process%d is: %d ",i+1,m);
m2=m-m1;
printf("\nremaining
memory is: %d",m2);
m=m2;
}
}
else
{
printf("memory
is not allocated for process%d",i+1);
}
printf("\nexternal
fragmentation for this process is:%d",m2);
}
getch();
}
OUTPUT:
Input:
Enter the memory
capacity: 80
Enter no of
processes: 2
Enter memory req
for process1: 23
Output:
The memory
allocated for process1 is: 80
Remaining memory
is: 57
External
fragmentation for this process is: 57
Enter memory req
for process2: 52
The memory
allocated for process2 is: 57
Remaining memory
is: 5
External
fragmentation for this process is: 5
MFT:
AIM: A
Program to simulate the MFT
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int m,p,s,p1;
int
m1[4],i,f,f1=0,f2=0,fra1,fra2,s1,pos;
clrscr();
printf("Enter
the memory size:");
scanf("%d",&m);
printf("Enter
the no of partitions:");
scanf("%d",&p);
s=m/p;
printf("Each
partn size is:%d",s);
printf("\nEnter
the no of processes:");
scanf("%d",&p1);
pos=m;
for(i=0;i<p1;i++)
{
if(pos<s)
{
printf("\nThere
is no further memory for process%d",i+1);
m1[i]=0;
break;
}
else
{
printf("\nEnter
the memory req for process%d:",i+1);
scanf("%d",&m1[i]);
if(m1[i]<=s)
{
printf("\nProcess
is allocated in partition%d",i+1);
fra1=s-m1[i];
printf("\nInternal
fragmentation for process is:%d",fra1);
f1=f1+fra1;
pos=pos-s;
}
else
{
printf("\nProcess
not allocated in partition%d",i+1);
s1=m1[i];
while(s1>s)
{
s1=s1-s;
pos=pos-s;
}
pos=pos-s;
fra2=s-s1;
f2=f2+fra2;
printf("\nExternal
Fragmentation for this process is:%d",fra2);
}
}
}
printf("\nProcess\tallocatedmemory");
for(i=0;i<p1;i++)
printf("\n%5d\t%5d",i+1,m1[i]);
f=f1+f2;
printf("\nThe
tot no of fragmentation is:%d",f);
getch();
return 0;
}
OUTPUT:
Input:
Enter the memory
size: 80
Enter the no of
partitions: 4
Each partition
size: 20
Enter the number
of processes: 2
Enter the memory
req for process1: 18
Output:
Process1 is
allocated in partn1
Internal
fragmentation for process1 is: 2
Enter the memory
req for process2: 22
Process2 is not
allocated in partn2
External
fragmentation for process2 is: 18
Process memory
allocated
1 20 18
2 20 22
The tot no of
fragmentation is: 20
3)
Simulate Bankers Algorithm for Deadlock Avoidance.
AIM: A
program to simulate the Bankers Algorithm for Deadlock Avoidance.
PROGRAM:
//Bankers
algorithm for deadlock avoidance.
#include<stdio.h>
#include<conio.h>
void main()
{
intn,r,i,j,k,p,u=0,s=0,m;
int
block[10],run[10],active[10],newreq[10];
int max[10][10],resalloc[10][10],resreq[10][10];
inttotalloc[10],totext[10],simalloc[10];
clrscr();
printf("Enter
the no of processes:");
scanf("%d",&n);
printf("Enter
the no of resource classes:");
scanf("%d",&r);
printf("Enter
the total existed resource in each class:");
for(k=1;k<=r;k++)
scanf("%d",&totext[k]);
printf("Enter
the allocated resources:");
for(i=1;i<=n;i++)
for(k=1;k<=r;k++)
scanf("%d",&resalloc);
printf("Enter
the process making the new request:");
scanf("%d",&p);
printf("Enter
the requested resource:");
for(k=1;k<=r;k++)
scanf("%d",&newreq[k]);
printf("Enter
the process which are n blocked or running:");
for(i=1;i<=n;i++)
{
if(i!=p)
{
printf("process
%d:\n",i+1);
scanf("%d%d",&block[i],&run[i]);
}
}
block[p]=0;
run[p]=0;
for(k=1;k<=r;k++)
{
j=0;
for(i=1;i<=n;i++)
{
totalloc[k]=j+resalloc[i][k];
j=totalloc[k];
}
}
for(i=1;i<=n;i++)
{
if(block[i]==1||run[i]==1)
active[i]=1;
else
active[i]=0;
}
for(k=1;k<=r;k++)
{
resalloc[p][k]+=newreq[k];
totalloc[k]+=newreq[k];
}
for(k=1;k<=r;k++)
{
if(totext[k]-totalloc[k]<0)
{
u=1;break;
}
}
if(u==0)
{
for(k=1;k<=r;k++)
simalloc[k]=totalloc[k];
for(s=1;s<=n;s++)
for(i=1;i<=n;i++)
{
if(active[i]==1)
{
j=0;
for(k=1;k<=r;k++)
{
if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{
j=1;break;
}
}
}
if(j==0)
{
active[i]=0;
for(k=1;k<=r;k++)
simalloc[k]=resalloc[i][k];
}
}
m=0;
for(k=1;k<=r;k++)
resreq[p][k]=newreq[k];
printf("Deadlock
willn't occur");
}
else
{
for(k=1;k<=r;k++)
{
resalloc[p][k]=newreq[k];
totalloc[k]=newreq[k];
}
printf("Deadlock
will occur");
}
getch();
}
OUTPUT:
Input:
Enter the no of
resources: 4
Enter the no of
resource classes: 3
Enter the total
existed resources in each class: 3 2 2
Enter the
allocated resources: 1 0 0 5 1 1 2 1 1 0 0 2
Enter the
process making the new request: 2
Enter the
requested resource: 1 1 2
Enter the
processes which are n blocked or running:
Process 1: 1 2
Process 3: 1 0
Process 4: 1 0
Output:
Deadlock will
occur
4)
Simulate Bankers Algorithm for Deadlock Prevention.
AIM: A
program to simulate Bankers Algorithm for Deadlock Prevention.
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
cl[10][10],al[10][10],av[10],i,j,k,m,n,c,ne[10][10],flag=0;
clrscr();
printf("\nEnter
the matrix");
scanf("%d
%d",&m,&n);
printf("\nEnter
the claim matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&cl[i][j]);
}
}
printf("\nEnter
allocated matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&al[i][j]);
}
}
printf("\nThe
need matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ne[i][j]=cl[i][j]-al[i][j];
printf("\t%d",ne[i][j]);
}
printf("\n");
}
printf("\nEnteravaliable
matrix");
for(i=0;i<3;i++)
scanf("%d",av[i]);
printf("Claim
matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",cl[i][j]);
}
printf("\n");
}
printf("\n
allocated matrix:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\t%d",al[i][j]);
}
printf("\n");
}
printf("
available matrix:\n");
for(i=0;i<3;i++)
{
printf("\t%d",av[i]);
}
for(k=0;k<m;k++)
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(av[j]>=ne[i][j])
flag=1;
else
break;
if(flag==1&&
j==n-1)
goto a;
}
}
a: if(flag==0)
{
printf("unsafestate");
}
if(flag==1)
{
flag=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;i++)
{
av[j]+=al[i][j];
al[i][j]=1;
}
}
printf("\n
safe state");
for(i=0;i<n;i++)
printf("\t
available matrix:%d",av[i]);
}
getch();
}
OUTPUT:
Input:
Enter the claim
matrix:3 2 2 6 1 3 3 1 4 4 2 2
Enter allocated
matrix:1 0 0 5 1 1 2 1 1 0 0 2
The need matrix:
2 2 2
1 0 2
1 0 3
4 2 0
Enter available
matrix1 1 2
Output:
Claim matrix:
3 2 2
6 1 3
3 1 4
4 2 2
Allocated
matrix:
1 0 0
5 1 1
2 1 1
0 0 2
Available matrix:
1 1 2
Safe State
5)
Simulate all Page Replacement Algorithms
a) FIFO
b) LRU
a) FIFO:
AIM: A
program to simulate FIFO Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
a[5],b[20],n,p=0,q=0,m=0,h,k,i,q1=1;
char f='F';
clrscr();
printf("Enter
the Number of Pages:");
scanf("%d",&n);
printf("Enter
%d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
printf("\nNo
of faults:%d",m);
getch();
}
OUTPUT:
Input:
Enter the Number
of Pages: 12
Enter 12 Page
Numbers:
2 3 2 1 5 2 4 5
3 2 5 2
Output:
2 2-> F
3 23-> F
2 23
1 231-> F
5 531-> F
2 521-> F
4 524-> F
5 524
3 324-> F
2 324
5 354-> F
2 352-> F
No of faults: 9
b)
LRU:
AIM: A
program to simulate LRU Page Replacement Algorithm
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
g=0,a[5],b[20],p=0,q=0,m=0,h,k,i,q1=1,j,u,n;
char f='F';
clrscr();
printf("Enter
the number of pages:");
scanf("%d",&n);
printf("Enter
%d Page Numbers:",n);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
{if(p==0)
{
if(q>=3)
q=0;
a[q]=b[i];
q++;
if(q1<3)
{
q1=q;
//g=1;
}
}
printf("\n%d",b[i]);
printf("\t");
for(h=0;h<q1;h++)
printf("%d",a[h]);
if((p==0)&&(q<=3))
{
printf("-->%c",f);
m++;
}
p=0;
g=0;
if(q1==3)
{
for(k=0;k<q1;k++)
{
if(b[i+1]==a[k])
p=1;
}
for(j=0;j<q1;j++)
{
u=0;
k=i;
while(k>=(i-1)&&(k>=0))
{
if(b[k]==a[j])
u++;
k--;
}
if(u==0)
q=j;
}
}
else
{
for(k=0;k<q;k++)
{
if(b[i+1]==a[k])
p=1;
}
}
}
printf("\nNo
of faults:%d",m);
getch();
}
OUTPUT:
Input:
Enter the Number
of Pages: 12
Enter 12 Page
Numbers:
2 3 2 1 5 2 4 5
3 2 5 2
Output:
2 2-> F
3 23-> F
2 23
1 231-> F
5 251-> F
2 251
4 254-> F
5 254
3 354-> F
2 352-> F
5 352
2 352
No of faults: 7
6:
Optimal page replacement algorithm
#include<stdio.h>
intfr[3];
intmain()
{
void
display();
int
p[12]={2,3,2,1,5,2,4,5,3,2,5,2},i,j,fs[3];
intmax,found=0,1g[3],index,1,flag1=0,flag2=0,pf=0,frsize=3;
for(i=0;i<3;i++)
{
fr[i]=-1;
}
for(j=0;j<12;j++)
{
flag1=0;flag2=0;
for(i=0,i<3;i++)
{
if(fr[i]==p[j])
{
flag1=2;
flag2=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<3;i++)
{
if(fr[i]==-1)
{
fr[i]=p[j];
flag2=1;
break;
}
}
}
if(flag2==0)
{
for(i=0;i<3;i++)
{
lg[i]=0;
}
for(i=0;i<frsize;i++)
{
for(k=j+1;k<12;k++)
{
if(fr[i]==p[k])
{
lg[i]=k-j;
break;
}
}
}
found=0;
for(i=0;i<frsize;i++)
{
if(lg[i]==0)
{
index=i;
found=1;
break;
}
}
if(found==0)
{
max=lg[0];
index=0;
for(i=1;i<frsize;i++)
{
if(max<lg[i])
{
max=lg[i];
index=i;
}
}
}
fr[index]=p[j];
pf++;
}
display();
}
printf(“\n no of pages faults:%d”,pf);
return 0;
}
void display()
{
int
i;
printf(“\n”);
for(i=0;i<3;i++)
printf(“\t%d”,fr[i]);
}
output:
-2 -1 -1
2 3 -1
2 3 -1
2 3 1
2 3 5
2 3 5
4 3 5
4 3 5
4 3 5
2 3 5
2 3 5
2 3 5
no of pages faults:3
LFU page replacement
LFU page replacement
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}
7:PROGRAM
FOR DEADLOCK DETECTION ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
{
int
found,flag,l,p[4][5],tp,c[4][5],i,j,k=1,m[5],r[5],a[5],temp[5],sum=0;
clrscr();
printf("enter total no of
processes");
scanf("%d",&tp);
printf("enter clain
matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&c[i][j]);
}
printf("enter allocation
matrix");
for(i=1;i<=4;i++)
for(j=1;j<=5;j++)
{
scanf("%d",&p[i][j]);
}
printf("enter resource vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&r[i]);
}
printf("enter availability
vector:\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
temp[i]=a[i];
}
for(i=1;i<=4;i++)
{
sum=0;
for(j=1;j<=5;j++)
{
sum+=p[i][j];
}
if(sum==0)
{
m[k]=i;
k++;
}
}
for(i=1;i<=4;i++)
{
for(l=1;l<k;l++)
if(i!=m[l])
{
flag=1;
for(j=1;j<=5;j++)
if(c[i][j]>temp[j])
{
flag=0;
break;
}
}
if(flag==1)
{
m[k]=i;
k++;
for(j=1;j<=5;j++)
temp[j]+=p[i][j];
}
}
printf("deadlock causing
processes are:");
for(j=1;j<=tp;j++)
{
found=0;
for(i=1;i<k;i++)
{
if(j==m[i])
found=1;
}
if(found==0)
printf("%d\t",j);
}
getch();
}
INPUT:enter total no. of processes : 4
enter claim matrix :
0 1 0 0 1
0 0 1 0 1
0 0 0 0 1
1 0 1 0 1
enter allocation matrix :
1 0 1 1 0
1 1 0 0 0
0 0 0 1 0
0 0 0 0 0
enter resource vector :
2 1 1 2 1
enter the availability vector :
0 0 0 0 1
deadlock causing processes are : 1 2
8:a)PROGRAM
TO IMPLEMENT SEQUENTIAL
FILEALLOCATION METHOD:
#include<stdio.h>
#include<conio.h>
main()
{
int n ,i,j,b[20],sb[20],t[20],x,c[20][20];
clrscr();
printf(“enter no of files”);
scanf(“%d”,&b[i]);
printf(“enter
the starting block of file%d”,i+1);
scanf(“%d”,&sb[i]);
t[i]=sb[i];
c[i][j]=sb[i]++;
}
Printf(“filename
\t Startblock \t length\n”);
for(i=0;i<n;i++)
printf(“%d\t
%d\t%d\n”,i+1,t[i],b[i]);
printf(“enter
file name”);
scanf(“%d”,&x);
printf(“filename
is %d”,x);
printf(“length is %d”,b[x-1]);
printf(“blocks
occupied”);
for(i=0;i<b[x-1];i++)
printf(“%4d”c[x-1][i]);
getch();
}
OUTPUT:
Enter no of
files:2
Enter no of
files occupied by file1 4
Enter the
starting block of fie1 2
Enter no of
files occupied by file2 10
Enter the
starting block of fie2 5
Filename
startblock length
1 2 4
2 5 10
Enter
filename:pavan
Filename
is:12803 length is :0 blocks occupied
8:b)PROGRAM
TO IMPLEMENT INDEXED
FILEALLOCATION METHOD:
#include<stdio.h>
#include<conio.h>
main()
{
int n
,i,j,m[20],sb[20],s[20],x,b[20][20];
clrscr();
printf(“enter no of files”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter
the starting block and size of
file%d”,i+1);
scanf(“%d%d”,&sb[i],&s[i]);
printf(“enter
blocks occupied by file%d”,i+1);
scanf(“%d”,&m[i]);
printf(“enter
blocks of file%d”,i+1);
for(j=0;j<m[i];j++)
scanf(“%d”,&b[i][j]);
}
printf(“file \t
index \t length\n”);
for(i=0;i<n;i++)
printf(“%d\t
%d\t%d\n”,i+1,sb[i],m[i]);
printf(“enter
file name”);
scanf(“%d”,&x);
printf(“filename
is %d”,x);
i=x-1;
printf(“index is %d”,sb[i]);
printf(“blocks
occupied”);
for(j=0;j<m[i];j++)
printf(“%3d”b[i][j]);
getch();
}
output:
enter no of
files:2
enter the
starting block and size of fie1: 2 5
enter blocks
occupied by file1: 10
enter blocks of fie1: 3
2 5 4 6 7 2 6 4
7
enter the
starting block and size of file2: 3 4
enter blocks
occupied by file2: 5
enter blocks of
file2: 2 3 4 5 6
file index length
1 2 10
2 3 5
enter
filename:pavan
filename
is:12803
index is :0
8:c)PROGRAM
TO IMPLEMENT LINKED FILE
ALLOCATION METHOD
#include<stdio.h>
#include<conio.h>
Struct file
{
char fname[10];
int
start,size,block[10];
}f[10];
main()
{
int i,j,n;
clrscr();
printf(“enter no
of files”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“enter
filename”);
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%d%d”,f[i].fname,f[i].start,f[i].size);
for(j=1;j<=f[i].size-1;j++)
printf(“%dà ”,f[i].block[j]);
printf(“%d”,f[i].block[j]);
printf(“\n”);
}
getch();
}
OUTPUT:
Enter no files:2
Enter filename:pavan
Enter starting block:20
Enter no of blocks:6
Enter block numbers:4
12
15
45
32
25
Enter filename: kumar
Enter starting block:12
Enter no of blocks:5
Enter block numbers:6
5
4
3
2
File start size block
Pavan
20 6 4Ã 12Ã 15Ã 45Ã 32Ã 25
Kumar
12 5 6Ã 5Ã 4Ã 3Ã 2
LFU page replacement program
#include<stdio.h>
int main()
{
int total_frames, total_pages, hit = 0;
int pages[25], frame[10], arr[25], time[25];
int m, n, page, flag, k, minimum_time, temp;
printf("Enter Total Number of Pages:\t");
scanf("%d", &total_pages);
printf("Enter Total Number of Frames:\t");
scanf("%d", &total_frames);
for(m = 0; m < total_frames; m++)
{
frame[m] = -1;
}
for(m = 0; m < 25; m++)
{
arr[m] = 0;
}
printf("Enter Values of Reference String\n");
for(m = 0; m < total_pages; m++)
{
printf("Enter Value No.[%d]:\t", m + 1);
scanf("%d", &pages[m]);
}
printf("\n");
for(m = 0; m < total_pages; m++)
{
arr[pages[m]]++;
time[pages[m]] = m;
flag = 1;
k = frame[0];
for(n = 0; n < total_frames; n++)
{
if(frame[n] == -1 || frame[n] == pages[m])
{
if(frame[n] != -1)
{
hit++;
}
flag = 0;
frame[n] = pages[m];
break;
}
if(arr[k] > arr[frame[n]])
{
k = frame[n];
}
}
if(flag)
{
minimum_time = 25;
for(n = 0; n < total_frames; n++)
{
if(arr[frame[n]] == arr[k] && time[frame[n]] < minimum_time)
{
temp = n;
minimum_time = time[frame[n]];
}
}
arr[frame[temp]] = 0;
frame[temp] = pages[m];
}
for(n = 0; n < total_frames; n++)
{
printf("%d\t", frame[n]);
}
printf("\n");
}
printf("Page Hit:\t%d\n", hit);
return 0;
}
|
No comments:
Post a Comment