-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsa.c
More file actions
270 lines (209 loc) · 3.79 KB
/
dsa.c
File metadata and controls
270 lines (209 loc) · 3.79 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct pr_queue
{
char name[30];
int size;
int id;
int rt;
int pr;
struct pr_queue* next;
}*start,*t,*q,*s;
//Defining functions for operations: priority, sjf, fcfs, display.
/*
Comparing from start node with all other nodes in the linked list.
's' is the node that is compared. 'q' is the rest of the nodes that is being compared to s.
't' stores the previously compared q node
*/
void priority(int n)
{
int k,j;
int tsize,tid,trt,tpr;
char tname[30];
s=start;
//Comparing every node.
for(k=0;k<n-1;k++)
{
s=start;
for(j=0;j<n-k-1;j++)
{
q=s->next;
if((s->pr)>(q->pr))
{
//Storing all values in temp variables for swapping.
strcpy(tname,s->name);
tsize=s->size;
tid=s->id;
trt=s->rt;
tpr=s->pr;
//Swapping values in s and q
strcpy(s->name,q->name);
s->size=q->size;
s->id=q->id;
s->rt=q->rt;
s->pr=q->pr;
//Restoring temp values in q
strcpy(q->name,tname);
q->size=tsize;
q->id=tid;
q->rt=trt;
q->pr=tpr;
}
s=s->next;
}
}
}
void sjf(int n)
{
int k,j;
int tsize,tid,trt,tpr;
char tname[30];
s=start;
//Comparing every node.
for(k=0;k<n-1;k++)
{
s=start;
for(j=0;j<n-k-1;j++)
{
q=s->next;
if((s->rt)>(q->rt))
{
//Storing all values in temp variables for swapping.
strcpy(tname,s->name);
tsize=s->size;
tid=s->id;
trt=s->rt;
tpr=s->pr;
//Swapping values in s and q
strcpy(s->name,q->name);
s->size=q->size;
s->id=q->id;
s->rt=q->rt;
s->pr=q->pr;
//Restoring temp values in q
strcpy(q->name,tname);
q->size=tsize;
q->id=tid;
q->rt=trt;
q->pr=tpr;
}
s=s->next;
}
}
}
void fcfs(int n)
{
int k,j;
int tsize,tid,trt,tpr;
char tname[30];
s=start;
//Comparing every node.
for(k=0;k<n-1;k++)
{
s=start;
for(j=0;j<n-k-1;j++)
{
q=s->next;
if((s->id)>(q->id))
{
//Storing all values in temp variables for swapping.
strcpy(tname,s->name);
tsize=s->size;
tid=s->id;
trt=s->rt;
tpr=s->pr;
//Swapping values in s and q
strcpy(s->name,q->name);
s->size=q->size;
s->id=q->id;
s->rt=q->rt;
s->pr=q->pr;
//Restoring temp values in q
strcpy(q->name,tname);
q->size=tsize;
q->id=tid;
q->rt=trt;
q->pr=tpr;
}
s=s->next;
}
}
}
void display(int n)
{
int i;
s=start;
printf("\n\n\nTHE ORDER OF PROCESSES ARE:\n");
for(i=0;i<n;i++)
{
puts(s->name);
s=s->next;
}
printf("\n\n\n");
}
int main()
{
int n,i;
printf("Enter number of processes.\n");
scanf("%d",&n);
//Creating n nodes in the priority queue and data entry.
for(i=0;i<n;i++)
{
s=(struct pr_queue *)malloc(sizeof(struct pr_queue));
printf("\n\nEnter name of %d process:\n",i+1);
scanf("%s",s->name);
printf("Enter size of process:\n");
scanf("%d",&s->size);
printf("Enter runtime of process:\n");
scanf("%d",&s->rt);
printf("Enter priority of process between 1 to %d:\n",n);
scanf("%d",&s->pr);
s->id=i+1;
s->next=NULL;
//Checking for 1st node
if(i==0)
start=s;
else
q->next=s;
q=s;
}
display(n);
//Displaying menu for process scheduling.
while(1)
{
printf("\n\nMENU FOR CPU SCHEDULING:\nEnter your choice:\n");
printf("1.PRIORITY\n2.SJF\n3.FCFS\n4.EXIT\n");
scanf("%d",&i);
//Calling function for display of process according to selection
if(i==4)
break;
else
{
switch(i)
{
case 1:
{
priority(n);
display(n);
break;
}
case 2:
{
sjf(n);
display(n);
break;
}
case 3:
{
fcfs(n);
display(n);
break;
}
default:
printf("Wrong choice entered.\nPlease try again.\n");
}
}
}
return 0;
}