-
Notifications
You must be signed in to change notification settings - Fork 1
/
os.c
631 lines (545 loc) · 13.7 KB
/
os.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// ===================== program 1 ====================
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
void main()
{
int pid;
pid=fork();
if(pid==0)
{
printf("Child process...");
printf("\n\nChild PID : %d",getpid());
printf("\nParent PID : %d",getppid());
printf("\n\nFinished with child\n");
}
else
{
wait(NULL);
printf("\nParent process");
printf("\nPARENT PID : %d",getpid());
printf("\nChild PID : %d",pid);
}
}
output :
Child process...
Child PID : 6612
Parent PID : 6608
Finished with child
Parent process
PARENT PID : 6608
Child PID : 6612
// ================ program 2 ===============
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
void main()
{
char buff;
int fd1,fd2;
fd1 = open("one.txt", O_RDONLY);
fd2 = open("two.txt", O_WRONLY|O_CREAT);
while(read(fd1,&buff,1))
{
write(fd2,&buff,1);
}
printf("the copy is successful ");
close(fd1);
close(fd2);
}
output :
the copy is successful
// ================ program 3 ====================
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
void main(int argc,char *arg[])
{
int pid;
pid=fork();
if(pid<0)
{
printf("fork failed");
exit(1);
}
else if(pid==0)
{
printf("\nNow in Child Process and it's o/p is \n");
execlp("ls","ls",NULL);
exit(0);
}
else
{
printf("\nChild Process created successfully");
printf("\nIt's Process id is %d\n",getpid());
wait(NULL);
printf("\nReturn back to Parent process, now ready to exit\n");
exit(0);
}
}
output :
Child Process created successfully
It's Process id is 108
Now in Child Process and it's o/p is
a.out main.c one.txt two.txt
Return back to Parent process, now ready to exit
// ================ program 4 ======================
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
int g = 0;
void *myThreadFun(void *vargp)
{
int *myid = (int*)vargp; //address covert to thread id
static int s = 0;
++s,++g;
printf("Thread ID : %u, static : %d, Global:%d\n", *myid,++s,++g);
}
int main()
{
int i;
pthread_t tid;
for(int i=0; i<3; i++)
{
pthread_create(&tid,NULL,myThreadFun,(void*)&tid);
}
pthread_exit(NULL);
return 0;
}
output :
Thread ID : 10995456, static : 2, Global:2
Thread ID : 10995456, static : 4, Global:4
Thread ID : 10995456, static : 6, Global:6
// ================= program 5 ====================
#include<stdio.h>
void main()
{
int n, tq, bt[10], at[10],bt1[10],wt = 0,tat = 0;
float awt,atat;
printf("Enter the number of process : ");
scanf("%d",&n);
for(int i = 0; i<n; i++)
{
printf("Enter burst and arrival time for process [%d] \n", i+1);
printf("Enter arrival time : ");
scanf("%d",&at[i]);
printf("Enter burst time : ");
scanf("%d",&bt[i]);
bt1[i] = bt[i];
}
printf("Enter time quantum : ");
scanf("%d",&tq);
int noOfProcessDone = 0;
int count = 0;
int i = 0;
int sum = 0;
printf("\nProcess\t\t Burst time \t\t TAT \t\t WT \n");
while(noOfProcessDone!=n)
{
if(bt1[i]<=tq && bt1[i]>0)
{
sum += bt1[i];
bt1[i] = 0;
count = 1;
}
else if(bt1[i]>0)
{
bt1[i] -= tq;
sum += tq;
}
if(bt1[i] == 0 && count == 1)
{
noOfProcessDone++;
printf("process[%d]\t\t %d \t\t %d \t\t %d \n", i+1, bt[i],sum-at[i],sum-at[i]-bt[i]);
tat += sum - at[i];
wt += sum - at[i] - bt[i];
count = 0;
}
if(i == n-1)
{
i = 0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i = 0;
}
}
atat = tat * 1.0 / n;
awt = wt * 1.0 /n;
printf("Average turn around time : %f \n",atat);
printf("Average waiting time : %f",awt);
}
output:
Enter the number of process : 5
Enter burst and arrival time for process [1]
Enter arrival time : 0
Enter burst time : 5
Enter burst and arrival time for process [2]
Enter arrival time : 1
Enter burst time : 3
Enter burst and arrival time for process [3]
Enter arrival time : 2
Enter burst time : 1
Enter burst and arrival time for process [4]
Enter arrival time : 3
Enter burst time : 2
Enter burst and arrival time for process [5]
Enter arrival time : 4
Enter burst time : 3
Enter time quantum : 2
Process Burst time TAT WT
process[3] 1 3 2
process[4] 2 4 2
process[2] 3 11 8
process[5] 3 9 6
process[1] 5 14 9
Average turn around time : 8.200000
Average waiting time : 5.400000
// ================ program 6 ==================
#include<stdio.h>
#include<stdlib.h>
int mutex=1 ,empty = 3, full = 0, x = 0;
int wait(int s)
{
return --s;
}
int signals(int s)
{
return ++s;
}
void producer()
{
mutex = wait(mutex);
full = signals(full);
empty = wait(empty);
x++;
printf("producer produces an item %d\n\n",x);
mutex = signals(mutex);
}
void consumer()
{
mutex = wait(mutex);
full = wait(full);
empty = signals(empty);
printf("consumer consumes an item %d\n\n",x);
x--;
mutex = signals(mutex);
}
void main()
{
int choice;
while(1)
{
printf("Enter your choice : \n1.Producer\n2.Consumer\n3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(empty !=0 && mutex ==1) producer();
else printf("Buffer is full\n");
break;
case 2: if(full !=0 && mutex == 1) consumer();
else printf("Buffer is empty\n");
break;
default: exit(1);
}
}
}
// ================== program 7 ===============
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process :");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
for(i = 0; i < p; i++)
for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];
do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}
process = -1;
for(i = 0; i < p; i++)
{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}
if(process != -1)
{
printf("\nProcess %d runs to completion!", process + 1);
safeSequence[count] = process + 1; count++;
for(j = 0; j < r; j++)
{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}
while(count != p && process != -1);
if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">;\n");
}
else
printf("\nThe system is in an unsafe state!!");
}
//=================program 8 ==================
#include<stdio.h>
int hitOrMiss(int num, int frames, int sf[])
{
for(int i = 0; i<frames; i++)
{
if(num == sf[i])
{
return 1;
}
}
return 0;
}
int fcfs(int n, int frames, int a[], int sf[])
{
int hit = 0, miss = 0, top = 0;
for(int i=0; i<n; i++)
{
if(hitOrMiss(a[i],frames,sf))
{
hit++;
}
else
{
miss++;
sf[top%frames] = a[i];
top++;
}
}
return miss;
}
void main()
{
int n;
printf("Enter the number of pages : ");
scanf("%d",&n);
int frames;
printf("Enter number of frames : ");
scanf("%d",&frames);
int a[n];
printf("Enter pages number : ");
for(int i = 0; i<n; i++)
{
scanf("%d",&a[i]);
}
int sf[frames];
for(int i = 0; i<frames; i++)
{
sf[i] = -1;
}
printf("page fault : %d",fcfs(n,frames,a,sf));
}
output:
Enter the number of pages : 14
Enter number of frames : 4
Enter pages number : 7 0 1 2 0 3 0 4 2 3 0 3 2 3
page fault : 7
// ================== program 9 ===============
#include<stdio.h>
#include<string.h>
void main()
{
char root_dir[10];
printf("Enter the root directory : ");
scanf("%s", root_dir);
int n;
printf("Enter the number of files : ");
scanf("%d",&n);
char sub_dir[n][10];
int count = 0;
while(1)
{
int choice;
printf("do you want to create files? (1 - yes, any - no : ");
scanf("%d",&choice);
if(choice !=1 )
break;
char filename[10];
printf("Enter filename : ");
scanf("%s",filename);
int found = 0;
for(int i = 0; i<n; i++)
{
if(strcmp(filename,sub_dir[i]) == 0)
{
found = 1;
break;
}
}
if(found)
{
printf("File already exist! ");
}
else if(count == n)
{
printf("Number of file exceeded \n");
}
else
{
strcpy(sub_dir[count++],filename);
}
}
printf("directory name: %s\n", root_dir);
printf("file names:\n");
for (int i = 0; i < n; i++)
printf("%s ", sub_dir[i]);
}
output :
Enter the root directory : anchal
Enter the number of files : 5
do you want to create files? (1 - yes, any - no) : 1
Enter filename : one
do you want to create files? (1 - yes, any - no) : 1
Enter filename : two
do you want to create files? (1 - yes, any - no) : 1
Enter filename : three
do you want to create files? (1 - yes, any - no) : 1
Enter filename : four
do you want to create files? (1 - yes, any - no) : 1
Enter filename : five
do you want to create files? (1 - yes, any - no) : 1
Enter filename : d
Number of file exceeded
do you want to create files? (1 - yes, any - no) : 1
Enter filename : r
Number of file exceeded
do you want to create files? (1 - yes, any - no) : 0
directory name: anchal
file names:
one two three four five
// ======================= program 10 ==================
#include<stdio.h>
#include<stdlib.h>
void main()
{
int index_block[50], idx, index[50],n;
for(int i =0; i<50; i++)
index_block[i] = 0;
x:printf("\nEnter the index block : ");
scanf("%d",&idx);
if(index_block[idx])
{
printf("\nindex is already allocated ");
goto x;
}
printf("Enter the block needed : ");
scanf("%d",&n);
printf("Enter the number of files for the index %d on the disk : ",idx);
int count = 0;
for(int i = 0; i<n; i++)
{
scanf("%d",&index[i]);
if(index_block[index[i]] == 0)
count++;
}
if(count == n)
{
for(int i = 0; i<n; i++)
index_block[index[i]] = 1;
printf("allocated");
printf("\nfile indexed");
for(int i=0; i<n; i++)
printf("\n%d ------------ %d : %d",idx,index[i],index_block[index[i]]);
}
else
{
printf("File in the index is already allocated");
goto x;
}
int c;
printf("\nDo you want to enter more files(Yes - 1/No - 0):");
scanf("%d", &c);
if(c == 1)
goto x;
else
exit(0);
}
output :
Enter the index block : 5
Enter the block needed : 4
Enter the number of files for the index 5 on the disk : 1 2 3 4
allocated
file indexed
5 ------------ 1 : 1
5 ------------ 2 : 1
5 ------------ 3 : 1
5 ------------ 4 : 1
Do you want to enter more files(Yes - 1/No - 0):1
Enter the index block : 6
Enter the block needed : 2
Enter the number of files for the index 6 on the disk : 1 2
File in the index is already allocated
Enter the index block : 7
Enter the block needed : 3
Enter the number of files for the index 7 on the disk : 7 8 9
allocated
file indexed
7 ------------ 7 : 1
7 ------------ 8 : 1
7 ------------ 9 : 1
Do you want to enter more files(Yes - 1/No - 0):0