Os Lab
Os Lab
ALGORITHM
1. Start the process
2. Declare the array size
3. Get the number of elements to be inserted
4. Select the process which have shortest burst will execute first
5. If two process have same burst length then FCFS scheduling algorithm used
6. Make the average waiting the length of next process
7. Start with the first process from its selection as above and let other process to be in
queue
6. Calculate the total number of burst time
7. Display the values
8. Stop the process
PROGRAM:
#include<stdio.h>
int main()
{
int n,j,temp,temp1,temp2,pr[10],b[10],t[10],w[10],p[10],i;
float att=0,awt=0;
for(i=0;i<10;i++)
{
b[i]=0;w[i]=0;
}
OUTPUT:
Enter the number of process 5
waiting time
0
2
6
11
17
AIM:
3.b)To implement the program for Round Robin scheduling algorithm
ALGORITHM
PROGRAM:
#include<stdio.h>
#include<conio.h>
int z[10],b[10],n,m[50],r,q,e=0,avg=0,i,j;
float f;
main()
{
clrscr();
printf("\n\tJOB SCHEDULING ALGORITHM[RR]");
printf("\n\t*******************************************************\n");
printf("\nEnter how many jobs:");
scanf("%d",&n);
printf("\nEnter burst time for corresponding job...\n");
for(i=1;i<=n;i++)
{
printf("\nProcess %d: ",i);
scanf("%d",&b[i]); z[i]=b[i];
}
printf("\nENTER THE TIME SLICE VALUE:");
scanf("%d",&q);
rr();
average();
getch();
return 0;
}
rr()
{
int max=0;
max=b[1];
for(j=1;j<=n;j++)
if(max<=b[j])
max=b[j];
if((max%q)==0)
r=(max/q);
else
r=(max/q)+1;
for(i=1;i<=r;i++)
{
printf("\nround %d",i);
for(j=1;j<=n;j++)
{
if(b[j]>0)
{
b[j]=b[j]-q;
if(b[j]<=0)
{
b[j]=0;
printf("\nprocess %d is completed",j);
}
else
printf("\nprocess %d remaining time is %d",j,b[j]);
}
}
delay(1000);
}
return 0;
}
average()
{
for(i=1;i<=n;i++)
{
e=0;
for(j=1;j<=r;j++)
{
if(z[i]!=0)
{
if(z[i]>=q)
{
m[i+e]=q; z[i]-=q;
}
else
{
m[i+e]=z[i]; z[i]=0;
}
}
else
m[i+e]=0;
e=e+n;
}
}
for(i=2;i<=n;i++)
for(j=1;j<=i-1;j++)
avg=avg+m[j];
for(i=n+1;i<=r*n;i++)
{
if(m[i]!=0)
{
for(j=i-(n-1);j<=i-1;j++)
avg=m[j]+avg;
}
}
f=avg/n;
printf("\nTOTAL WATING:%d",avg);
printf("\n\nAVERAGE WAITING TIME:%f\n",f);
for(i=1;i<=r*n;i++)
{ if(m[i]!=0)
if(i%n==0){
printf("P%d",(i%n)+(n));
else
printf("P%d",(i%n));
for(j=1;j<=m[i];j++)
printf("%c",22);
}
printf("\n");
getch();
return 0;
}
AIM
3.c)To write a program to implement the FCFS scheduling algorithm
ALGORITHM
1. Start the process
2. Declare the array size
PROGRAM:
#include<stdio.h>
main()
{
int n,a[10],b[10],t[10],w[10],g[10],i,m;
float att=0,awt=0;
for(i=0;i<10;i++)
{
a[i]=0; b[i]=0; w[i]=0; g[i]=0;
}
printf("enter the number of process");
scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
scanf("%d",&b[i]);
printf("\nenter the arrival times");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
g[0]=0;
for(i=0;i<10;i++)
g[i+1]=g[i]+b[i];
for(i=0;i<n;i++)
{
w[i]=g[i]-a[i];
t[i]=g[i+1]-a[i];
awt=awt+w[i];
att=att+t[i];
}
awt =awt/n;
att=att/n;
printf("\n\tprocess\twaiting time\tturn arround time\n");
for(i=0;i<n;i++)
{
printf("\tp%d\t\t%d\t\t%d\n",i,w[i],t[i]);
}
printf("the average waiting time is %f\n",awt);
printf("the average turn around time is %f\n",att);
}
OUTPUT:
Enter the number of process 4
Enter the burst times
4 9 8 3
Enter the arrival times
0 2 4 3
process
waiting time turn around time
p0
0
4
p1
2
11
p2
9
17
p3
18
21
The average waiting time is 7.250000
Tthe average turnaround time is 13.250000
PROGRAM:
#include
main()
{
int i,j,bt[10],n,pt[10],wt[10],tt[10],t,k,l,w1=0,t1=0,b=0,p=0;
float at,aw;
clrscr();
printf("enter no of jobs");
scanf("%d",&n);
printf("enter burst time");
for(i=0;i<n;i++)
{
scanf("%d",&b);
bt[i]=b;
}
printf("enter priority values");
for(i=0;i<n;i++)
{
scanf("%d",&p);
pt[i]=p;
}
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(pt[j]<pt[j+1])
{
t=pt[j];
pt[j]=pt[j+1];
pt[j+1]=t;
k=bt[j];
bt[j]=bt[j+1];
bt[j+1]=k;
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=bt[i]+wt[i];
tt[i]=bt[i]+wt[i];
w1=w1+wt[i];
t1=t1+tt[i];
}
aw=w1/n;
at=t1/n;
printf("\nbt\tprority\twt\ttt\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\n",bt[i],pt[i],wt[i],tt[i]);
printf("aw=%f\nat=%f",aw,at);
getch();
}
Output:
PROGRAM:
#include
#include
main()
{
int f[50],i,st,j,len,c,k;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
X:
printf("\n Enter the starting block & length of file");
scanf("%d%d",&st,&len);
for(j=st;j<(st+len);j++)
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("Block already allocated");
break;
}
if(j==(st+len))
printf("\n the file is allocated to disk");
printf("\n if u want to enter more files?(y-1/n-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch();
}
Output:
int f[50],p,i,j,k,a,st,len,n,c;
clrscr();
for(i=0;i<50;i++)
f[i]=0;
printf("Enter how many blocks that are already allocated");
scanf("%d",&p);
printf("\nEnter the blocks no.s that are already allocated");
for(i=0;i<p;i++)
{
scanf("%d",&a);
f[a]=1;
}
X:
printf("Enter the starting index block & length");
scanf("%d%d",&st,&len);
k=len;
for(j=st;j<(k+st);j++)
{
if(f[j]==0)
{
f[j]=1;
printf("\n%d->%d",j,f[j]);
}
else
{
printf("\n %d->file is already allocated",j);
k++;
}
}
printf("\n If u want to enter one more file? (yes-1/no-0)");
scanf("%d",&c);
if(c==1)
goto X;
else
exit();
getch( );
}
Output:
goto x;
}
for(i=0;i<n;i++)
scanf("%d",&inde[i]);
for(i=0;i<n;i++)
if(f[inde[i]]==1)
{
printf("Block already allocated");
goto x;
}
for(j=0;j<n;j++)
f[inde[j]]=1;
printf("\n allocated");
printf("\n file indexed");
for(k=0;k<n;k++)
printf("\n %d->%d:%d",p,inde[k],f[inde[k]]);
printf(" Enter 1 to enter more files and 0 to exit\t");
scanf("%d",&c);
if(c==1)
goto x;
else
exit();
getch();
}
Output:
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n,r;
void input();
void show();
void cal();
int main()
{
int i,j;
printf("********** Baner's Algo ************\n");
input();
show();
cal();
getch();
return 0;
}
void input()
{
int i,j;
printf("Enter the no of Processes\t");
scanf("%d",&n);
printf("Enter the no of resources instances\t");
scanf("%d",&r);
printf("Enter the Max Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
printf("\n");
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
printf("P%d->",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
for(i=0;i<n;i++)
{
if(finish[i]==1)
{
c1++;
}
else
{
printf("P%d->",i);
}
}
if(c1==n)
{
printf("\n The system is in safe state");
}
else
{
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
Output
{
for(j=0;j<r;j++)
{
scanf("%d",&alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for(j=0;j<r;j++)
{
scanf("%d",&avail[j]);
}
}
void show()
{
int i,j;
printf("Process\t Allocation\t Max\t Available\t");
for(i=0;i<n;i++)
{
printf("\nP%d\t ",i+1);
for(j=0;j<r;j++)
{
printf("%d ",alloc[i][j]);
}
printf("\t");
for(j=0;j<r;j++)
{
printf("%d ",max[i][j]);
}
printf("\t");
if(i==0)
{
for(j=0;j<r;j++)
printf("%d ",avail[j]);
}
}
}
void cal()
{
int finish[100],temp,need[100][100],flag=1,k,c1=0;
int dead[100];
int safe[100];
int i,j;
for(i=0;i<n;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<n;i++)
{
for(j=0;j<r;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(flag)
{
flag=0;
for(i=0;i<n;i++)
{
int c=0;
for(j=0;j<r;j++)
{
if((finish[i]==0)&&(need[i][j]<=avail[j]))
{
c++;
if(c==r)
{
for(k=0;k<r;k++)
{
avail[k]+=alloc[i][j];
finish[i]=1;
flag=1;
}
//printf("\nP%d",i);
if(finish[i]==1)
{
i=n;
}
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<n;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
printf("\n\nSystem is in Deadlock and the Deadlock process are\n");
for(i=0;i<n;i++)
{
printf("P%d\t",dead[i]);
}
}
else
{
printf("\nNo Deadlock Occur");
}
}
Output