3B.
TWO LEVEL DIRECTORY ORGANIZATION:
SOURCE CODE:
#include <stdio.h>
#include<conio.h>
#include <string.h>
struct Directory {
char dname[10];
char fname[10][10];
int fcnt;
};
void main() {
struct Directory dir[10];
int i, ch, dcnt, k;
char f[30], d[30];
clrscr();
dcnt = 0;
while (1) {
printf(" --------- ");
printf("\n\n1. Create Directory\n2. Create File\n3. Delete File");
printf("\n4. Search File\n5. Display\n6. Exit\n");
printf("\n\nEnter your Choice:");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter name of directory: ");
scanf("%s", dir[dcnt].dname);
dir[dcnt].fcnt = 0;
dcnt++;
printf("Directory created\n");
break;
case 2:
printf("\nEnter name of the directory: ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
printf("Enter name of the file: ");
scanf("%s", f);
strcpy(dir[i].fname[dir[i].fcnt], f);
dir[i].fcnt++;
printf("File created\n");
break;
}
if (i == dcnt)
printf("Directory %s not found\n", d); break;
case 3:
printf("\nEnter name of the directory: ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
printf("Enter name of the file: ");
scanf("%s", f);
for (k = 0; k < dir[i].fcnt; k++) {
if (strcmp(f, dir[i].fname[k]) == 0) {
printf("File %s is deleted\n", f);
dir[i].fcnt--;
strcpy(dir[i].fname[k], dir[i].fname[dir[i].fcnt]);
break;
if (k == dir[i].fcnt)
printf("File %s not found\n", f);
break;
}
}
if (i == dcnt)
printf("Directory %s not found\n", d);
break;
case 4:
printf("\nEnter name of the directory: ");
scanf("%s", d);
for (i = 0; i < dcnt; i++) {
if (strcmp(d, dir[i].dname) == 0) {
printf("Enter the name of the file: ");
scanf("%s", f);
for (k = 0; k < dir[i].fcnt; k++) {
if (strcmp(f, dir[i].fname[k]) == 0) {
printf("File %s is found\n", f);
break;
if (k == dir[i].fcnt)
printf("File %s not found\n", f);
break;
}
}
if (i == dcnt)
printf("Directory %s not found\n", d);
break;
case 5:
if (dcnt == 0)
printf("\nNo Directories\n");
else {
printf("\nDirectory\tFiles\n");
for (i = 0; i < dcnt; i++) {
printf("%s\t\t", dir[i].dname);
for (k = 0; k < dir[i].fcnt; k++)
printf("\t%s", dir[i].fname[k]);
printf("\n");
break;
default:
exit(0);
}
}
OUTPUT:
EXPERIMENT-5: Simulate Bankers Algorithm for
Dead Lock Prevention.
AIM: To simulate Banker's algorithm to prevent a Dead Lock.
DESCRIPTION:
Dead Lock is defined as the permanent blocking of a set of processes that
either compete for system resources or communicate with each other. There is
no efficient solution for this, and this Involves conflicting needs for resources
by two or more processes. We can say the conditions for deadlock as
• Mutual Exclusion
• No preemption
• Hold and wait
This algorithm verifies the attainment of dead lock for the given system.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int pno, rno, i, j, k, op, p, re, n;
int max[10][10], curr[10][10];
int req[10][10], av[10], r[10], a, value;
void main()
{
clrscr();
printf("Enter the No. of Processes: ");
scanf("%d", &pno);
printf("\nEnter the No. of Resources: ");
scanf("%d", &rno);
printf("\nEnter the Max No. of instances of each resource\n");
for (i = 0; i < rno; i++)
printf("Resource %c: ", 'A' + i);
scanf("%d", &r[i]);
av[i] = r[i];
printf("\nEnter the max demand of each Process for a resource\n");
for (i = 0; i < pno; i++)
printf("\nFor Process P%d\n", i);
for (j = 0; j < rno; j++)
printf("Resource %c: ", 'A' + j);
scanf("%d", &max[i][j]);
}
printf("\nEnter the max no. of instances of a resource allocated to a process:\n");
for (i = 0; i < pno; i++)
printf("\nFor Process P%d\n", i);
for (j = 0; j < rno; j++)
printf("Resource %c: ", 'A' + j);
scanf("%d", &curr[i][j]);
printf("\nThe system needs:\n");
for (i = 0; i < pno; i++)
for (j = 0; j < rno; j++)
req[i][j] = max[i][j] - curr[i][j];
printf("%d ", req[i][j]);
}
printf("\n");
value = safe(pno, rno);
if (value == 0)
printf("\n\nIt is not safe in the beginning.");
else
printf("\n\nIt is safe in the beginning.");
getch();
int safe(int n, int k)
int rest[10], currav[10], temp[10], pos = 0;
int count = 0, possible = 1;
for (i = 0; i < n; i++)
rest[i] = 1;
for (i = 0; i < k; i++)
currav[i] = av[i];
while (possible)
j = 0;
for (i = 0; i < n; i++)
if (rest[i] != 0)
for (p = 0; p < k; p++)
if (req[i][p] <= currav[p])
currav[p] += curr[i][p];
else
break;
if (p == k)
rest[i] = 0;
j++;
if (rest[i] == 0)
{
count++;
temp[pos++] = i;
if (count == n)
possible = 0;
if (j == 0)
possible = 0;
for (i = 0; i < n; i++)
if (rest[i] != 0)
return 0;
printf("\nResourses are allocated in this Order:\n");
for (i = 0; i < n; i++)
printf("P%d ", temp[i]);
return 1;
}
OUTPUT:
EXPERIMENT-6: Simulate the following page
replacement algorithms.
6A. FIFO Page Replacement Algorithm:
AIM: To simulate FIFO page replacement algorithm.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int fifo();
int n, pg[20], frm,
fr[10], pgf = 0, i, j; float
pfr = 0;
void main() {
clrscr();
printf("Enter the number of page elements in the reference string: ");
scanf("%d", &n);
printf("\nEnter the reference string: ");
for (i = 0; i < n; i++) {
scanf("%d", &pg[i]);
printf("\nEnter the number of frames: ");
scanf("%d", &frm);
pgf = fifo();
printf("\nNumber of page faults: %d", pgf);
pfr = (float)pgf / n;
printf("\nPage fault ratio: %f", pfr);
getch();
int fifo() {
int pos, i = 0, hit = 0;
for (j = 0; j < frm; j++) {
fr[j] = -1; // Initialize frame array
while (i < n) {
int found = 0;
for (pos = 0; pos < frm; pos++) {
if (fr[pos] == pg[i]) {
hit++;
found = 1;
break;
if (!found) {
// Replace the page in the frame
fr[pgf % frm] = pg[i];
printf("\n");
for (j = 0; j < frm; j++) {
printf("%d\t", fr[j]);
printf("\n");
pgf++;
i++;
}
printf("\nNumber of hits: %d", hit);
return pgf;
}
OUTPUT:
6B. LRU Page Replacement Algorithm:
AIM: To simulate LRU page replacement algorithm.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int t[3],hits, pgf = 0, n, a[20], i, j, frm[10], nfm, k;
float hit_ratio;
int min[10];
int lru() {
int pos = -1, pres = 0, m, l, p;
for (i = 0, pos = 0; pos < nfm && i < n; i++) {
if (pos >= 1)
for (m = 0; m < pos; m++) {
if (a[i] == frm[m]) {
pres = 1;
break;
if (pres == 0) {
frm[pos++] = a[i];
pgf++;
}
for (k = 0; k < nfm; k++) {
printf("%d ", frm[k]);
printf("\n");
pres = 0;
for (i = 0; i < n; i++) {
for (m = 0; m < nfm; m++)
if (a[i] == frm[m]) {
pres = 1;
break;
if (pres == 0) {
for (j = 0; j < nfm; j++)
for (k = 0; k < n; k++)
if (frm[j] == a[k]) {
min[j] = k;
pos = 0;
l = min[0];
for (p = 1; p < nfm; p++)
if (min[p] < l) {
l = min[p];
pos = p;
frm[pos] = a[i];
for (k = 0; k < nfm; k++) {
printf("%d ", frm[k]);
min[k] = -1;
pgf++;
printf("\n");
pres = 0;
return pgf;
void main() {
clrscr();
printf("Enter the number of elements in the reference string: ");
scanf("%d", &n);
printf("Enter the reference string: ");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("Enter the number of frames: ");
scanf("%d", &nfm);
for (i = 0; i < nfm; i++)
min[i] = -1;
pgf = lru();
printf("Number of page faults is %d\n", pgf);
// Calculate hits and hit ratio
hits = n - pgf;
hit_ratio = (float)hits / n;
printf("Number of hits: %d\n", hits);
printf("Hit ratio: %.2f\n", hit_ratio);
getch();
OUTPUT: