· 7 years ago · Nov 02, 2018, 01:50 AM
1//CHILD, PARENT, ORPHAN, ZOMBIE, DAEMON
2//Predict the Output of the following program.
3#include<stdio.h>
4#include<sys/types.h>
5#include<unistd.h>
6int main() {
7
8 // make two process which run same
9 // program after this instruction
10 fork();
11
12 printf("Hello world! %d\n", getpid());
13 return 0;
14}
15
16
17//Calculate number of times hello is printed.
18#include <stdio.h>
19#include <sys/types.h>
20int main()
21{
22 fork();
23 fork();
24 fork();
25 printf("hello %d\n ", getpid());
26 return 0;
27}
28
29
30
31//Predict the Output of the following program
32#include<stdio.h>
33#include <sys/types.h>
34#include<unistd.h>
35void forkexample()
36{
37 // child process because return value zero
38 if (fork()==0)
39 printf("Hello from Child! %d\n", getpid());
40
41 // parent process because return value non-zero.
42 else
43 printf("Hello from Parent!%d\n", getppid());
44}
45int main()
46{
47 forkexample();
48 return 0;
49}
50
51
52//Predict the Output of the following program.
53#include<stdio.h>
54#include<sys/types.h>
55#include<unistd.h>
56
57void forkexample()
58{
59 int x = 1;
60
61 if (fork() == 0)
62 printf("%d Child has x = %d\n", getpid(),++x);
63 else
64 printf("%d Parent has x = %d\n", getppid(),--x);
65}
66int main()
67{
68 forkexample();
69 return 0;
70}
71
72
73
74//ORPHAN PROCESS
75#include <stdio.h>
76#include <unistd.h>
77int main()
78{
79 pid_t p;
80 /* create child process */
81 p = fork();
82 if (p == 0)
83 {
84 /* fork() returns Zero to child */
85 sleep(10);
86 }
87 printf("The child process pid is %d parent pid %d\n", getpid(), getppid());
88 /*parent/child waits for 20 secs and exits*/
89 sleep(20);
90 printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());
91 return 0;
92}
93
94
95
96//ZOMBIE PROCESS
97#include <stdio.h>
98#include <stdlib.h>
99#include <sys/types.h>
100#include <unistd.h>
101int main()
102{
103 // Fork returns process id. // in parent process.
104 pid_t child_pid = fork(); // Parent process.
105 if (child_pid > 0)
106 {
107 sleep(50);printf("Finally Parent with id = %d awakens whose child with id=%d already
108existed\n",getpid(),child_pid);
109 }
110 else
111 {
112 // Child process
113printf("Child process with id = %d exists while its parent with id =%d is still sleeping
114\n",getpid(),getppid());
115printf("Thus child with id=%d becomes zombie now\n",getpid());
116exit (0);
117 }
118 return 0;
119
120
121
122//DAEMON PROCESS
123#include <stdio.h>
124#include <stdlib.h>
125#include <sys/types.h>
126#include <unistd.h>
127int main()
128{
129 int pid = fork();
130 if (pid < 0)
131 {
132 printf("Fork Error ");
133 exit(1);
134 } /* fork error */
135 if (pid > 0)
136 {
137 printf("Parent exists");
138 exit(0);
139 } /* parent exits */
140 printf("Child continues at the background as daemon");
141 /* child (daemon) continues */
142 return 0;
143}
144
145
146
147
148//READERS_WRITERS_USING_SEMAPHORES
149#include <stdio.h>
150#include <conio.h>
151#include <stdbool.h>
152struct semaphore
153{
154 int mutex;
155 int rcount;
156 int rwait;
157 bool wrt;
158};
159voidaddR(struct semaphore *s)
160{
161 if (s->mutex == 0 && s->rcount == 0)
162 {
163 printf("\nSorry, File open in Write mode.\nNew Reader added to queue.\n");
164 s->rwait++;
165 }
166 else
167 {
168 printf("\nReader Process added.\n");
169 s->rcount++;
170 s->mutex--;
171 }
172 return;
173}
174voidaddW(struct semaphore *s)
175{
176 if (s->mutex == 1)
177 {
178 s->mutex--;
179 s->wrt = 1;
180 printf("\nWriter Process added.\n");
181 }
182 else if (s->wrt)
183 printf("\nSorry, Writer already operational.\n");
184 elseprintf("\nSorry, File open in Read mode.\n");
185 return;
186}
187voidremR(struct semaphore *s)
188{
189 if (s->rcount == 0)
190 printf("\nNo readers to remove.\n");
191 else
192 {
193 printf("\nReader Removed.\n");
194 s->rcount--;
195 s->mutex++;
196 }
197 return;
198}
199voidremW(struct semaphore *s)
200{
201 if (s->wrt == 0)
202 printf("\nNo Writer to Remove");
203 else
204 {
205 printf("\nWriter Removed\n");
206 s->mutex++;
207 s->wrt = 0;
208 if (s->rwait != 0)
209 {
210 s->mutex -= s->rwait;
211 s->rcount = s->rwait;
212 s->rwait = 0;
213 printf("%d waiting Readers Added.", s->rcount);
214 }
215 }
216}
217int main()
218{
219 struct semaphore S1 = {1, 0, 0};
220 while (1)
221 {
222 system("cls");
223 printf("Options :-\n1.Add Reader.\n2.Add Writer.\n3.Remove Reader.\n4.Remove Writer.\n5.Exit.\n\n\tChoice : ");
224 intch;
225 scanf("%d", &ch);
226 switch (ch)
227 {
228 case 1:
229 addR(&S1);
230 break;
231 case 2:
232 addW(&S1);
233 break;
234 case 3:
235 remR(&S1);
236 break;
237 case 4:
238 remW(&S1);
239 break;
240 case 5:
241 printf("\n\tGoodBye!");
242 getch();
243 return 0;
244 default:
245 printf("\nInvalid Entry!");
246 continue;
247 }
248 printf("\n\n<<<<<< Current Status >>>>>>\n\n\tMutex\t\t:\t%d\n\tActive Readers\t:\t%d\n\tWaiting Readers\t:\t%d\n\tWriter Active\t:\t%s\n\n", S1.mutex, S1.rcount, S1.rwait, (S1.mutex == 0 && S1.rcount == 0) ? "YES" : "NO");
249 system("pause");
250 }
251}
252
253
254
255//BANKERS_ALGORITHM_FOR_DEADLOCK_
256#include <stdio.h>
257#include <stdlib.h>
258int main()
259{
260 int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10], safeSequence[10];
261 int p, r, i, j, process, count;
262 count = 0;
263
264 printf("Enter the no of processes : ");
265 scanf("%d", &p);
266
267 for(i = 0; i< p; i++)
268 completed[i] = 0;
269
270 printf("\n\nEnter the no of resources : ");
271 scanf("%d", &r);
272
273 printf("\n\nEnter the Max Matrix for each process : ");
274 for(i = 0; i < p; i++)
275 {
276 printf("\nFor process %d : ", i + 1);
277 for(j = 0; j < r; j++)
278 scanf("%d", &Max[i][j]);
279 }
280
281 printf("\n\nEnter the allocation for each process : ");
282 for(i = 0; i < p; i++)
283 {
284 printf("\nFor process %d : ",i + 1);
285 for(j = 0; j < r; j++)
286 scanf("%d", &alloc[i][j]);
287 }
288
289 printf("\n\nEnter the Available Resources : ");
290 for(i = 0; i < r; i++)
291 scanf("%d", &avail[i]);
292
293 for(i = 0; i < p; i++)
294
295 for(j = 0; j < r; j++)
296 need[i][j] = Max[i][j] - alloc[i][j];
297
298 do
299 {
300 printf("\n Max matrix:\tAllocation matrix:\n");
301
302 for(i = 0; i < p; i++)
303 {
304 for( j = 0; j < r; j++)
305 printf("%d ", Max[i][j]);
306 printf("\t\t");
307 for( j = 0; j < r; j++)
308 printf("%d ", alloc[i][j]);
309 printf("\n");
310 }
311
312 process = -1;
313
314 for(i = 0; i < p; i++)
315 {
316 if(completed[i] == 0)//if not completed
317 {
318 process = i ;
319 for(j = 0; j < r; j++)
320 {
321 if(avail[j] < need[i][j])
322 {
323 process = -1;
324 break;
325 }
326 }
327 }
328 if(process != -1)
329 break;
330 }
331
332 if(process != -1)
333 {
334 printf("\nProcess %d runs to completion!", process + 1);
335 safeSequence[count] = process + 1;
336 count++;
337 for(j = 0; j < r; j++)
338 {
339 avail[j] += alloc[process][j];
340 alloc[process][j] = 0;
341 Max[process][j] = 0;
342 completed[process] = 1;
343 }
344 }
345 }
346 while(count != p && process != -1);
347
348 if(count == p)
349 {
350 printf("\nThe system is in a safe state!!\n");
351 printf("Safe Sequence : < ");
352 for( i = 0; i < p; i++)
353 printf("%d ", safeSequence[i]);
354 printf(">\n");
355 }
356 else
357 printf("\nThe system is in an unsafe state!!");
358
359}
360
361
362
363//FCFS
364#include <stdio.h>
365void fcfs(int); /* to implement first come first serve scheduling */
366void turn_around(int, int[], int[]); /* to calculate turn around time */
367int main()
368{
369 static int size; /* size of the queue */
370 printf("Enter the total number of processes: ");
371 scanf("%d", &size);
372 if (size == 0)
373 goto empty_queue; /* no process in the queue */
374 fcfs(size); /* first come first serve */
375 return 0; /* end of run */
376empty_queue:
377 printf("\nProcess queue is empty. End of run.\n");
378}
379/* fcfs : to implement first come first serve scheduling */
380void fcfs(int size)
381{
382 int i, j, result;
383 int arrival[size], burst[size], waiting[size];
384 for (i = 0; i < size; ++i)
385 waiting[i] = 0; /* initialize all values with 0 */
386 /* input the arrival time and burst time for each process */
387 for (i = 0; i < size; ++i)
388 {
389 printf("\nEnter arrival time for process %d : ", i + 1);
390 scanf("%d", &arrival[i]);
391 printf("Enter burst time for process %d : ", i + 1);
392 scanf("%d", &burst[i]);
393 }
394 /* first come first serve */
395 for (i = 1; i < size; ++i)
396 {
397 result = 0;
398 for (j = 0; j < i; ++j)
399 result += burst[j];
400 waiting[i] = result - arrival[i];
401 /* waiting time = starting time - arrival time */
402 }
403 /* print the waiting time */
404 printf("\nWaiting Time:\t");
405 for (i = 0; i < size; ++i)
406 printf("%d\t", waiting[i]);
407 /* average waiting time */
408 for (i = 0; i < size; ++i)
409 result += waiting[i];
410 result /= size;
411 printf("\nAverage Waiting Time:\t%d", result);
412 /* turn around time */
413 turn_around(size, burst, waiting);
414 printf("\n");
415}
416/* turn_around : to calculate turn around time for each process */
417void turn_around(int size, int burst[], int waiting[])
418{
419 int i;
420 int turn_around_time[size];
421 for (i = 0; i < size; ++i)
422 turn_around_time[i] = burst[i] + waiting[i]; /* turn around time = burst
423time + waiting time */
424 printf("\nTurn Around Time:\t");
425 for (i = 0; i < size; ++i)
426 printf("%d\t", turn_around_time[i]);
427}
428
429
430
431//SJF_NON-PREEMPTIVE
432#include<stdio.h>
433void main()
434{
435int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
436float avg_wt,avg_tat;
437printf("Enter number of process:");
438scanf("%d",&n);
439printf("\nEnter Burst Time:\n");
440for(i=0;i<n;i++)
441{
442printf("p%d:",i+1);
443scanf("%d",&bt[i]);
444p[i]=i+1; //contains process number
445}
446//sorting burst time in ascending order using selection sort
447for(i=0;i<n;i++)
448{
449pos=i;
450for(j=i+1;j<n;j++)
451{
452if(bt[j]<bt[pos])
453pos=j;
454}
455temp=bt[i];
456bt[i]=bt[pos];
457bt[pos]=temp;
458temp=p[i];
459p[i]=p[pos];
460p[pos]=temp;
461}
462wt[0]=0; //waiting time for first process will be zero
463//calculate waiting time
464for(i=1;i<n;i++)
465{
466wt[i]=0;
467for(j=0;j<i;j++)
468wt[i]+=bt[j];
469total+=wt[i];
470}
471avg_wt=(float)total/n; //average waiting time
472total=0;
473printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
474for(i=0;i<n;i++)
475{
476tat[i]=bt[i]+wt[i]; //calculate turnaround time
477total+=tat[i];
478printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
479}
480avg_tat=(float)total/n; //average turnaround time
481printf("\n\nAverage Waiting Time=%f",avg_wt);
482printf("\nAverage Turnaround Time=%f\n",avg_tat);
483}
484
485
486
487//SRTF_SJF(PREEMPTIVE)
488#include <stdio.h>
489int main()
490{
491int arrival_time[10], burst_time[10], temp[10];
492int i, smallest, count = 0, time, limit;
493double wait_time = 0, turnaround_time = 0, end;
494float average_waiting_time, average_turnaround_time;
495printf("\nEnter the Total Number of Processes:\t");
496scanf("%d", &limit);
497printf("\nEnter Details of %d Processes\n", limit);
498for(i = 0; i < limit; i++)
499{
500printf("\nEnter Arrival Time:\t");
501scanf("%d", &arrival_time[i]);
502printf("Enter Burst Time:\t");
503scanf("%d", &burst_time[i]);
504temp[i] = burst_time[i];
505}
506burst_time[9] = 9999;
507for(time = 0; count != limit; time++)
508{
509smallest = 9;
510for(i = 0; i < limit; i++)
511{
512if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] &&
513burst_time[i] > 0)
514{
515smallest = i;
516}
517}
518burst_time[smallest]--;
519if(burst_time[smallest] == 0)
520{
521count++;
522end = time + 1;
523wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
524turnaround_time = turnaround_time + end - arrival_time[smallest];
525}
526}
527average_waiting_time = wait_time / limit;
528average_turnaround_time = turnaround_time / limit;
529printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
530printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
531return 0;
532}
533
534
535
536//PRIORITY_SCHEDULING
537#include<stdio.h>
538int main()
539{
540int p[30],pr[30],key,loc,bt[30],temp,max,wt[30],ta[30],sum=0,i,j,n;
541wt[0]=0;
542printf("enter the number of processes=");
543scanf("%d",&n);
544printf("enter the burst time for each process");
545for(i=0;i<n;i++)
546{
547p[i]=i+1;
548printf("\np%d=",i+1);
549scanf("%d",&bt[i]);
550printf("\t priority of p%d=",i+1);
551scanf("%d",&pr[i]);
552}
553for(i=0;i<n;i++)
554{ max=i;
555for(j=i+1;j<n;j++)
556{
557if(pr[j] <pr[max])
558max=j;
559}
560temp=pr[max];
561pr[max]=pr[i];
562pr[i]=temp;
563temp=bt[max];
564bt[max]=bt[i];
565bt[i]=temp;
566temp=p[max];
567p[max]=p[i];
568p[i]=temp;
569}
570for(i=0;i<n;i++)
571{ wt[i+1]=bt[i]+wt[i];
572ta[i]=bt[i]+wt[i];
573sum+=ta[i];
574}
575for(i=0;i<n;i++)
576{
577printf("\n waiting time for p[%d]=%d",p[i],wt[i]);
578printf("\t turn around time for p[%d]=%d",p[i],ta[i]);
579}
580printf("\n\n average turn around=%d",sum/n);
581return 1;
582}
583
584
585
586//ROUND_ROBIN
587#include<stdio.h>
588int main()
589{
590int count,j,n,time,remain,flag=0,time_quantum;
591int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
592printf("Enter Total Process:\t ");
593scanf("%d",&n);
594remain=n;
595for(count=0;count<n;count++)
596{
597printf("Enter Arrival Time and Burst Time for Process Process Number
598%d :",count+1);
599scanf("%d",&at[count]);
600scanf("%d",&bt[count]);
601rt[count]=bt[count];
602}
603printf("Enter Time Quantum:\t");
604scanf("%d",&time_quantum);
605printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
606for(time=0,count=0;remain!=0;)
607{
608if(rt[count]<=time_quantum && rt[count]>0)
609{
610time+=rt[count];
611rt[count]=0;
612flag=1;
613}
614else if(rt[count]>0)
615{
616rt[count]-=time_quantum;
617time+=time_quantum;
618}
619if(rt[count]==0 && flag==1)
620{
621remain--;
622printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-
623bt[count]);
624wait_time+=time-at[count]-bt[count];
625turnaround_time+=time-at[count];
626flag=0;
627}
628if(count==n-1)
629count=0;
630else if(at[count+1]<=time)
631count++;
632else
633count=0;
634}
635printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
636printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);
637return 0;
638}
639
640
641
642//DINING_PHILOSIPHER
643#include<stdio.h>
644
645#define n 4
646
647int compltedPhilo = 0,i;
648
649struct fork{
650 int taken;
651}ForkAvil[n];
652
653struct philosp{
654 int left;
655 int right;
656}Philostatus[n];
657
658void goForDinner(int philID){ //same like threads concept here cases implemented
659 if(Philostatus[philID].left==10 && Philostatus[philID].right==10)
660 printf("Philosopher %d completed his dinner\n",philID+1);
661 //if already completed dinner
662 else if(Philostatus[philID].left==1 && Philostatus[philID].right==1){
663 //if just taken two forks
664 printf("Philosopher %d completed his dinner\n",philID+1);
665
666 Philostatus[philID].left = Philostatus[philID].right = 10; //remembering that he completed dinner by assigning value 10
667 int otherFork = philID-1;
668
669 if(otherFork== -1)
670 otherFork=(n-1);
671
672 ForkAvil[philID].taken = ForkAvil[otherFork].taken = 0; //releasing forks
673 printf("Philosopher %d released fork %d and fork %d\n",philID+1,philID+1,otherFork+1);
674 compltedPhilo++;
675 }
676 else if(Philostatus[philID].left==1 && Philostatus[philID].right==0){ //left already taken, trying for right fork
677 if(philID==(n-1)){
678 if(ForkAvil[philID].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
679 ForkAvil[philID].taken = Philostatus[philID].right = 1;
680 printf("Fork %d taken by philosopher %d\n",philID+1,philID+1);
681 }else{
682 printf("Philosopher %d is waiting for fork %d\n",philID+1,philID+1);
683 }
684 }else{ //except last philosopher case
685 int dupphilID = philID;
686 philID-=1;
687
688 if(philID== -1)
689 philID=(n-1);
690
691 if(ForkAvil[philID].taken == 0){
692 ForkAvil[philID].taken = Philostatus[dupphilID].right = 1;
693 printf("Fork %d taken by Philosopher %d\n",philID+1,dupphilID+1);
694 }else{
695 printf("Philosopher %d is waiting for Fork %d\n",dupphilID+1,philID+1);
696 }
697 }
698 }
699 else if(Philostatus[philID].left==0){ //nothing taken yet
700 if(philID==(n-1)){
701 if(ForkAvil[philID-1].taken==0){ //KEY POINT OF THIS PROBLEM, THAT LAST PHILOSOPHER TRYING IN reverse DIRECTION
702 ForkAvil[philID-1].taken = Philostatus[philID].left = 1;
703 printf("Fork %d taken by philosopher %d\n",philID,philID+1);
704 }else{
705 printf("Philosopher %d is waiting for fork %d\n",philID+1,philID);
706 }
707 }else{ //except last philosopher case
708 if(ForkAvil[philID].taken == 0){
709 ForkAvil[philID].taken = Philostatus[philID].left = 1;
710 printf("Fork %d taken by Philosopher %d\n",philID+1,philID+1);
711 }else{
712 printf("Philosopher %d is waiting for Fork %d\n",philID+1,philID+1);
713 }
714 }
715 }else{}
716}
717
718int main(){
719 for(i=0;i<n;i++)
720 ForkAvil[i].taken=Philostatus[i].left=Philostatus[i].right=0;
721
722 while(compltedPhilo<n){
723 /* Observe here carefully, while loop will run until all philosophers complete dinner
724 Actually problem of deadlock occur only thy try to take at same time
725 This for loop will say that they are trying at same time. And remaining status will print by go for dinner function
726 */
727 for(i=0;i<n;i++)
728 goForDinner(i);
729 printf("\nTill now num of philosophers completed dinner are %d\n\n",compltedPhilo);
730 }
731
732 return 0;
733}
734
735
736
737//PRODUCER_CONSUMER
738#include<stdio.h>
739#include<stdlib.h>
740
741int mutex=1,full=0,empty=3,x=0;
742
743int main()
744{
745 int n;
746 void producer();
747 void consumer();
748 int wait(int);
749 int signal(int);
750 printf("\n1.Producer\n2.Consumer\n3.Exit");
751 while(1)
752 {
753 printf("\nEnter your choice:");
754 scanf("%d",&n);
755 switch(n)
756 {
757 case 1: if((mutex==1)&&(empty!=0))
758 producer();
759 else
760 printf("Buffer is full!!");
761 break;
762 case 2: if((mutex==1)&&(full!=0))
763 consumer();
764 else
765 printf("Buffer is empty!!");
766 break;
767 case 3:
768 exit(0);
769 break;
770 }
771 }
772
773 return 0;
774}
775
776int wait(int s)
777{
778 return (--s);
779}
780
781int signal(int s)
782{
783 return(++s);
784}
785
786void producer()
787{
788 mutex=wait(mutex);
789 full=signal(full);
790 empty=wait(empty);
791 x++;
792 printf("\nProducer produces the item %d",x);
793 mutex=signal(mutex);
794}
795
796void consumer()
797{
798 mutex=wait(mutex);
799 full=wait(full);
800 empty=signal(empty);
801 printf("\nConsumer consumes item %d",x);
802 x--;
803 mutex=signal(mutex);
804}
805
806
807
808//SHARED MEMORY THROUGH IPC
809//For writer’s process
810#include <iostream>
811#include <sys/ipc.h>
812#include <sys/shm.h>
813#include <stdio.h>
814using namespace std;
815int main()
816{
817 // ftok to generate unique key
818 key_t key = ftok("shmfile",65);
819 // shmget returns an identifier in shmid
820 int shmid = shmget(key,1024,0666|IPC_CREAT);
821 // shmat to attach to shared memory
822 char *str = (char*) shmat(shmid,(void*)0,0);
823 cout<<"Write Data : ";
824 gets(str);
825 printf("Data written in memory: %s\n",str);
826 //detach from shared memory
827 shmdt(str);
828 return 0;
829}
830
831
832//For reader’s process
833#include <iostream>
834#include <sys/ipc.h>
835#include <sys/shm.h>
836#include <stdio.h>
837using namespace std;
838int main()
839{
840 // ftok to generate unique key
841 key_t key = ftok("shmfile",65);
842 // shmget returns an identifier in shmid
843 int shmid = shmget(key,1024,0666|IPC_CREAT);
844 // shmat to attach to shared memory
845 char *str = (char*) shmat(shmid,(void*)0,0);
846 printf("Data read from memory: %s\n",str);
847 //detach from shared memory
848 shmdt(str);
849 // destroy the shared memory
850 shmctl(shmid,IPC_RMID,NULL);
851 return 0;
852}
853
854
855
856//FIFO
857
858#include<stdio.h>
859int main()
860{
861int reference_string[10], page_faults = 0, m, n, s, pages, frames;
862printf("\nEnter Total Number of Pages:\t");
863scanf("%d", &pages);
864printf("\nEnter values of Reference String:\n");
865for(m = 0; m < pages; m++)
866{
867printf("Value No. [%d]:\t", m + 1);
868scanf("%d", &reference_string[m]);
869}
870printf("\nEnter Total Number of Frames:\t");
871{
872scanf("%d", &frames);
873}
874int temp[frames];
875for(m = 0; m < frames; m++)
876{
877temp[m] = -1;
878}
879for(m = 0; m < pages; m++)
880{
881s = 0;
882for(n = 0; n < frames; n++)
883{
884if(reference_string[m] == temp[n])
885{
886s++;
887page_faults--;
888}
889}
890page_faults++;
891if((page_faults <= frames) && (s == 0))
892{
893temp[m] = reference_string[m];
894}
895else if(s == 0)
896{
897temp[(page_faults - 1) % frames] = reference_string[m];
898}
899printf("\n");
900for(n = 0; n < frames; n++)
901{
902printf("%d\t", temp[n]);
903}
904}
905printf("\nTotal Page Faults:\t%d\n", page_faults);
906return 0;
907}
908
909
910//LRU
911#include<stdio.h>
912
913int findLRU(int time[], int n){
914 int i, minimum = time[0], pos = 0;
915
916 for(i = 1; i < n; ++i){
917 if(time[i] < minimum){
918 minimum = time[i];
919 pos = i;
920 }
921 }
922
923 return pos;
924}
925
926int main()
927{
928 int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
929 printf("Enter number of frames: ");
930 scanf("%d", &no_of_frames);
931
932 printf("Enter number of pages: ");
933 scanf("%d", &no_of_pages);
934
935 printf("Enter reference string: ");
936
937 for(i = 0; i < no_of_pages; ++i){
938 scanf("%d", &pages[i]);
939 }
940
941 for(i = 0; i < no_of_frames; ++i){
942 frames[i] = -1;
943 }
944
945 for(i = 0; i < no_of_pages; ++i){
946 flag1 = flag2 = 0;
947
948 for(j = 0; j < no_of_frames; ++j){
949 if(frames[j] == pages[i]){
950 counter++;
951 time[j] = counter;
952 flag1 = flag2 = 1;
953 break;
954 }
955 }
956
957 if(flag1 == 0){
958 for(j = 0; j < no_of_frames; ++j){
959 if(frames[j] == -1){
960 counter++;
961 faults++;
962 frames[j] = pages[i];
963 time[j] = counter;
964 flag2 = 1;
965 break;
966 }
967 }
968 }
969
970 if(flag2 == 0){
971 pos = findLRU(time, no_of_frames);
972 counter++;
973 faults++;
974 frames[pos] = pages[i];
975 time[pos] = counter;
976 }
977
978 printf("\n");
979
980 for(j = 0; j < no_of_frames; ++j){
981 printf("%d\t", frames[j]);
982 }
983 }
984
985 printf("\n\nTotal Page Faults = %d", faults);
986
987 return 0;
988}
989
990
991
992//LFU
993#include<stdio.h>
994int main()
995{
996int total_frames, total_pages, hit = 0;
997int pages[25], frame[10], arr[25], time[25];
998int m, n, page, flag, k, minimum_time, temp;
999printf("Enter Total Number of Pages:\t");
1000scanf("%d", &total_pages);
1001printf("Enter Total Number of Frames:\t");
1002scanf("%d", &total_frames);
1003for(m = 0; m < total_frames; m++)
1004{
1005frame[m] = -1;
1006}
1007for(m = 0; m < 25; m++)
1008{
1009arr[m] = 0;
1010}
1011printf("Enter Values of Reference String\n");
1012for(m = 0; m < total_pages; m++)
1013{
1014printf("Enter Value No.[%d]:\t", m + 1);
1015scanf("%d", &pages[m]);
1016}
1017printf("\n");
1018for(m = 0; m < total_pages; m++)
1019{
1020arr[pages[m]]++;
1021time[pages[m]] = m;
1022flag = 1;
1023k = frame[0];
1024for(n = 0; n < total_frames; n++)
1025{
1026if(frame[n] == -1 || frame[n] == pages[m])
1027{
1028if(frame[n] != -1)
1029{
1030hit++;
1031}
1032flag = 0;
1033frame[n] = pages[m];
1034break;
1035}
1036if(arr[k] > arr[frame[n]])
1037{
1038k = frame[n];
1039}
1040}
1041if(flag)
1042{
1043minimum_time = 25;
1044for(n = 0; n < total_frames; n++)
1045{
1046if(arr[frame[n]] == arr[k] && time[frame[n]] <
1047minimum_time)
1048{
1049temp = n;
1050minimum_time = time[frame[n]];
1051}
1052}
1053arr[frame[temp]] = 0;
1054frame[temp] = pages[m];
1055}
1056for(n = 0; n < total_frames; n++)
1057{
1058printf("%d\t", frame[n]);
1059}
1060printf("\n");
1061}
1062printf("Page Hit:\t%d\n", hit);
1063return 0;
1064}
1065
1066
1067
1068//OPTIMAL
1069#include<stdio.h>
1070int main()
1071{
1072int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1,
1073flag2, flag3, i, j, k, pos, max, faults = 0;
1074printf("Enter number of frames: ");
1075scanf("%d", &no_of_frames);
1076printf("Enter number of pages: ");
1077scanf("%d", &no_of_pages);
1078printf("Enter page reference string: ");
1079for(i = 0; i < no_of_pages; ++i){
1080scanf("%d", &pages[i]);
1081}
1082for(i = 0; i < no_of_frames; ++i){
1083frames[i] = -1;
1084}
1085for(i = 0; i < no_of_pages; ++i){
1086flag1 = flag2 = 0;
1087for(j = 0; j < no_of_frames; ++j){
1088if(frames[j] == pages[i]){
1089flag1 = flag2 = 1;
1090break;
1091}
1092}
1093if(flag1 == 0){
1094for(j = 0; j < no_of_frames; ++j){
1095if(frames[j] == -1){
1096faults++;
1097frames[j] = pages[i];
1098flag2 = 1;
1099break;
1100}
1101}
1102}
1103if(flag2 == 0){
1104flag3 =0;
1105for(j = 0; j < no_of_frames; ++j){
1106temp[j] = -1;
1107for(k = i + 1; k < no_of_pages; ++k){
1108if(frames[j] == pages[k]){
1109temp[j] = k;
1110break;
1111}
1112}
1113}
1114for(j = 0; j < no_of_frames; ++j){
1115if(temp[j] == -1){
1116pos = j;
1117flag3 = 1;
1118break;
1119}
1120}
1121if(flag3 ==0){
1122max = temp[0];
1123pos = 0;
1124for(j = 1; j < no_of_frames; ++j){
1125if(temp[j] > max){
1126max = temp[j];
1127pos = j;
1128}
1129}
1130}
1131frames[pos] = pages[i];
1132faults++;
1133}
1134printf("\n");
1135for(j = 0; j < no_of_frames; ++j){
1136printf("%d\t", frames[j]);
1137}
1138}
1139printf("\n\nTotal Page Faults = %d", faults);
1140return 0;
1141}
1142
1143
1144
1145//FILE_ALLOCATION
1146//SEQUENTIAL
1147#include<stdio.h>
1148#include<stdlib.h>
1149void main()
1150{
1151int f[50], i, st, len, j, c, k, count = 0;
1152for(i=0;i<50;i++)
1153f[i]=0;
1154printf("Files Allocated are : \n");
1155x: count=0;
1156printf("Enter starting block and length of files: ");
1157scanf("%d%d", &st,&len);
1158for(k=st;k<(st+len);k++)
1159if(f[k]==0)
1160count++;
1161if(len==count)
1162{
1163for(j=st;j<(st+len);j++)
1164if(f[j]==0)
1165{
1166f[j]=1;
1167printf("%d\t%d\n",j,f[j]);
1168}
1169if(j!=(st+len-1))
1170printf("The file is allocated to disk\n");
1171}
1172else
1173printf(" The file is not allocated \n");
1174printf("Do you want to enter more file(Yes - 1/No - 0)");
1175scanf("%d",&c);
1176if(c==1)
1177goto x;
1178else
1179exit;
1180}
1181
1182
1183//FILE_ALLOCATION
1184//INDEXED
1185#include<stdio.h>
1186#include<stdlib.h>
1187void main()
1188{
1189int f[50], index[50],i, n, st, len, j, c, k, ind,count=0;
1190for(i=0;i<50;i++)
1191f[i]=0;
1192x:printf("Enter the index block: ");
1193scanf("%d",&ind);
1194if(f[ind]!=1)
1195{
1196printf("Enter no of blocks needed and no of files for the index %d
1197on the disk : \n",
1198ind);
1199scanf("%d",&n);
1200}
1201else
1202{
1203printf("%d index is already allocated \n",ind);
1204goto x;
1205}
1206y: count=0;
1207for(i=0;i<n;i++)
1208{
1209scanf("%d", &index[i]);
1210if(f[index[i]]==0)
1211count++;
1212}
1213if(count==n)
1214{
1215for(j=0;j<n;j++)
1216f[index[j]]=1;
1217printf("Allocated\n");
1218printf("File Indexed\n");
1219for(k=0;k<n;k++)
1220printf("%d-------->%d : %d\n",ind,index[k],f[index[k]]);
1221}
1222else
1223{
1224printf("File in the index is already allocated \n");
1225printf("Enter another file indexed");
1226goto y;
1227}
1228printf("Do you want to enter more file(Yes - 1/No - 0)");
1229scanf("%d", &c);
1230if(c==1)
1231goto x;
1232else
1233exit(0);
1234}
1235
1236
1237
1238//FILE_ALLOCATION
1239//LINKED
1240#include<stdio.h>
1241#include<conio.h>
1242#include<stdlib.h>
1243int main()
1244{
1245int f[50], p,i, st, len, j, c, k, a;
1246for(i=0;i<50;i++)
1247f[i]=0;
1248printf("Enter how many blocks already allocated: ");
1249scanf("%d",&p);
1250printf("Enter blocks already allocated: ");
1251for(i=0;i<p;i++)
1252{
1253 scanf("%d",&a);
1254 f[a]=1;
1255}
1256x: printf("Enter index starting block and length: ");
1257scanf("%d%d", &st,&len);
1258k=len;
1259if(f[st]==0)
1260{
1261 for(j=st;j<(st+k);j++)
1262 {
1263 if(f[j]==0)
1264 {
1265 f[j]=1;
1266 printf("%d-------->%d\n",j,f[j]);
1267 }
1268 else
1269 {
1270 printf("%d Block is already allocated \n",j);
1271 k++;
1272 }
1273 }
1274}
1275else
1276printf("%d starting block is already allocated \n",st);
1277printf("Do you want to enter more file(Yes - 1/No - 0)");
1278scanf("%d", &c);
1279if(c==1)
1280 goto x;
1281else
1282 exit(0);
1283getch();
1284}
1285
1286
1287
1288//SHELL_PROGRAMMING
1289
1290#comment
1291clear echo
1292echo “Hello Worldâ€
1293
1294#Display command line arguments
1295echo “No. of CL Args are : $#â€
1296echo “$0 is scriptnameâ€
1297echo “$1 is first argâ€
1298echo “$2 is second argâ€
1299echo “all of them are: $*â€
1300
1301#check if arg is +ve or -ve
1302if[$# -eq 0]
1303then
1304echo “$0 : give integer inputâ€
1305exit 1
1306fi
1307if test $1 -gt 0
1308then
1309echo “$1 no. is +veâ€
1310else
1311echo “$1 no. is -veâ€
1312fi
1313
1314#Loop for Multiplication table
1315If [$# -eq 0]
1316then
1317echo “error, enter number along will file callâ€
1318exit 1
1319fi
1320n = $1
1321for i in 1 2 3 4 5 6 7 8 9 10
1322do
1323echo “$n * $i = ‘expr $1 \* $n’â€
1324done
1325
1326#to print file
1327if cat $1
1328then
1329echo -e “\n\n File $1, found & echoedâ€
1330fi