-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal.c
1193 lines (1050 loc) · 27 KB
/
final.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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Harsh Daryani 2018B1A70645H
//Virag Lakhani 2018B3A70973H
//Jui Pradhan 2018B3A70984H
//Deepanshu Mody 2018B1A70949H
//Rohit Sethi 2018B4A70611H
//Saaketh Reddy Vaddi 2019A7PS1215H
//Rohan Sachan 2018B3A70992H
//Pranav Khabale 2018B1A70794H
//Aaryan Gupta 2018B1A70775H
//Group No. 45
#include<sys/types.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<pthread.h>
#include<errno.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include <stdio.h>
#include <sys/mman.h>
#define READ 0
#define WRITE 1
char er[14];
# define SIZE 100
//Below is the queue code we have used for round robin scheduling
void enqueue();
int dequeue();
int inp_arr[SIZE];
int Rear = - 1;
int Front = - 1;
void enqueue(int insert_item)
{
if (Rear == SIZE - 1)
printf("Overflow \n");
else
{
if (Front == - 1)
Front = 0;
Rear = Rear + 1;
inp_arr[Rear] = insert_item;
}
}
int dequeue()
{
if (Front == - 1 || Front > Rear)
{
//printf("Underflow \n");
return -1;
}
else
{
int a=inp_arr[Front];
Front = Front + 1;
return a;
}
}
int IsEmpty()
{
if (Front == - 1 && Front > Rear)
return 0;
else
return 0;
}
long long int sum_c1=0;
long long int sum_c3=0;
long long int n1,n2,n3;//workload
//mutex initialisation
pthread_mutex_t mutex_c1;
pthread_mutex_t mutex_c2;
pthread_mutex_t mutex_c3;
double a,b,c;
FILE *fptr;//file
long long int arr[1000000];//array for c1
//Here the monitor thread has a pointer to the shared memory variable(so changes there will be reflected here) throug which is decides whether to sleep or awake the task thread.
//task thread of c1
void *add(void *param)
{
pthread_mutex_lock(&mutex_c1);
long long int *addr=(long long int *)param;
for(long long int i=0;i<=*addr;i++)
sum_c1+=i;
pthread_mutex_unlock(&mutex_c1);
pthread_exit(0);
}
//monitor thread of c1
void *c1_monitor(void *param)
{
char *addr=(char *)param;
pthread_mutex_lock(&mutex_c1);
//printf("%c",*addr);
while(1)
{
if(*addr == '0')
{
break;
}
}
pthread_mutex_unlock(&mutex_c1);
pthread_exit(0);
}
//task thread of c2
void *read_c2(void *param)
{
pthread_mutex_lock(&mutex_c2);
int num;
long long int* upper=(long long int*) param;
long long int lim=*upper;
if ((fptr = fopen("add.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
for (long long int i = 0; i <lim; i++)
{
fscanf(fptr, "%d", &num);
printf("%d\n",num);
}
fclose(fptr);
pthread_mutex_unlock(&mutex_c2);
pthread_exit(0);
}
//monitor thread of c2
void *c2_monitor(void *param)
{
char *addr=(char *)param;
pthread_mutex_lock(&mutex_c2);
while(1)
{
if(*addr == '1')
break;
}
pthread_mutex_unlock(&mutex_c2);
pthread_exit(0);
}
//task thread of c3
void *add_c3(void *param)
{
pthread_mutex_lock(&mutex_c3);
int num;
long long int* upper=(long long int*) param;
long long int lim=*upper;
if ((fptr = fopen("add.txt", "r")) == NULL)
{
printf("Error! opening file");
exit(1);
}
for (long long int i = 0; i <lim; i++)
{
fscanf(fptr, "%d", &num);
sum_c3 += num;
}
fclose(fptr);
pthread_mutex_unlock(&mutex_c3);
pthread_exit(0);
}
//monitor thread of c3
void *c3_monitor(void *param)
{
char *addr=(char *)param;
pthread_mutex_lock(&mutex_c3);
while(1)
{
if(*addr=='2')
break;
}
pthread_mutex_unlock(&mutex_c3);
pthread_exit(0);
}
//Shared memory to be used for time analysis
void* getSharedMem(size_t size)
{
int prot = PROT_READ | PROT_WRITE;
int vis = MAP_SHARED | MAP_ANONYMOUS;
return mmap(NULL, size, prot, vis, -1, 0);
}
double *ti;
int main()
{
printf("Enter 0 for FCFS and 1 for Round Robin : ");
//shared memory
int shmid;
char *shmptr;
int schedule;// fcfs or RR
scanf("%d",&schedule);
if(schedule==0)
{
//fcfs scheduling
ti=getSharedMem(sizeof(double)*6);//shared memory double array to pass time from child to parent
printf("Enter value of n1:");
scanf("%lld",&n1);
printf("Enter value of n2:");
scanf("%lld",&n2);
printf("Enter value of n3:");
scanf("%lld",&n3);
printf("\nEnter 0 to give n1 numbers as input or to add from 1 to n1 in c1\n");
int po;
scanf("%d",&po);
if(po==0)
{
printf("\nInput %lld numbers to add in C1\n",n1);
for(int i=0;i<n1;i++)
scanf("%lld",&arr[i]);
}
else
{
for(int i=0;i<n1;i++)
arr[i]=i;
}
printf("\n");
pid_t pid1;
//3 pipes for communicating result of 3 childs to parent
int fd1[2];
int fd2[2];
int fd3[2];
if(pipe(fd1)<0)
{
perror("pipe failed");
exit(1);
}
if(pipe(fd2)<0)
{
perror("pipe failed");
exit(1);
}
if(pipe(fd3)<0)
{
perror("pipe failed");
exit(1);
}
//first fork to create c1
pid1 = fork();
//recording arrival time of c1
struct timespec arrive_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c1);
if (pid1 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid1 == 0)
{ //child c1
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd1[READ]);
long long int *nl=&n1;
pthread_t tid[2];
pthread_attr_t at[2];
pthread_attr_init(&at[0]);
pthread_attr_init(&at[1]);
//monitor thread of c1
if((pthread_create(&tid[0],&at[0],c1_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c1
if((pthread_create(&tid[1],&at[1],add,nl))==-1)
{
printf("asdf");
}
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
//writing on pipe
write(fd1[WRITE],&sum_c1,sizeof(sum_c1));
close(fd1[WRITE]);
//recording time
struct timespec finish_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c1);
ti[0]=((double)finish_c1.tv_sec + 1.0e-9*finish_c1.tv_nsec);
//detach from shared memory
shmdt(shmptr);
}
else
{
pid_t pid2;
//second fork to create c2
pid2 = fork();
//recording arrival time of c2
struct timespec arrive_c2={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c2);
if (pid2 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid2 == 0)
{
//child c2
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd2[READ]);
long long int *nl=&n2;
pthread_t tid2[2];
pthread_attr_t at2[2];
pthread_attr_init(&at2[0]);
pthread_attr_init(&at2[1]);
//monitor thread of c2
if((pthread_create(&tid2[0],&at2[0],c2_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c2
if((pthread_create(&tid2[1],&at2[1],read_c2,nl))==-1)
{
printf("asdf");
}
pthread_join(tid2[0],NULL);
pthread_join(tid2[1],NULL);
//writing on pipe
write(fd2[WRITE],"Done Printing",14);
close(fd2[WRITE]);
}
else
{
pid_t pid3;
//third fork to create c3
pid3 = fork();
//recording arrival time of c3
struct timespec arrive_c3={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c3);
if (pid3 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid3 == 0)
{
//child c3
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd3[READ]);
long long int *nl=&n3;
pthread_t tid3[2];
pthread_attr_t at3[2];
pthread_attr_init(&at3[0]);
pthread_attr_init(&at3[1]);
//monitor thread of c2
if((pthread_create(&tid3[0],&at3[0],c3_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c2
if((pthread_create(&tid3[1],&at3[1],add_c3,nl))==-1)
{
printf("asdf");
}
pthread_join(tid3[0],NULL);
pthread_join(tid3[1],NULL);
//writing on pipe
write(fd3[WRITE],&sum_c3,sizeof(sum_c3));
close(fd3[WRITE]);
}
else
{
//parent process
//Creating shared memory
if((shmid=shmget(2051, 32, 0666 | IPC_CREAT))<0)
{
printf("error in shared memory");
exit(0);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char*)-1)
{
printf("error in shmptr");
exit(1);
}
shmptr[0]='0'+0;
//recording time of start of execution of c1
struct timespec a_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &a_c1);
//reading from c1
close(fd1[WRITE]);
long long int sum_c1;
read(fd1[READ],&sum_c1,sizeof(sum_c1));
close(fd1[READ]);
wait(NULL);
//recording time for end of execution of c1
struct timespec finish_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c1);
//printing result of c1
printf("\nsum is %lld\n",sum_c1);
//printing time taken by c1
printf("\ntime required for C1 %.5f sec\n",ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2);
a=ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2;
//FCFS scheduling using shared memory
shmptr[0]='1'+0;
//reading from c2
close(fd2[WRITE]);
char buf[14];
read(fd2[READ],buf,14);
//printing result of c1
printf("\n%s\n",buf);
close(fd2[READ]);
wait(NULL);
//recording time for end of execution of c2
struct timespec finish_c2={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c2);
//printing time taken by c2
printf("\ntime required for C2 %.5f sec\n",((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0]);
b=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0];
//FCFS scheduling using shared memory
shmptr[0]='2'+0;
//reading from c3
close(fd3[WRITE]);
long long int sum_c3;
read(fd3[READ],&sum_c3,sizeof(sum_c3));
close(fd3[READ]);
//printing result of c1
printf("sum is %lld ",sum_c3);
wait(NULL);
//recording time for end of execution of c2
struct timespec finish_c3={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c3);
//printing time taken by c3
printf("\ntime required for C3 %.5f sec\n",((double)finish_c3.tv_sec + 1.0e-9*finish_c3.tv_nsec) - ((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec));
// Detaching and Removing a Shared Memory Segment
shmctl(shmid,IPC_RMID,NULL);
c=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0];
//printing fcfs scheduling using shared memory time from child
printf("\nC1 starts at %d sec\n",0);
printf("\nC2 starts at %.5f sec\n",ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2);
printf("\nC3 starts at %.5f sec\n",((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2);
//printing fcfs turn around time
printf("\nTurnaround time for c1 %.5fsec\n",ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec));
printf("\nTurnaround time for c2 %.5fsec\n",((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ((double)arrive_c2.tv_sec + 1.0e-9*arrive_c2.tv_nsec));
printf("\nTurnaround time for c3 %.5fsec\n",((double)finish_c3.tv_sec + 1.0e-9*finish_c3.tv_nsec) - ((double)arrive_c3.tv_sec + 1.0e-9*arrive_c3.tv_nsec));
//printing waiting time
printf("\nWaiting time for c1 %.d sec\n",2);
printf("\nWaiting time for c2 %.5fsec\n",ti[0] - ((double)arrive_c2.tv_sec + 1.0e-9*arrive_c2.tv_nsec));
printf("\nWaiting time for c3 %.5fsec\n",((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ((double)arrive_c3.tv_sec + 1.0e-9*arrive_c3.tv_nsec));
//avg trt and avg wt of fcfs
double q1,q2,q3,w1,w2,w3;
q1=ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec);
q2=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ((double)arrive_c2.tv_sec + 1.0e-9*arrive_c2.tv_nsec);
q3=((double)finish_c3.tv_sec + 1.0e-9*finish_c3.tv_nsec) - ((double)arrive_c3.tv_sec + 1.0e-9*arrive_c3.tv_nsec);
w1=2;
w2=ti[0] - ((double)arrive_c2.tv_sec + 1.0e-9*arrive_c2.tv_nsec);
w3=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ((double)arrive_c3.tv_sec + 1.0e-9*arrive_c3.tv_nsec);
double avg_wt = (w1+w2+w3)/3;
double avg_tat = (q1+q2+q3)/3;
printf("\n\n Average Turn Around Time: \t%lf\n\n", avg_wt);
printf("\n\n Average Waiting Time: \t%lf\n\n", avg_tat);
}
}
}
}
else
{
//time quantum for round robin
printf("\n Enter Time quantum in sec : ");
double tq;
scanf("%lf",&tq);
ti=getSharedMem(sizeof(double)*6);
printf("Enter value of n1:");
scanf("%lld",&n1);
printf("Enter value of n2:");
scanf("%lld",&n2);
printf("Enter value of n3:");
scanf("%lld",&n3);
printf("\nEnter 0 to give n1 numbers as input or to add from 1 to n1 in c1\n");
int po;
scanf("%d",&po);
if(po==0)
{
printf("\nInput %lld numbers to add in C1\n",n1);
for(int i=0;i<n1;i++)
scanf("%lld",&arr[i]);
}
else
{
for(int i=0;i<n1;i++)
arr[i]=i;
}
printf("\n");
pid_t pid1;
//3 pipes for communicating result of 3 childs to parent
int fd1[2];
int fd2[2];
int fd3[2];
if(pipe(fd1)<0)
{
perror("pipe failed");
exit(1);
}
if(pipe(fd2)<0)
{
perror("pipe failed");
exit(1);
}
if(pipe(fd3)<0)
{
perror("pipe failed");
exit(1);
}
//first fork to create c1
pid1 = fork();
//recording arrival time of c1
struct timespec arrive_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c1);
if (pid1 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid1 == 0)
{ //child c1
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd1[READ]);
long long int *nl=&n1;
pthread_t tid[2];
pthread_attr_t at[2];
pthread_attr_init(&at[0]);
pthread_attr_init(&at[1]);
//monitor thread of c1
if((pthread_create(&tid[0],&at[0],c1_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c1
if((pthread_create(&tid[1],&at[1],add,nl))==-1)
{
printf("asdf");
}
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
//writing on pipe
write(fd1[WRITE],&sum_c1,sizeof(sum_c1));
close(fd1[WRITE]);
//recording time
struct timespec finish_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c1);
ti[0]=((double)finish_c1.tv_sec + 1.0e-9*finish_c1.tv_nsec);
//detach from shared memory
shmdt(shmptr);
}
else
{
pid_t pid2;
//second fork to create c2
pid2 = fork();
//recording arrival time of c2
struct timespec arrive_c2={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c2);
if (pid2 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid2 == 0)
{
//child c2
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd2[READ]);
long long int *nl=&n2;
pthread_t tid2[2];
pthread_attr_t at2[2];
pthread_attr_init(&at2[0]);
pthread_attr_init(&at2[1]);
//monitor thread of c2
if((pthread_create(&tid2[0],&at2[0],c2_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c2
if((pthread_create(&tid2[1],&at2[1],read_c2,nl))==-1)
{
printf("asdf");
}
pthread_join(tid2[0],NULL);
pthread_join(tid2[1],NULL);
//writing on pipe
write(fd2[WRITE],"Done Printing",14);
close(fd2[WRITE]);
}
else
{
pid_t pid3;
//third fork to create c3
pid3 = fork();
//recording arrival time of c3
struct timespec arrive_c3={0,0};
clock_gettime(CLOCK_MONOTONIC, &arrive_c3);
if (pid3 < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid3 == 0)
{
//child c3
sleep(2);
//attaching to shared memory
if((shmid=shmget(2051, 32, 0))==-1)
{
printf("error in read c1");
exit(1);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char *)-1)
{
printf("error in shmptr in c1");
exit(1);
}
close(fd3[READ]);
long long int *nl=&n3;
pthread_t tid3[2];
pthread_attr_t at3[2];
pthread_attr_init(&at3[0]);
pthread_attr_init(&at3[1]);
//monitor thread of c2
if((pthread_create(&tid3[0],&at3[0],c3_monitor,shmptr))==-1)
{
printf("asdf");
}
//task thread of c2
if((pthread_create(&tid3[1],&at3[1],add_c3,nl))==-1)
{
printf("asdf");
}
pthread_join(tid3[0],NULL);
pthread_join(tid3[1],NULL);
//writing on pipe
write(fd3[WRITE],&sum_c3,sizeof(sum_c3));
close(fd3[WRITE]);
}
else
{
//parent process
//Creating shared memory
if((shmid=shmget(2051, 32, 0666 | IPC_CREAT))<0)
{
printf("error in shared memory");
exit(0);
}
shmptr=shmat(shmid,0,0);
if(shmptr==(char*)-1)
{
printf("error in shmptr");
exit(1);
}
shmptr[0]='0'+0;
//recording time of start of execution of c1
struct timespec a_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &a_c1);
//reading from c1
close(fd1[WRITE]);
long long int sum_c1;
read(fd1[READ],&sum_c1,sizeof(sum_c1));
close(fd1[READ]);
wait(NULL);
//recording time for end of execution of c1
struct timespec finish_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c1);
//printing result of c1
printf("\nsum is %lld\n",sum_c1);
//printing time taken by c1
printf("\ntime required for C1 %.5f sec\n",ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2);
a=ti[0] - ((double)a_c1.tv_sec + 1.0e-9*a_c1.tv_nsec)-2;
//FCFS scheduling using shared memory
shmptr[0]='1'+0;
//reading from c2
close(fd2[WRITE]);
char buf[14];
read(fd2[READ],buf,14);
//printing result of c1
printf("\n%s\n",buf);
close(fd2[READ]);
wait(NULL);
//recording time for end of execution of c2
struct timespec finish_c2={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c2);
//printing time taken by c2
printf("\ntime required for C2 %.5f sec\n",((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0]);
b=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0];
//FCFS scheduling using shared memory
shmptr[0]='2'+0;
//reading from c3
close(fd3[WRITE]);
long long int sum_c3;
read(fd3[READ],&sum_c3,sizeof(sum_c3));
close(fd3[READ]);
//printing result of c1
printf("sum is %lld ",sum_c3);
wait(NULL);
//recording time for end of execution of c2
struct timespec finish_c3={0,0};
clock_gettime(CLOCK_MONOTONIC, &finish_c3);
//printing time taken by c3
printf("\ntime required for C3 %.5f sec\n",((double)finish_c3.tv_sec + 1.0e-9*finish_c3.tv_nsec) - ((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec));
// Detaching and Removing a Shared Memory Segment
shmctl(shmid,IPC_RMID,NULL);
c=((double)finish_c2.tv_sec + 1.0e-9*finish_c2.tv_nsec) - ti[0];
//round robin scheduling using queue data structure
enqueue(0);
enqueue(1);
enqueue(2);
int y1=0,l=0,z=0;
while(IsEmpty())
{
int u;
u=dequeue();
if(u==0)
{
shmptr[0]='0'+0;
struct timespec s_c1={0,0};
clock_gettime(CLOCK_MONOTONIC, &s_c1);
}
else if(u==1)
{
shmptr[0]='1'+0;
struct timespec s_c2={0,0};
clock_gettime(CLOCK_MONOTONIC, &s_c2);
}
else if(u==2)
{
shmptr[0]='2'+0;
struct timespec s_c3={0,0};
clock_gettime(CLOCK_MONOTONIC, &s_c3);
}
sleep(tq);
if(u==0)
{
wait(NULL);
close(fd1[WRITE]);
long long int sum_c1=-1;
read(fd1[READ],&sum_c1,sizeof(sum_c1));
close(fd1[READ]);
a=sum_c1;
if(sum_c1==-1)
{enqueue(0);y1++;}
}
else if(u==1)
{
wait(NULL);
close(fd2[WRITE]);
char buf[14];
read(fd2[READ],buf,14);
strncpy(er,buf,14);
close(fd2[READ]);
if(buf[0]=='\0')
{enqueue(1);l++;}
}
else if(u==2)
{
wait(NULL);
close(fd3[WRITE]);
long long int sum_c3=-1;
read(fd3[READ],&sum_c3,sizeof(sum_c3));
close(fd3[READ]);
b=sum_c3;
if(sum_c3==-1)
{enqueue(2);z++;}
}
}
double q[100000],w[100000],e[100000];
int ii=0,j=0,k=0;
double aa=a;
while(aa>0)
{
if(aa>tq)
{
q[ii]=tq;
ii++;
aa=aa-tq;
}
else
{
q[ii]=aa;
aa=0;
}
}
double bb=b;
while(bb>0)
{
if(bb>tq)
{
w[j]=tq;
j++;
bb=bb-tq;
}
else
{
w[j]=bb;
bb=0;
}
}
double cc=c;
while(cc>0)
{
if(cc>tq)
{
e[k]=tq;
k++;
cc=cc-tq;
}
else
{
e[k]=cc;
cc=0;
}
}
ii=ii+1;j=j+1;k=k+1;
int as=ii+j+k;
double wer[as];
int ia=0,ib=0,ic=0,id=0;
while(ib<ii && ic<j && id<k)
{
wer[ia]=q[ib];
ib++;
ia++;
wer[ia]=w[ic];
ic++;
ia++;
wer[ia]=e[id];
id++;
ia++;
}
while(ib<ii && ic<j)
{
wer[ia]=q[ib];
ib++;
ia++;
wer[ia]=w[ic];