Round Robin and Scheduling Algorithms
Round Robin and Scheduling Algorithms
AIM:
To write a program for Round Robin scheduling
ALGORITHM:
Step-1:Start the program
1
CODING:
#include<stdio.h>
Void main( )
{
Int i,n, pid[15],bst[15],wait=0,tarnd,ts;
printf("\n Enter the no of process:");
scanf("%d",&n);
printf("\nEnter the values:");
for(i=1;i<=n;i++)
{
printf("\n Enter the process id:");
scanf("%d",&pid[i]);
printf("\nEnter the burst time:");
scanf("%d", &bst[i]);
}
printf("\nEnter the value for time slice:");
scanf("%d",&ts);
if(bst[i]>ts)
{
n=n+1;
bst[n]=bst[i]-ts;
bst[i]=bst[i]-bst[n];
pid[n]=pid[i];
}
tarnd=wait+bst[i];
2
printf("\n%d\t%d\t%d\t%d\t",pid[i],bst[i],wait,tarnd);
wait=tarnd;
3
OUTPUT:
Enter the no ofprocess:3
3 4 8 12
1 2 12 14
4
RESULT:
5
1(b) SJFS CHEDULING
AIM:
To write a program for Shortest job first algorithm
ALGORITHM:
6
CODING:
#include <stdio.h>
void main() {
scanf("%d", &n);
scanf("%d", &pid[i]);
scanf("%d", &bst[i]);
t = bst[i];
bst[i] = bst[j];
bst[j] = t;
7
// Swap process ID to keep track
t = pid[i];
pid[i] = pid[j];
pid[j] = t;
printf("\nPID\tBurst\tWait\tTurnaround");
wait = tarnd;
8
OUTPUT:
5 1 0 1
4 3 1 4
3 5 4 9
2 7 9 16
1 9 16 25
9
RESULT:
10
1(c) FCFS SCHEDULING
AIM:
ALGORITHM:
11
CODING:
#include <stdio.h>
void main() {
scanf("%d", &n);
scanf("%d", &pid[i]);
scanf("%d", &bst[i]);
printf("\nPID\tBurst\tWait\tTurnaround");
wait = tarnd;
12
OUTPUT:
1 5 0 5
2 5 5 10
3 5 10 15
4 5 15 20
5 5 20 25
13
RESULT:
14
1(d) PRIORITY SCHEDULING
AIM:
ALGORITHM:
15
CODING:
#include<stdio.h>
void main()
{
int bt[20], p[20], wt[20], pr[20], i, j, n, total = 0, pos, temp, avg_wt, avg_tat, tat[20];
printf("enter the number of process:");
scanf("%d", &n);
16
}
avg_wt = total / n;
total = 0;
printf("\nprocess\tbursttime\twaitingtime\tturnaroundtime");
for (i = 0; i < n; i++)
{
tat[i] = bt[i] + wt[i];
total += tat[i];
printf("\np[%d]\t%d\t%d\t%d\t", p[i], bt[i], wt[i], tat[i]);
}
avg_tat = total / n;
printf("\n Average Waiting time=%d", avg_wt);
printf("\nAverageturnaroundtime=%d", avg_tat);
}
17
OUTPUT:
Enter the number of process: 4
p[1]
burst time: 2
priority: 3
p[2]
burst time: 4
priority: 6
p[3]
burst time: 7
priority: 5
p[4]
burst time: 6
priority: 4
p[1] 2 0 2
p[4] 6 2 8
p[3] 7 8 15
p[2] 4 15 19
18
RESULT:
19
2(a) FILE ALLOCATION STRATEGIES(SEQUANTIAL FILE)
AIM:
ALGORITHM:
20
CODING:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int f[50], i, st, j, len, c, k;
21
OUTPUT:
3 -> 1
4 -> 1
5 -> 1
6 -> 1
7 -> 1
8 -> 1
9 -> 1
10 -> 1
11 -> 1
12 -> 1
22
RESULT:
23
2(b) FILE ALLOCATION STRATEGIES( INDEXED)
AIM:
ALGORITHM:
24
CODING:
#include <stdio.h>
void main()
{
Int f[50], i, k, j, inde[50], n, c, count = 0, p;
if (f[p] == 0)
{
f[p] = 1;
printf("enter no of files on index: ");
scanf("%d", &n);
}
else
{
printf("Block already allocated\n");
goto x;
}
25
for (j = 0; j < n; j++)
f[inde[j]] = 1;
printf("\nallocated");
printf("\nfile indexed");
if (c == 1)
goto x;
}
26
OUTPUT:
Allocated
File Indexed:
5 -> 6 : 1
5 -> 7 : 1
5 -> 8 : 1
5 -> 9 : 1
27
RESULT:
28
2(c) FILE ALLOCATION STRATEGIES( LINKED)
AIM:
ALGORITHM:
29
CODING:
#include <stdio.h>
void main() {
int f[50], p, i, j, k, a, st, len, n, c;
k = len;
for (j = st; j < (k + st); j++) {
if (f[j] == 0) {
f[j] = 1;
printf("\n%d -> Allocated", j);
} else {
printf("\n %d -> File is already allocated", j);
k++; // Increase k to compensate for already allocated block
}
}
30
scanf("%d", &c);
if (c == 1)
goto x;
}
for (j = 0; j < n; j++)
f[inde[j]] = 1;
printf("\nallocated");
printf("\nfile indexed");
if (c == 1)
goto x;
}
31
OUTPUT:
3->1
4->1
5->1
7->1
9->1
11->1
12->1
13->1
14->1
15->1
32
RESULT:
33
3) SEMAPHORES
AIM:
ALGORITHM:
34
CODING:
#include <stdio.h>
void main() {
in = out = pr = cr = 0;
scanf("%d", &n);
do {
scanf("%d", &ch);
switch (ch) {
case 1:
if (in >= n) {
} else {
pr = 1;
in++;
scanf("%d", &a[in]);
if (in == n)
pr = 0;
break;
case 2:
35
if (in == out) {
} else if (pr == 1) {
} else {
cr = 1;
in--;
if (in == 0)
cr = 0;
break;
36
OUTPUT:
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
37
The element 56 is consumed
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
1. Producer
2. Customer
3. Exit
38
RESULT:
39
4(a) FILE ORGANION TECHIQUES (Single Level Directory)
AIM:
To write the program for File Organization Techniques Single Level Directory
ALGORITHM:
40
CODING:
#include <stdio.h>
#include <string.h>
struct {
int fcnt;
} dir;
void main() {
int i, ch;
char f[30];
[Link] = 0;
scanf("%s", [Link]);
while (1) {
scanf("%d", &ch);
switch (ch) {
case 1:
41
printf("\nEnter the name of the file -- ");
scanf("%s", [Link][[Link]]);
[Link]++;
break;
case 2:
scanf("%s", f);
if (strcmp(f, [Link][i]) == 0) {
[Link]--;
break;
if (i == [Link])
break;
case 3:
scanf("%s", f);
if (strcmp(f, [Link][i]) == 0) {
break;
42
}
if (i == [Link])
break;
case 4:
if ([Link] == 0)
printf("\nDirectory Empty");
else {
printf("\t%s", [Link][i]);
break;
case 5:
exit(0);
default:
43
OUTPUT:
44
1. Create File 2. Delete File 3. Search File
File B is deleted
45
RESULT:
46
4(b) FILE ORGANION TECHIQUES (Two Level Directory)
AIM:
To write the program for File Organization Techniques Two Level Directory
ALGORITHM:
47
CODING:
#include <stdio.h>
#include <string.h>
struct {
int fcnt;
} dir[10];
void main() {
dcnt = 0;
while (1) {
scanf("%d", &ch);
switch (ch) {
case 1:
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
48
dcnt++;
printf("Directory created");
break;
case 2:
scanf("%s", d);
if (strcmp(d, dir[i].dname) == 0) {
scanf("%s", dir[i].fname[dir[i].fcnt]);
dir[i].fcnt++;
printf("File created");
break;
if (i == dcnt)
break;
case 3:
scanf("%s", d);
if (strcmp(d, dir[i].dname) == 0) {
scanf("%s", f);
49
if (strcmp(f, dir[i].fname[k]) == 0) {
dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
goto jmp;
goto jmp;
jmp:
break;
case 4:
scanf("%s", d);
if (strcmp(d, dir[i].dname) == 0) {
scanf("%s", f);
if (strcmp(f, dir[i].fname[k]) == 0) {
goto jmp1;
50
goto jmp1;
jmp1:
break;
case 5:
if (dcnt == 0)
printf("\nNo Directories");
else {
printf("\nDirectory\tFiles");
printf("\n%s\t\t", dir[i].dname);
printf("\t%s", dir[i].fname[k]);
break;
case 6:
exit(0);
default:
printf("Invalid choice!");
51
OUTPUT:
Directory created
Directory created
File created
52
Enter name of the directory -- DIR1
File created
File created
Directory Files
DIR1 A1 A2
DIR2 B1
53
1. Create Directory 2. Create File 3. Delete File
File A2 is deleted
54
RESULT:
55
4(c) FILE ORGANION TECHIQUES
(HIRARCHICAL DIRECTORY)
AIM:
ALGORITHM:
56
CODING:
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
struct tree_element {
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element* link[5];
};
void create(node** root, int lev, char* dname, int lx, int rx, int x);
void display(node* root);
void main() {
int gd = DETECT, gm;
node* root = NULL;
clrscr();
create(&root, 0, "root", 0, 639, 320);
clrscr();
initgraph(&gd, &gm, "c:\\tc\\BGI");
display(root);
getch();
closegraph();
}
void create(node** root, int lev, char* dname, int lx, int rx, int x) {
int i, gap;
if (*root == NULL) {
*root = (node*)malloc(sizeof(node));
printf("Enter name of dir/file (under %s): ", dname);
57
fflush(stdin);
gets((*root)->name);
(*root)->level = lev;
(*root)->y = 50 + lev * 50;
(*root)->x = x;
(*root)->lx = lx;
(*root)->rx = rx;
if ((*root)->ftype == 1) { // Directory
printf("No of subdirectories/files (for %s): ", (*root)->name);
scanf("%d", &(*root)->nc);
if ((*root)->nc == 0)
gap = rx - lx;
else
gap = (rx - lx) / (*root)->nc;
58
settextjustify(1, 1);
setfillstyle(1, BLUE);
setcolor(14);
if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}
59
OUTPUT:
60
Enter 1 for Dir / 2 for file: 2
61
RESULT:
62
4(d) FILE ORGANION TECHIQUES
(DAG)
AIM:
ALGORITHM:
63
CODING:
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>
#include <stdlib.h>
struct tree_element {
char name[20];
int x, y, ftype, lx, rx, nc, level;
struct tree_element *link[5];
};
typedef struct {
char from[20];
char to[20];
} link;
link L[10];
int nofl;
node *root;
void create(node **root, int lev, char *dname, int lx, int rx, int x);
void display(node *root);
void read_links();
void draw_link_lines();
void search(node *root, char *s, int *x, int *y);
void main() {
int gd = DETECT, gm;
root = NULL;
clrscr();
64
create(&root, 0, "root", 0, 639, 320);
read_links();
clrscr();
initgraph(&gd, &gm, "c:\\tc\\BGI");
draw_link_lines();
display(root);
getch();
closegraph();
}
void read_links() {
int i;
printf("How many links: ");
scanf("%d", &nofl);
for (i = 0; i < nofl; i++) {
printf("File/dir: ");
fflush(stdin);
gets(L[i].from);
printf("Username: ");
fflush(stdin);
gets(L[i].to);
}
}
void draw_link_lines() {
int i, x1, y1, x2, y2;
for (i = 0; i < nofl; i++) {
search(root, L[i].from, &x1, &y1);
search(root, L[i].to, &x2, &y2);
setcolor(LIGHTGREEN);
setlinestyle(3, 0, 1);
line(x1, y1, x2, y2);
setcolor(YELLOW);
setlinestyle(0, 0, 1);
}
65
}
void create(node **root, int lev, char *dname, int lx, int rx, int x) {
int i, gap;
if (*root == NULL) {
*root = (node *)malloc(sizeof(node));
printf("Enter name of dir/file (under %s): ", dname);
fflush(stdin);
gets((*root)->name);
(*root)->level = lev;
(*root)->y = 50 + lev * 50;
(*root)->x = x;
(*root)->lx = lx;
(*root)->rx = rx;
66
(*root)->link[i] = NULL;
if ((*root)->ftype == 1) {
printf("No of subdirectories/files (for %s): ", (*root)->name);
scanf("%d", &(*root)->nc);
if ((*root)->nc == 0)
gap = rx - lx;
else
gap = (rx - lx) / (*root)->nc;
if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}
if (root->ftype == 1)
bar3d(root->x - 20, root->y - 10, root->x + 20, root->y + 10, 0, 0);
67
else
fillellipse(root->x, root->y, 20, 20);
if (root != NULL) {
for (i = 0; i < root->nc; i++) {
line(root->x, root->y, root->link[i]->x, root->link[i]->y);
}
68
OUTPUT:
69
Enter 1 for Dir / 2 for File: 1
File/Dir: B
UserName: USER2
File/Dir: HTML
UserName: USER1
70
RESULT:
71
5 ) BANKERS ALGORITHM FOR DEADLOCK
AVOIDANCE
AIM:
ALGORITHM:
72
CODING:
#include <stdio.h>
int n, r;
void input();
void show();
void call();
void main() {
int i, j;
printf("Deadlock Avoidance\n");
input();
show();
call();
void input() {
int i, j;
scanf("%d", &n);
73
scanf("%d", &r);
scanf("%d", &max[i][j]);
scanf("%d", &all[i][j]);
scanf("%d", &avail[j]);
void show() {
int i, j;
printf("P%d\t", i);
74
for (j = 0; j < r; j++) {
printf("\t");
printf("\t");
if (i == 0) {
printf("\n");
void call() {
int safe[20], c1 = 0;
75
}
while (flag) {
flag = 0;
int c = 0;
if (finish[i] == 0) {
c++;
if (c == r) {
avail[k] += all[i][k];
finish[i] = 1;
flag = 1;
76
}
if (finish[i] == 1) {
c1++;
if (c1 == n) {
} else {
77
OUTPUT:
DEADLOCK AVOIDANCE
102
221
101
111
111
P1 111 221
78
RESULT:
79
6) DEADLOCK DETECTION
AIM:
ALGORITHM:
80
CODING:
#include <stdio.h>
int n, r;
void input();
void show();
void call();
void main() {
int i, j;
printf("Deadlock Detection\n");
input();
show();
call();
getch();
void input() {
int i, j;
scanf("%d", &n);
scanf("%d", &r);
81
printf("Enter the max matrix:\n");
scanf("%d", &max[i][j]);
scanf("%d", &all[i][j]);
scanf("%d", &avail[j]);
void show() {
int i, j;
printf("Process\tAllocation\tMaximum\t\tAvailable\n");
printf("\nP%d:\t", i);
printf("\t\t");
82
for (j = 0; j < r; j++)
if (i == 0) {
printf("\t\t");
printf("\n");
void call() {
finish[i] = 0;
while (flag) {
flag = 0;
83
if (finish[i] == 0) {
c = 0;
c++;
finish[i] = 1;
flag = 1;
if (finish[i] == 0) {
dead[deadCount++] = i;
if (deadCount > 0) {
84
printf("P%d ", dead[i]);
printf("\n");
} else {
85
OUTPUT:
DEADLOCK DETECTION
Enter no of processes:
111
212
100
201
000
P0 P1
86
RESULT:
87
7(a) PAGE REPLACEMENT ALGORITHM (FIFO)
AIM:
To write the program for Page replacement algorithm FIFO
ALGORITHM:
Step-1: Start the program.
Step-2: Read the number of pages n.
Step-3: Read the number of page numbers.
Step-4: Read the page numbers into an array a[i].
Step-5: Initialize a val[i] = 0 array to check page hits.
Step-6: Print the results.
Step-7: Stop the program
88
CODING:
#include <stdio.h>
void main() {
char f = 'F';
scanf("%d", &n);
scanf("%d", &b[i]);
if (p == 0) {
if (q >= 3)
q = 0;
a[q] = b[i];
q++;
if (q1 < 3) {
q1 = q;
89
printf("\n%d\t", b[i]);
m++;
p = 0;
if (b[i + 1] == a[k])
90
OUTPUT:
1 1 --> F
2 12 --> F
3 123 --> F
2 123
4 423 --> F
1 413 --> F
91
RESULT:
92
7(b) PAGE REPLACEMENT ALGORITHM (LRU)
AIM:
To write the program for Page replacement algorithm LRU
ALGORITHM:
Step-1: Start the program
- Page array
- If not present (page fault), find the least recently used page using counter values
93
CODING:
#include <stdio.h>
void main()
int k, i, q1 = 1, j, u, h;
char f = 'F';
scanf("%d", &n);
scanf("%d", &b[i]);
if (p == 0) {
if (q >= 3) q = 0;
a[q] = b[i];
q++;
if (q1 < 3) {
q1 = q;
printf("\n%d\t", b[i]);
printf("%d", a[h]);
94
if ((p == 0) && (q <= 3)) {
m++;
p = 0;
g = 0;
if (q1 == 3) {
if (b[i + 1] == a[k])
p = 1;
u = 0;
k = i;
if (b[k] == a[j])
u++;
k--;
if (u == 0)
q = j;
} else {
if (b[i + 1] == a[k])
95
p = 1;
96
OUTPUT:
-------------------------------
2 2 --> F
3 23 --> F
2 23
1 231 --> F
5 531 --> F
2 531
4 431 --> F
5 431
3 431 --> F
2 231 --> F
3 231
2 231
97
RESULT:
98
7(c) PAGE REPLACEMENT ALGORITHM (LFU)
AIM:
To write the program for Page replacement algorithm LFU
ALGORITHM:
99
CODING:
#include <stdio.h>
void main() {
int rs[50], i, j, k, m, f;
scanf("%d", &m);
scanf("%d", &rs[i]);
scanf("%d", &f);
cntr[i] = 0;
a[i] = -1;
100
for(j = 0; j < f; j++) {
if(rs[i] == a[j]) {
cntr[j]++;
break;
min = 0;
min = k;
a[min] = rs[i];
cntr[min] = 1;
pf++;
printf("%d\t", a[j]);
if(j == f)
printf("\n");
101
OUTPUT:
1 -1 -1 PF No: 1
1 2 -1 PF No: 2
1 2 3 PF No: 3
4 2 3 PF No: 4
4 5 3 PF No: 5
4 5 3
4 5 3
4 5 3
4 5 3
1 5 3 PF No: 6
1 4 3 PF No: 7
1 4 5 PF No: 8
102
RESULT:
103
8 )SHARED MEMORIES AND IPC
AIM:
ALGORITHM:
104
CODING:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
void main() {
int i;
void* shared_memory;
char buff[100];
int shmid;
strcpy(shared_memory, buff);
105
OUTPUT:
106
RESULT:
107
9 ) PAGE TECHNIQUES AND MEMORY MANAGEMENT
AIM:
ALGORITHM:
108
CODING:
#include <stdio.h>
#include <stdlib.h>
void main() {
scanf("%d", &np);
scanf("%d", &ps);
109
OUTPUT:
1024
110
RESULT:
111
10 ) PAGE TECHNIQUE AND MEMORY MANAGEMENT
AIM:
ALGORITHM:
112
CODING:
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int counter = 0;
unsigned long i = 0;
counter += 1;
return NULL;
int main(void) {
113
int i = 0;
int err;
while (i < 2) {
if (err != 0)
i++;
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
114
OUTPUT:
Job 1 started
Job 2 started
Job 2 finished
Job 1 finished
115
RESULT:
116