-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOS_Project.c
129 lines (120 loc) · 1.77 KB
/
OS_Project.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//Concept of Deadlock
#include<stdio.h>
struct proc
{
int pid;
int at,bt,wt,tat,rbt;
int flag,flag1;
};
struct proc p1[10];
int i,j,k,n,no,m;
float atat=0.0,awt=0.0;
int tbt=0;
int minimum1();
int main()
{
int minv,locv,mins,locs;
printf("\nenter the number of processes:");
scanf("%d",&n);
printf("\nenter the proc information:");
printf("\npid at bt");
for(i=0;i<n;i++)
{
p1[i].wt=0;
p1[i].tat=0;
p1[i].flag=0;
p1[i].flag1=0;
scanf("%d%d%d",&p1[i].pid,&p1[i].at,&p1[i].bt);
tbt+=p1[i].bt;
p1[i].rbt=p1[i].bt;
}
printf("\nthe proc information:");
printf("\npid at bt");
for(i=0;i<n;i++)
{
printf("\n%d %d %d",p1[i].pid,p1[i].at,p1[i].bt);
}
minv=p1[0].at;
locv=0;
for(i=1;i<n;i++)
{
if(p1[i].at<minv)
{
locv=i; //tells min at process in locv
minv=p1[i].at;
}
}
for(i=0;i<n;i++)
{
if(p1[i].at==minv)
{
p1[i].flag1=1; //processes having same minimum at
}
}
mins=p1[0].bt;
locs=0;
for(i=0;i<n;i++)
{
if(p1[i].flag1==1&&p1[i].bt<mins)
{
mins=p1[i].bt; //gives process with minimum burst time
locs=i;
}
}
printf("\ngantt chart:");
for(i=minv;i<tbt+minv;i++)
{
for(j=0;j<n;j++)
{
if(p1[j].rbt>0&&p1[j].at<=i)
{
p1[j].flag=1;
}
}
no=minimum1();
printf("%d p[%d]",i,p1[no].pid);
p1[no].rbt=p1[no].rbt-1;
for(k=0;k<n;k++)
{
if(p1[k].rbt>0&&p1[k].at<=i&&k!=no)
{
p1[k].wt++;
}
}
}
printf("%d",tbt+minv);
for(i=0;i<n;i++)
{
awt+=p1[i].wt;
}
awt=awt/n;
for(i=0;i<n;i++)
{
p1[i].tat=p1[i].wt+p1[i].bt;
atat+=p1[i].tat;
}
atat=atat/n;
printf("\n average wt=%f, average tat=%f",awt,atat);
printf("\nthe proc information:");
printf("\npid at bt wt tat");
for(i=0;i<n;i++)
{
printf("\n%d %d %d %d %d",p1[i].pid,p1[i].at,p1[i].bt,p1[i].wt,p1[i].tat);
}
}
int minimum1()
{
int loc,z;
int mini;
mini=99;
loc=-1;
for(z=0;z<n;z++)
{
if(p1[z].rbt>0&&p1[z].at<=i&&p1[z].rbt<mini)
{
mini=p1[z].rbt;
loc=z;
}
}
return loc;
}