OS
Banker's Algorithm
1#include <stdio.h>
2
3int main() {
4 int n = 5; // Number of processes
5 int m = 3; // Number of resource types
6 int i, j, k;
7
8 int alloc[5][3] = {
9 { 0, 1, 0 }, // P0
10 { 2, 0, 0 }, // P1
11 { 3, 0, 2 }, // P2
12 { 2, 1, 1 }, // P3
13 { 0, 0, 2 } // P4
14 };
15
16 int max[5][3] = {
17 { 7, 5, 3 }, // P0
18 { 3, 2, 2 }, // P1
19 { 9, 0, 2 }, // P2
20 { 2, 2, 2 }, // P3
21 { 4, 3, 3 } // P4
22 };
23
24 int avail[3] = { 3, 3, 2 }; // Available Resources
25
26 int newRequest[5][3] = {0}, ProNo;
27
28 printf("Banker's Algorithm\n");
29
30 printf("For the new request: enter process number (0 to 4)\n");
31 scanf("%d", &ProNo);
32
33 printf("Enter the new request of process P%d in the order resources [A B C]\n", ProNo);
34 scanf("%d", &newRequest[ProNo][0]);
35 scanf("%d", &newRequest[ProNo][1]);
36 scanf("%d", &newRequest[ProNo][2]);
37
38 // Check if request is less than or equal to available
39 if (newRequest[ProNo][0] > avail[0] ||
40 newRequest[ProNo][1] > avail[1] ||
41 newRequest[ProNo][2] > avail[2]) {
42 printf("Request cannot be granted now as it exceeds available resources.\n");
43 printf("Process must wait.\n");
44 return 0;
45 }
46
47 // Pretend allocation
48 avail[0] -= newRequest[ProNo][0];
49 avail[1] -= newRequest[ProNo][1];
50 avail[2] -= newRequest[ProNo][2];
51
52 alloc[ProNo][0] += newRequest[ProNo][0];
53 alloc[ProNo][1] += newRequest[ProNo][1];
54 alloc[ProNo][2] += newRequest[ProNo][2];
55
56 // Calculate need matrix
57 int need[n][m];
58 for (i = 0; i < n; i++) {
59 for (j = 0; j < m; j++) {
60 need[i][j] = max[i][j] - alloc[i][j];
61 }
62 }
63
64 int f[n], ans[n], ind = 0;
65 for (k = 0; k < n; k++) {
66 f[k] = 0;
67 }
68
69 int flag;
70 int y = 0;
71
72 for (k = 0; k < 5; k++) {
73 for (i = 0; i < n; i++) {
74 if (f[i] == 0) {
75 flag = 0;
76 for (j = 0; j < m; j++) {
77 if (need[i][j] > avail[j]) {
78 flag = 1;
79 break;
80 }
81 }
82 if (flag == 0) {
83 ans[ind++] = i; // Safe sequence
84 for (y = 0; y < m; y++) {
85 avail[y] += alloc[i][y];
86 }
87 f[i] = 1;
88 }
89 }
90 }
91 }
92
93 int safe = 1;
94 for (i = 0; i < n; i++) {
95 if (f[i] == 0) {
96 safe = 0;
97 break;
98 }
99 }
100
101 if (safe == 0) {
102 printf("Request cannot be granted now, as it may lead to Deadlock.\n");
103 printf("There is no safe sequence.\n");
104 printf("Your request is not granted to avoid Deadlock.\n");
105 printf("Process has to wait.\n");
106 } else {
107 printf("Request can be granted, as a safe sequence is present.\n");
108 printf("There will be no deadlock.\n");
109 printf("Following is the SAFE Sequence:\n");
110 for (i = 0; i < n - 1; i++)
111 printf("P%d -> ", ans[i]);
112 printf("P%d\n", ans[n - 1]);
113 }
114
115 return 0;
116}
OUTPUTBanker's AlgorithmFor the new request: enter process number (0 to 4)1Enter the new request of process P1 in the order resources [A B C]102Request can be granted, as a safe sequence is present.There will be no deadlock.Following is the SAFE Sequence:P1 -> P3 -> P4 -> P0 -> P2
Producer Consumer Problem
1#include<unistd.h>
2#include<stdio.h>
3#include<pthread.h>
4#include<semaphore.h>
5int buf[5], f, r; //Circular queue
6sem_t mutex, full, empty;
7void *produce(void *arg)
8{
9 int i;
10 for(i=0;i<10;i++)
11 {
12 sem_wait(&empty);//Wait of empty slot and decrement the empty
13 sem_wait(&mutex); //Wait if consumer is comsuming a item
14
15 printf("produced item is %d\n",i);
16 buf[(++r)%5]=i;
17 sleep(1);
18
19 sem_post(&mutex); // signal the consumer to consume the item mutex=0
20 sem_post(&full); // full will be increamented
21
22 }
23}
24void *consume(void *arg)
25{
26 int item,i;
27 for(i=0;i<10;i++)
28 {
29 sem_wait(&full); //Wait for item and decrement the full
30 sem_wait(&mutex); // Wait if producer s producing a item
31
32 item=buf[(++f)%5];
33 printf("consumed item is %d\n",item);
34 sleep(1);
35
36 sem_post(&mutex); // signal the producer to produce the item mutex=0
37 sem_post(&empty); // incrementing the empty variable
38 }
39}
40int main()
41{
42 pthread_t tid1,tid2;
43 sem_init(&mutex, 0, 1);
44 sem_init(&full, 0, 0);
45 sem_init(&empty, 0, 5);
46 pthread_create(&tid1, NULL, produce, NULL);
47 pthread_create(&tid2,NULL, consume, NULL);
48 pthread_join(tid1,NULL);
49 pthread_join(tid2,NULL);
50
51 return 0;
52}
OUTPUTproduced item is 0produced item is 1produced item is 2produced item is 3produced item is 4consumed item is 0consumed item is 1consumed item is 2^C
Paging technique of memory management
1#include<stdio.h>
2int main()
3{
4int ms, Fsize, NoFrames, NoProcess, RemFrames, i, j, x, y, pa, offset;
5int NoPages, PageTable[10];
6printf("\nEnter the memory size -- ");
7scanf("%d", &ms);
8printf("\nEnter the Frame size -- ");
9scanf("%d", &Fsize);
10NoFrames = ms/Fsize;
11printf("\n The no. of Frames available in memory are -- %d ", NoFrames);
12 RemFrames = NoFrames;
13
14 printf("\n Enter no. of pages required : ");
15 scanf("%d", &NoPages);
16
17 if(NoPages >RemFrames)
18 {
19 printf("\n Memory is Full");
20 return (0);
21 }
22 RemFrames = RemFrames - NoPages;
23 printf("\n ---Enter page table --- ");
24 for(j=0;j<NoPages;j++)
25 scanf("%d", &PageTable[j]);
26
27 printf("\n ---page table --- ");
28 printf(" \n| PNo || FNo |");
29 for(j=0;j<NoPages;j++)
30 printf(" \n| %d || %d |",j,PageTable[j]);
31
32 int yes=1;
33 do
34 {
35 printf("\nEnter Logical Address to find Physical Address ");
36 printf("\nEnter page number and offset -- ");
37 scanf(" %d %d",&y, &offset);
38 if( y>=NoPages || offset>=Fsize)
39 {
40 printf("\n trap: Page Number or offset illegal");
41 return(0);
42 }
43 else
44 {
45 pa = (PageTable[y]*Fsize) + offset;
46
47 printf("Fsize=%d,offset=%d\n,frame no=%d",Fsize,offset,PageTable[y]);
48 printf("\n The Physical Address is -- %d", pa);
49 }
50 printf("\nContinue : yes=1,no=0\n");
51 scanf("%d",&yes);
52
53 }while(yes==1);
54
55 return 0;
56}
OUTPUTEnter the memory size -- 32Enter the Frame size -- 4The no. of Frames available in memory are -- 8Enter no. of pages required : 4---Enter page table --- 5612---page table ---| PNo || FNo || 0 || 5 || 1 || 6 || 2 || 1 || 3 || 2 |Enter Logical Address to find Physical AddressEnter page number and offset -- 0 0Fsize=4,offset=0,frame no=5The Physical Address is -- 20Continue : yes=1,no=01Enter Logical Address to find Physical AddressEnter page number and offset -- 1 3Fsize=4,offset=3,frame no=6The Physical Address is -- 27Continue : yes=1,no=0^C
Segmentation technique of Memory Management
1#include<stdio.h>
2int main()
3{
4 int i, y, PhyAddre, offset;
5 int NoSeg, SegmentTable[10][10];
6
7 printf("\nEnter number segments -- ");
8 scanf("%d", &NoSeg);
9 printf("\nEnter the Segmentation Table data: Base value & Limit Value\n -- ");
10 for(i=0;i<NoSeg;i++)
11 scanf("%d%d", &SegmentTable[i][0],&SegmentTable[i][1]);
12
13 printf("\n----Enter the Segmentation Table data---\n");
14 printf("Segment NO || Base || Limit || \n");
15 for(i=0;i<NoSeg;i++)
16 printf("|| %d || %d || %d || \n",i,SegmentTable[i][0],SegmentTable[i][1]);
17
18 int yes=1;
19
20do
21 {
22 printf("\nEnter Logical Address to find Physical Address ");
23 printf("\nEnter segment number and offset\n ");
24 scanf(" %d %d",&y, &offset);
25
26 if(offset>SegmentTable[y][1])
27 {
28 printf("Trap : Addressing Error\n");
29 }
30 else
31 {
32 PhyAddre=SegmentTable[y][0]+offset;
33 printf("\n The Physical Address is -- %d", PhyAddre);
34 printf("\nContinue : yes=1,no=0\n");
35 scanf("%d",&yes);
36 }
37 }while(yes==1);
38
39 return 0;
40}
OUTPUTEnter number of segments -- 5Enter the Segmentation Table data: Base value & Limit Value--219 6002300 1490 1001327 5801952 96----Enter the Segmentation Table data---Segment NO || Base || Limit |||| 0 || 219 || 600 |||| 1 || 2300 || 14 |||| 2 || 90 || 100 |||| 3 || 1327 || 580 |||| 4 || 1952 || 96 ||Enter Logical Address to find Physical AddressEnter segment number and offset0 430The Physical Address is -- 649Continue : yes=1,no=01Enter Logical Address to find Physical AddressEnter segment number and offset1 10The Physical Address is -- 2310Continue : yes=1,no=01Enter Logical Address to find Physical Address
Enter segment number and offset2 500Trap : Addressing ErrorContinue : yes=1,no=0^C
Page Replacement Policies (FCFS)
1#include<stdio.h>
2int main()
3{
4 int i,j,n,a[50],frame[10],no,k,avail,count=0;
5 printf("\n ENTER THE NUMBER OF PAGES:\n");
6 scanf("%d",&n);
7 printf("\n ENTER THE PAGE NUMBER :\n");
8 for(i=1;i<=n;i++)
9 scanf("%d",&a[i]);
10
11 printf("\n ENTER THE NUMBER OF FRAMES :");
12 scanf("%d",&no);
13
14 for(i=0;i<no;i++)
15 frame[i]= -1;
16 j=0;
17 printf("\tref string\t page frames\n");
18
19 for(i=1;i<=n;i++)
20 {
21 printf("%d\t\t",a[i]);
22 avail=0;
23 for(k=0;k<no;k++)
24 if(frame[k]==a[i])
25 avail=1;
26 if (avail==0)
27 {
28 frame[j]=a[i];
29 j=(j+1)%no;
30 count++;
31 for(k=0;k<no;k++)
32 printf("%d\t",frame[k]);
33 }
34 printf("\n");
35 }
36 printf("Page Fault Is %d",count);
37 return 0;
38}
OUTPUTENTER THE NUMBER OF PAGES: 20ENTER THE PAGE NUMBER : 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1ENTER THE NUMBER OF FRAMES :3ref string page frames7 7 -1 -10 7 0 -11 7 0 12 2 0 103 2 3 10 2 3 04 4 3 02 4 2 03 4 2 30 0 2 332 1 0 1 32 0 1 2017 7 1 20 7 0 21 7 0 1Page Fault Is 15