0% found this document useful (0 votes)
3 views29 pages

C Programs for CPU Scheduling Algorithms

The document outlines a series of lab exercises focused on implementing various scheduling policies and file allocation methods using C programming. It includes detailed code examples for First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, Round Robin Scheduling, Sequential File Allocation, and Indexed File Allocation. Each section provides the aim, code implementation, and sample outputs for better understanding.

Uploaded by

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

C Programs for CPU Scheduling Algorithms

The document outlines a series of lab exercises focused on implementing various scheduling policies and file allocation methods using C programming. It includes detailed code examples for First Come First Served (FCFS), Shortest Job First (SJF), Priority Scheduling, Round Robin Scheduling, Sequential File Allocation, and Indexed File Allocation. Each section provides the aim, code implementation, and sample outputs for better understanding.

Uploaded by

shivhari321202
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

INDEX

S. No. List of Experiments Date Sign

1 Write a Program to implement the FCFS (First Come First Served)


Scheduling Policy
2. Write a Program to implement the SJF (Shortest Job First)
Scheduling Policy
3. Write a Program to implement the Priority (Non-preemptive)
Scheduling Policy
4. Write a Program to implement the RR (Round Robin) Scheduling
Policy
5. Write a Program to implement the Sequential File Allocation
6. Write a Program to implement the Indexed File Allocation

7. Write a Program to implement the Linked File Allocation


8. Write a Program to implement the solution for Producer-Consumer
problem using inter process communication techniques-Semaphores
9. Write a Program to implement the Banker’s Algorithm for Dead Lock
Avoidance and Dead Lock Prevention.
1. Lab Exercise

Aim: Write a C program to implement the FCFS Scheduling Policy.

#include<stdio.h>
void main()
{
int i,n,sum,wt,tat,twt,ttat;
int t[10];
float awt,atat;
clrscr();
printf("Enter number of processors:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of the process %d",i+1);
scanf("\n %d",&t[i]);
}
printf("\n\n FIRST COME FIRST SERVE SCHEDULING ALGORITHM \n");
printf("\n Process ID \t Waiting Time \t Turn Around Time \n");
printf("1 \t\t 0 \t\t %d \n",t[0]);
sum=0;
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
sum+=t[i-1];
wt=sum;
tat=sum+t[i];
twt=twt+wt;
ttat=ttat+tat;
printf("\n %d \t\t %d \t\t %d",i+1,wt,tat);
printf("\n\n");
}
awt=(float)twt/n;
atat=(float)ttat/n;
printf("\n Average Waiting Time %4.2f",awt);
printf("\n Average Turnaround Time %4.2f",atat);
getch();
}
1
Output

Enter number of processors: 3


Enter the Burst Time of the process 1: 2
Enter the Burst Time of the process 2: 5
Enter the Burst Time of the process 3: 4

FIRST COME FIRST SERVE SCHEDULING ALGORITHM


Process ID Waiting Time Turn Around Time
1 0 2
2 2 7
3 7 11

Average Waiting Time 3.00


Average Turnaround Time 6.67

2
2. Lab Exercise

Aim: Write a C program to implement SJF Scheduling Policy

#include<stdio.h>
void main()
{
int i,j,k,n,sum,wt[10],tt[10],twt,ttat;
int t[10],p[10];
float awt,atat;
clrscr();

printf("Enter number of process\n");


scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\n Enter the Burst Time of Process %d",i);
scanf("\n %d",&t[i]);
}

