0% found this document useful (0 votes)
187 views

Os Lab

The document discusses implementing various CPU scheduling algorithms and file allocation strategies. It provides programs for shortest job first, round robin, first come first serve, and priority CPU scheduling algorithms. It also provides programs for sequential, linked, and indexed file allocation strategies. The banker's algorithm program for deadlock avoidance is outlined with steps to declare arrays, get number of elements, and stop the program.

Uploaded by

guna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views

Os Lab

The document discusses implementing various CPU scheduling algorithms and file allocation strategies. It provides programs for shortest job first, round robin, first come first serve, and priority CPU scheduling algorithms. It also provides programs for sequential, linked, and indexed file allocation strategies. The banker's algorithm program for deadlock avoidance is outlined with steps to declare arrays, get number of elements, and stop the program.

Uploaded by

guna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

3.

Implement the following CPU scheduling algorithms


AIM:
3.a)To implement the c program for shortest job first scheduling algorithm

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;
}

printf("enter the number of process");


scanf("%d",&n);
printf("enter the burst times");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
p[i]=i;
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(b[i]>b[j])
{
temp=b[i];
temp1=p[i];
b[i]=b[j];
p[i]=p[j];
b[j]=temp;
p[j]=temp1;
}
}
}
w[0]=0;
for(i=0;i<n;i++)
w[i+1]=w[i]+b[i];
for(i=0;i<n;i++)
{
t[i]=w[i]+b[i];
awt=awt+w[i];
att=att+t[i];
}
awt=awt/n;
att=att/n;
printf("\n\t process \t waiting time \t turn around time \n");
for(i=0;i<n;i++)
printf("\t p[%d] \t %d \t\t %d \n",p[i],w[i],t[i]);
printf("the average waitingtimeis %f\n",awt);
printf("the average turnaround time is %f\n",att);
return 1;
}

OUTPUT:
Enter the number of process 5

Enter the burst times


2 4 5 6 8
process
p[0]
p[1]
p[2]
p[3]
p[4]

waiting time
0
2
6
11
17

turn around time


2
6
11
17
25

The average waiting time is 7.200000


The average turnaround time is 12.200000

AIM:
3.b)To implement the program for Round Robin scheduling algorithm
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 Round Robin 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>
#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;
}

SAMPLE INPUT AND OUTPUT:


Enter how many jobs:4
Enter burst time for corresponding job...
Process 1: 7
Process 2: 5
Process 3: 4
Process 4: 2
ENTER THE TIME SLICE VALUE:2
round 1
process 1 remaining time is 5
process 2 remaining time is 3
process 3 remaining time is 2
process 4 is completed
round 2
process 1 remaining time is 3
process 2 remaining time is 1
process 3 is completed
round 3
process 1 remaining time is 1
process 2 is completed
round 4
process 1 is completed
TOTAL WATING:39
AVERAGE WAITING TIME:9.000000
P1P2P3P4P1P2P3P1P2P1

AIM
3.c)To write a program to implement the FCFS scheduling algorithm
ALGORITHM
1. Start the process
2. Declare the array size

3. Get the number of processes to be inserted


4. Get the value
5. Start with the first process from its initial position 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>
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

Priority Scheduling Algorithm Program:


AIM:
3.d)To implement thec program for Priority Scheduling algorithm
ALGORITHM
1. Start the process
2. Declare the array size
3. Get the number of processes to be inserted
4. Get the value
5. Start with the first process from its initial position 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
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:

4.Implement all File allocation strategies:


a)Sequential
b)Indexed
c)Linked
Theory
a)Sequential file allocation strategy: In this type of strategy, the files are allocated in a sequential
manner such that there is a continuity among the various parts or fragments of the file.
b)Indexed file allocation strategy: In this type of strategy, the files are allocated based on the
indexes that are created for each fragment of the file such that each and every similar indexed
file is maintained by the primary index thereby providing flow to the file fragments.
c)Linked file allocation strategy: In this type of strategy, the files are allocated in a linked list
format where each and every fragment is linked to the other file through either addresses or
pointers. Thus, the starting location of the file serves for the purpose of extraction of the entire
file because every fragment is linked to each other.
Sequential File Allocation Program:
AIM:
4.a)To implement the program for Sequential File Allocation algorithm

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:

b)Linked File Allocation Program:


AIM:
4.b)To implement the program for Linked File Allocation Program algorithm
#include
#include
main()
{

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:

4.c)Indexed File Allocation Program:


AIM:
4.c)To implement the program Indexed File Allocation Program algorithm
#include
int f[50],i,k,j,inde[50],n,c,count=0,p;
main()
{
clrscr();
for(i=0;i<50;i++)
f[i]=0;
x:
printf("enter index block\t");
scanf("%d",&p);
if(f[p]==0)
{
f[p]=1;
printf("enter no of files on index\t");
scanf("%d",&n);
}
else
{
printf("Block already allocated\n");

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:

7.IMPLEMENT BANKERS ALGORITHM FOR DEADLOCK AVOIDANCE


AIM:
To Implement the Program Bankers Algorithm For Deadlock Avoidance Algorithm
ALGORITHM

1. Start the process


2. Declare the array size
3. Get the number of elements to be inserted
4..Stop the program

#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

8.Implement An Deadlock Detection Algorithm


AIM:
To Implement the Program Bankers Algorithm For Deadlock Detection Algorithm
#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("********** Deadlock Detection 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 resource 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 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

You might also like