0% found this document useful (0 votes)
10 views3 pages

Deadlock J

Uploaded by

Shivangi Siddhu
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)
10 views3 pages

Deadlock J

Uploaded by

Shivangi Siddhu
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
You are on page 1/ 3

Department Of Information Technology Operating System Lab

EXPERIMENT-
AIM:
To write a c program to implement Best Fit and Worst Fit Algorithm

SOURCE CODE:

.
#include<stdio.h>

void implementFirstFit(int BSize[], int blocks, int processSize[], int processes)


{

int allocate[processes];
int
intmain()
occupied[blocks];
{
for(int i = 0; i < processes; i++)
{ int n,bt[20],wt[20],ct[20],tat[20],i,j;
allocate[i] = -1;
} float avwt=0,avtat=0,tp;
int iat[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
for(int = 0; i < blocks; i++){
occupied[i] = 0;
printf("Enter total number of processes(maximum 20):");
}
scanf("%d",&n);
printf("\nEnter
for Process Burst
(int i = 0; i < processes; i++) Time\n");
{
for(i=0;i<n;i++){
for (int j = 0; j < blocks; j++)
{
if printf("P[%d]:",i+1);
(!occupied[j] && BSize[j] >= processSize[i])
{
scanf("%d",&bt[i]);
allocate[i] = j;
} occupied[j] = 1;

ct[0]=bt[0];
break;
}
for(i=1;i<n;i++){
}
}
ct[i]=ct[i-1]+bt[i];
printf("\nProcess
} No.\t Process Size\t Block no.\n");
for (int i = 0; i < processes; i++)
{ for(i=0;i<n;i++){
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if tat[i]=ct[i]-at[i];
(allocate[i] != -1)
printf("%d\n",allocate[i] + 1);
}else
printf("Not Allocated\n");
} wt[0]=0;
} for(i=1;i<n;i++){

wt[i]=tat[i]-bt[i];
}
printf("\nProcess\t\tArrival time\tBurst Time\tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++){

Shivangi Siddhu 2100910130101


Department Of Information Technology Operating System Lab

void implimentBestFit(int BSize[], int blocks, int processSize[], int proccesses)


{
int allocation[proccesses];
avwt+=wt[i];
int occupied[blocks];
for(int i avtat+=tat[i];
= 0; i < proccesses; i++){
allocation[i] = -1;
} printf("\nP[%d]\t\t%d\t\t%d\t\t%d\t\t%d",i+1,at[i],bt[i],wt[i],tat[i]);
for(int} i = 0; i < blocks; i++){
occupied[i] = 0;
} avwt=avwt/i;
avtat=avtat/i;
for (int i = 0; i < proccesses; i++)
{ printf("\n\nAverage Waiting Time:%f",avwt);
intprintf("\nAverage
indexPlaced = -1; Turnaround Time:%f",avtat);
for (int j = 0; j < blocks; j++) {
return 0; >= processSize[i] && !occupied[j])
if (BSize[j]
{
}
if (indexPlaced == -1)
indexPlaced = j;

else if (BSize[j] < BSize[indexPlaced])


indexPlaced = j;
}
}

if (indexPlaced != -1)
{

allocation[i] = indexPlaced;
occupied[indexPlaced] = 1;
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\n");


for (int i = 0; i < proccesses; i++)
{
printf("%d \t\t\t %d \t\t\t", i+1, processSize[i]);
if (allocation[i] != -1)
printf("%d\n",allocation[i] + 1);
else
printf("Not Allocated\n");
}
}

void main()
{
int BSize[] = {30, 3, 10};
int processSize[] = {1, 6, 1};
int m =sizeof(BSize)/sizeof(BSize[0]);
int n =sizeof(processSize)/sizeof(processSize[0]);
int ch;
printf("1:First Fit\n2.Best Fit\n3.exit\nenter your choice:");
scanf("%d",&ch);
switch(ch)
{

case 1:implementFirstFit(BSize,m, processSize, n);


break;
case 2:implimentBestFit(BSize,m, processSize, n);
break;
case 3:

default:
printf("INAVLID");

Shivangi Siddhu 2100910130101


Department Of Information Technology Operating System Lab

OUTPUT:

Shivangi Siddhu 2100910130101

You might also like