for(i=0;i<n;i++)
p[i]=i;
for(i=0;i<n;i++)
{
for(k=i+1;k<n;k++)
{
if(t[i]>t[k])
{
int temp;
temp=t[i];
t[i]=t[k];
t[k]=temp;

temp=p[i];
p[i]=p[k];
p[k]=temp;
}
}

3
printf("\n\n SHORTEST JOB FIRST SCHEDULING ALGORITHM");
printf("\n PROCESS ID \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n\n");
wt[0]=0;
for(i=0;i<n;i++)
{
sum=0;
for(k=0;k<i;k++)
{
wt[i]=sum+t[k];
sum=wt[i];
}
}
for(i=0;i<n;i++)
{
tt[i]=t[i]+wt[i];
}
for(i=0;i<n;i++)
{
printf("%5d \t\t5%d \t\t %5d \t\t %5d \n\n",p[i],t[i],wt[i],tt[i]);
}
twt=0;
ttat=t[0];
for(i=1;i<n;i++)
{
twt=twt+wt[i];
ttat=ttat+tt[i];
}
awt=(float)twt/n;
atat=(float)ttat/n;

printf("\n AVERAGE WAITING TIME %4.2f",awt);


printf("\n AVERAGE TURN AROUND TIME %4.2f",atat);
getch();
}

Output

4
Enter number of process 3
Enter the Burst Time of Process 4
Enter the Burst Time of Process 13
Enter the Burst Time of Process 25

SHORTEST JOB FIRST SCHEDULING ALGORITHM


PROCESS ID BURST TIME WAITING TIME TURNAROUND TIME
1 3 0 3
0 4 3 7
2 5 7 12

AVERAGE WAITING TIME 3.33


AVERAGE TURN AROUND TIME 7.33

5
3. Lab Exercise

Aim: Write a C program to implement PRIORITY Scheduling Policy

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n,tat[10],wt[10],bt[10],pid[10],pr[10],t,twt=0,ttat=0;
float awt,atat;
clrscr();
printf("\n-----------PRIORITY SCHEDULING--------------\n");
printf("Enter the No of Process: ");
scanf("%d", &n);
for (i=0;i<n;i++)
{
pid[i] = i;
printf("Enter the Burst time of Pid %d : ",i);
scanf("%d",&bt[i]);
printf("Enter the Priority of Pid %d : ",i);
scanf ("%d",&pr[i]);
}
// Sorting start
for (i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if (pr[i] > pr[j] )
{
t = pr[i];
pr[i] = pr[j];
pr[j] = t;

t = bt[i];
bt[i] = bt[j];
bt[j] = t;

t = pid[i];
pid[i] = pid[j];
pid[j] = t;

6
}
}

// Sorting finished

tat[0] = bt[0];
wt[0] = 0;

for (i=1;i<n;i++)
{
wt[i] = wt[i-1] + bt[i-1];
tat[i] = wt[i] + bt[i];
}

printf("\n---------------------------------------------------------------\n");
printf("Pid\t Priority\tBurst time\t WaitingTime\tTurnArroundTime\n");
printf("\n--------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("\n%d\t\t%d\t%d\t\t%d\t\t%d",pid[i],pr[i],bt[i],wt[i],tat[i]);
}
for(i=0;i<n;i++)
{
ttat = ttat+tat[i];
twt = twt + wt[i];
}
awt = (float)twt / n;
atat = (float)ttat / n;
printf("\n\[Link] Time: %f\[Link] Around Time: %f\n",awt,atat);
getch();
}

7
Output

-----------PRIORITY SCHEDULING--------------
Enter the No of Process: 4
Enter the Burst time of Pid 0 : 2
Enter the Priority of Pid 0 : 3
Enter the Burst time of Pid 1 : 6
Enter the Priority of Pid 1 : 2
Enter the Burst time of Pid 2 : 4
Enter the Priority of Pid 2 : 1
Enter the Burst time of Pid 3 : 5
Enter the Priority of Pid 3 : 7

----------------------------------------------------------------------------------------
Pid Priority Burst time WaitingTime TurnArroundTime

----------------------------------------------------------------------------------------
2 1 4 0 4
1 2 6 4 10
0 3 2 10 12
3 7 5 12 17

[Link] Time: 6.500000


[Link] Around Time: 10.750000

8
4. Lab Exercise

Aim: Write a C program to implement the ROUND ROBIN Scheduling Policy

#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++)

9
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)

10
{
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++)

11
printf("%c",22);
}
printf("\n");
getch();
return 0;
}
Output

JOB SCHEDULING ALGORITHM[RR]


*******************************************************
Enter how many jobs:4
Enter burst time for corresponding job...
Process 1: 10
Process 2: 15
Process 3: 20
Process 4: 25
ENTER THE TIME SLICE VALUE:5
round 1
process 1 remaining time is 5
process 2 remaining time is 10
process 3 remaining time is 15
process 4 remaining time is 20
round 2
process 1 is completed
process 2 remaining time is 5
process 3 remaining time is 10
process 4 remaining time is 15
round 3
process 2 is completed
process 3 remaining time is 5
process 4 remaining time is 10
round 4
process 3 is completed
process 4 remaining time is 5
round 5
process 4 is completed
TOTAL WATING:130

AVERAGE WAITING TIME:32.000000


P1▬▬▬▬▬P2▬▬▬▬▬P3▬▬▬▬▬P4▬▬▬▬▬P1▬▬▬▬▬P2▬▬▬▬▬P3▬▬▬▬
▬P4▬▬▬▬▬P2▬▬▬▬▬P3▬▬▬▬▬P4▬▬▬▬▬P3▬
▬▬▬▬P4▬▬▬▬▬P4▬▬▬▬▬

12
5. Lab Exercise

Aim: Write a C Program to implement Sequential File Allocation 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 [Link] files:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter no. of Partitions occupied by file%d",i+1);
scanf("%d",&b[i]);
printf("Enter the starting block of file%d",i+1);
scanf("%d",&sb[i]);
t[i]=sb[i];
for(j=0;j<b[i];j++)
c[i][j]=sb[i]++;
}
printf("Filename\tStart block\tlength\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("File name is:%d",x);
printf("length is:%d",b[x-1]);
printf("Partitions occupied:");
for(i=0;i<b[x-1];i++)
printf("%4d",c[x-1][i]);
getch();
}

13
Output

Enter no. of files: 2


Enter no. of Partitions occupied by file1 4
Enter the starting block of file1 2
Enter no. of Partitions occupied by file2 10
Enter the starting block of file2 5
Filename Start block length
1 2 4
2 5 10
Enter file name: rajesh
File name is:12803 length is:0Partitions occupied

14
6. Lab Exercise

Aim: Write a C Program to implement Indexed File Allocation method.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
int n, m[20], i, j, sb[20], s[20], b[20][20];
char fname[20][50], search[50];

printf("Enter number of files: ");


scanf("%d", &n);

for (i = 0; i < n; i++) {


printf("Enter name of file %d: ", i + 1);
scanf("%s", fname[i]);

printf("Enter starting block and size of file %s: ", fname[i]);


scanf("%d%d", &sb[i], &s[i]);

printf("Enter number of partitions occupied by file %s: ", fname[i]);


scanf("%d", &m[i]);

printf("Enter partitions of file %s: ", fname[i]);


for (j = 0; j < m[i]; j++) {
scanf("%d", &b[i][j]);
}
}

printf("\nFile\t\tIndex\tLength\n");
for (i = 0; i < n; i++) {
printf("%s\t\t%d\t%d\n", fname[i], sb[i], m[i]);
}

printf("\nEnter file name to search: ");


scanf("%s", search);

// Search the file


int found = 0;
for (i = 0; i < n; i++) {
if (strcmp(search, fname[i]) == 0) {
found = 1;
printf("File name is: %s\n", fname[i]);
printf("Index block: %d\n", sb[i]);

15
printf("Blocks occupied: ");
for (j = 0; j < m[i]; j++) {
printf("%d ", b[i][j]);
}
printf("\n");
break;
}
}

if (!found) {
printf("File '%s' not found!\n", search);
}

return 0;
}

Output

Enter number of files: 2


Enter name of file 1: venkat
Enter starting block and size of file venkat: 2 5
Enter number of partitions occupied by file venkat: 3
Enter partitions of file venkat: 2 5 4
Enter name of file 2: oslab
Enter starting block and size of file oslab: 3 4
Enter number of partitions occupied by file oslab: 4
Enter partitions of file oslab: 2 3 4 5

File Index Length


venkat 2 3
oslab 3 4

Enter file name to search: venkat


File name is: venkat
Index block: 2
Blocks occupied: 2 5 4

16
7. Lab Exercise

Aim: Write a 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 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 [Link] Partitions:");
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=1;j<=f[i].size-1;j++)
printf("%d--->",f[i].block[j]);
printf("%d",f[i].block[j]);

17
printf("\n");
}
getch();
}

Output

Enter no. of files:2


Enter file name:venkat
Enter starting block:20
Enter [Link] Partitions:6
Enter block numbers: 4
12
15
45
32
25
Enter file name:rajesh
Enter starting block:12
Enter [Link] Partitions:5
Enter block numbers:6
5
4
3
2
File start size block
venkat 20 6 4--->12--->15--->45--->32--->25
rajesh 12 5 6--->5--->4--->3--->2

18
8. Lab Exercise

Aim: Implement the solution for Producer-Consumer problem using inter process communication
techniques-Semaphores

#include <stdio.h>
#include <stdlib.h>

// Initial semaphore and buffer values


int mutex = 1;
int full = 0;
int empty = 3; // Buffer size is 3
int x = 0; // Item counter

// Function declarations
int wait(int);
int signal(int);
void producer();
void consumer();

int main() {
int choice;

printf("----------- PRODUCER - CONSUMER PROBLEM -----------\n");


printf("1. Producer\n2. Consumer\n3. Exit\n");

while (1) {
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
if ((mutex == 1) && (empty != 0)) {
producer();
} else {
printf("Buffer is full!!");
}
break;

case 2:
if ((mutex == 1) && (full != 0)) {
consumer();
} else {
printf("Buffer is empty!!");
}
break;

case 3:
printf("Exiting the program...\n");
exit(0);

19
default:
printf("Invalid choice! Please enter 1, 2, or 3.");
}
}

return 0;
}

// Decrement semaphore
int wait(int s) {
return (--s);
}

// Increment semaphore
int signal(int s) {
return (++s);
}

// Producer function
void producer() {
mutex = wait(mutex);
full = signal(full);
empty = wait(empty);
x++;
printf("Producer produces item %d\n", x);
mutex = signal(mutex);
}

// Consumer function
void consumer() {
mutex = wait(mutex);
full = wait(full);
empty = signal(empty);
printf("Consumer consumes item %d\n", x);
x--;
mutex = signal(mutex);
}

Output

----------- PRODUCER - CONSUMER PROBLEM -----------


1. Producer
2. Consumer
3. Exit

Enter your choice: 1


Producer produces item 1

Enter your choice: 1

20
Producer produces item 2

Enter your choice: 1


Producer produces item 3

Enter your choice: 1


Buffer is full!!
Enter your choice: 2
Consumer consumes item 3

Enter your choice: 2


Consumer consumes item 2

Enter your choice: 2


Consumer consumes item 1

Enter your choice: 2


Buffer is empty!!
Enter your choice: 3
Exiting the program...

21
9. Lab Exercise

Aim: To implement deadlock avoidance & Prevention by using Banker’s Algorithm.

#include<stdio.h>
char res[10]={'A','B','C','D','E','F','G','H','I','J'};
void disp(int m,int n,int alloc[5][5],int max[5][5],int avble[5],int need[5][5]);
void safesq(int m,int n,int alloc[5][5],int avble[5],int need[5][5]);
void resreq(int m,int n,int alloc[5][5],int avble[5],int need[5][5]);
void main()
{
int i,j,ch,n,m,work1[5];
int avble[5],alloc[5][5],max[5][5],need[5][5];
printf("\nENTER THE NUMBER OF PROCESSES : \t");
scanf("%d",&n);
printf("\nENTER THE NUMBER OF RESOURCES : \t");
scanf("%d",&m);
printf("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
printf("\n\tRESOURCE %c:\t",res[i]);
scanf("%d",&avble[i] );
}
printf("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE\n");
for(i=0;i<n;i++)
{
printf("\n\tFOR PROCESS P%d \n",i);
for(j=0;j<m;j++)
{
printf("\n\tRESOURCE %c : \t",res[j]);
scanf("%d",&max[i][j]);
}
}
printf("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO");
printf(" A PROCESS.\n");
for(i=0;i<n;i++)
{
printf("\n\tFOR PROCESS P%d \n",i);
for(j=0;j<m;j++)

22
{
printf("\n\tRESOURCE %c : \t",res[j]);
scanf("%d",&alloc[i][j]);
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(1)
{
printf("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE SEQUENCE");
printf("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR CHOICE:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
disp(m,n,alloc,max,avble,need);
break;
case 2:
safesq(m,n,alloc,avble,need);
break;
case 3:
resreq(m,n,alloc,avble,need);
break;
case 4:
exit(0);
default:

23
printf("\n\tINVALID CHOICE ENTERED.\n");
}
}
}
void disp(int m,int n,int alloc[5][5],int max[5][5],int avble[5],int need[5][5])
{
int i,j;
printf("\n\t\tALLOCATION\tMAX\tNEED\t AVAILABLE");
printf("\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
printf("%c ",res[j]);
printf(" ");
}
for(i=0;i<n;i++)
{
printf("\n\tP%d\t",i);
for(j=0;j<m;j++)
printf("%d ",alloc[i][j]);
printf(" ");
for(j=0;j<m;j++)
printf("%d ",max[i][j]);
printf("\t");
for(j=0;j<m;j++)
printf("%d ",need[i][j]);
printf(" ");
if(i==0)
{
for(j=0;j<m;j++)
printf("%d ",avble[j]);
}
}
}
void safesq(int m,int n,int alloc[5][5],int avble[5],int need[5][5])
{
int i,j,k=0,l,work[5],work1[5],fin[5],flag=0,flag1=0,safesq[6];
for(i=0;i<m;i++)

24
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
printf("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
printf(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}

25
}
if(flag==0)
{
printf("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
printf("P%d ",safesq[i]);
}
}
void resreq(int m,int n,int alloc[5][5],int avble[5],int need[5][5])
{
int i,j,num,alloc1[5][5],avble1[5],req[5],flag=0,flag1=0;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
printf("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
scanf("%d",&num);
printf("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS P%d\n",num);
for(i=0;i<m;i++)
{
printf("\n\tRESOURCE %c:\t",res[i]);
scanf("%d",&req[i]);
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{

26
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
printf("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
printf("ARE NOT AVAILABLE.\n");
}
}
else
printf("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}

Output

ENTER THE NO. OF PROCESSES:4

ENTER THE NO. OF RESOURCES:3


PROCESS 1
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:1
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:1
PROCESS 2
MAXIMUM VALUE FOR RESOURCE 1:6
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:3

27
ALLOCATED FROM RESOURCE 1:5
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:1
PROCESS 3
MAXIMUM VALUE FOR RESOURCE 1:3
MAXIMUM VALUE FOR RESOURCE 2:1
MAXIMUM VALUE FOR RESOURCE 3:4
ALLOCATED FROM RESOURCE 1:2
ALLOCATED FROM RESOURCE 2:1
ALLOCATED FROM RESOURCE 3:2
PROCESS 4
MAXIMUM VALUE FOR RESOURCE 1:4
MAXIMUM VALUE FOR RESOURCE 2:2
MAXIMUM VALUE FOR RESOURCE 3:2
ALLOCATED FROM RESOURCE 1:0
ALLOCATED FROM RESOURCE 2:0
ALLOCATED FROM RESOURCE 3:2
ENTER TOTAL VALUE OF RESOURCE 1:9
ENTER TOTAL VALUE OF RESOURCE 2:3
ENTER TOTAL VALUE OF RESOURCE 3:6

RESOURCES ALLOCATED NEEDED TOTAL AVAIL


P1 322 101 221 936 110
P2 613 511 102
P3 314 212 102
P4 422 002 420

AVAIL BEFORE AVAIL AFTER


DEADLOCK OCCURED

28

You might also like