0% found this document useful (0 votes)
10 views13 pages

Os Short Codes 2-1

The document contains multiple C programs that demonstrate various memory management techniques, including first-fit and best-fit allocation strategies, page replacement algorithms like FIFO and LRU, and a simple file management system. Each program prompts the user for input regarding blocks, files, or pages and outputs the allocation results or page faults. The programs are structured to handle user interactions and display results in a formatted manner.

Uploaded by

adilamil15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views13 pages

Os Short Codes 2-1

The document contains multiple C programs that demonstrate various memory management techniques, including first-fit and best-fit allocation strategies, page replacement algorithms like FIFO and LRU, and a simple file management system. Each program prompts the user for input regarding blocks, files, or pages and outputs the allocation results or page faults. The programs are structured to handle user interactions and display results in a formatted manner.

Uploaded by

adilamil15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

6)a

#include<stdio.h>

int main() {

int frag[10], blk[10], file[10], b, f, nb, nf, temp, max = 0;

int blk_used[10] = {0}, file_blk[10];

printf("Enter Total Blocks: ");

scanf("%d", &nb);

printf("Enter Total Files: ");

scanf("%d", &nf);

printf("Enter Block Sizes:\n");

for(b = 0; b < nb; b++) {

printf("Block[%d]: ", b + 1);

scanf("%d", &blk[b]);

printf("Enter File Sizes:\n");

for(f = 0; f < nf; f++) {

printf("File[%d]: ", f + 1);

scanf("%d", &file[f]);

for(f = 0; f < nf; f++) {

for(b = 0; b < nb; b++) {

if(!blk_used[b] && (temp = blk[b] - file[f]) >= 0 && temp > max) {

file_blk[f] = b;

max = temp;

frag[f] = max;

blk_used[file_blk[f]] = 1;

max = 0;

printf("\nFile\tSize\tBlock\tBlock Size\tFragment");
for(f = 0; f < nf; f++) {

printf("\n%d\t%d\t%d\t%d\t\t%d", f + 1, file[f], file_blk[f] + 1, blk[file_blk[f]], frag[f]);

return 0;

6)b

#include<stdio.h>
int main() {
int frag[10], blk[10], file[10], b, f, nb, nf, temp, max = 0;
int blk_used[10] = {0}, file_blk[10];
printf("Enter Total Blocks: ");
scanf("%d", &nb);
printf("Enter Total Files: ");
scanf("%d", &nf);
printf("Enter Block Sizes:\n");
for(b = 0; b < nb; b++) {
printf("Block[%d]: ", b + 1);
scanf("%d", &blk[b]);
}
printf("Enter File Sizes:\n");
for(f = 0; f < nf; f++) {
printf("File[%d]: ", f + 1);
scanf("%d", &file[f]);
}
for(f = 0; f < nf; f++) {
for(b = 0; b < nb; b++) {
if(!blk_used[b] && (temp = blk[b] - file[f]) >= 0 && temp > max) {
file_blk[f] = b;
max = temp;
}
}
frag[f] = max;
blk_used[file_blk[f]] = 1;
max = 0;
}
printf("\nFile\tSize\tBlock\tBlock Size\tFragment");
for(f = 0; f < nf; f++) {
printf("\n%d\t%d\t%d\t%d\t\t%d", f + 1, file[f], file_blk[f] + 1, blk[file_blk[f]], frag[f]);
}
return 0;
}
6)c
#include<stdio.h>
void main() {
int frag[20], blk[20], proc[20], i, j, nb, np, diff, min = 9999;
int blk_used[20] = {0}, alloc[20] = {0};

printf("Best Fit Memory Management\n");


printf("Enter number of blocks: ");
scanf("%d", &nb);
printf("Enter number of processes: ");
scanf("%d", &np);

printf("Enter block sizes:\n");


for(i = 1; i <= nb; i++) {
printf("Block[%d]: ", i);
scanf("%d", &blk[i]);
}

printf("Enter process sizes:\n");


for(i = 1; i <= np; i++) {
printf("Process[%d]: ", i);
scanf("%d", &proc[i]);
}
for(i = 1; i <= np; i++) {
for(j = 1; j <= nb; j++) {
if(!blk_used[j] && (diff = blk[j] - proc[i]) >= 0 && diff < min) {
alloc[i] = j;
min = diff;
}
}
frag[i] = min;
if(alloc[i]) blk_used[alloc[i]] = 1;
min = 9999;
}

printf("\nProc\tSize\tBlock\tBlock Size\tFragment");
for(i = 1; i <= np; i++) {
if(alloc[i])
printf("\n%d\t%d\t%d\t%d\t\t%d", i, proc[i], alloc[i], blk[alloc[i]], frag[i]);
else
printf("\n%d\t%d\tNot Allocated", i, proc[i]);
}
}
7)a
#include<stdio.h>

int main() {

int i, j = 0, n, p[50], f[10], fn, k, found, faults = 0;

printf("Enter number of pages: ");

scanf("%d", &n);

printf("Enter page numbers: ");

for(i = 0; i < n; i++) scanf("%d", &p[i]);

printf("Enter number of frames: ");

scanf("%d", &fn);

for(i = 0; i < fn; i++) f[i] = -1;


printf("Ref\tFrames\n");

for(i = 0; i < n; i++) {

found = 0;

for(k = 0; k < fn; k++) if(f[k] == p[i]) found = 1;

if(!found) {

f[j] = p[i];

j = (j + 1) % fn;

faults++;

for(k = 0; k < fn; k++) printf("%d\t", f[k]);

printf("\n");

printf("Page Faults: %d", faults);

return 0;

7)b

#include<stdio.h>
int i, j, nf, nr, ref[50], frm[10], pf = 0, victim = -1, rec[50], cnt = 0;
int lruvictim(int nf, int frm[], int rec[]);

int main() {
printf("LRU Page Replacement\n");
printf("Enter number of frames: ");
scanf("%d", &nf);
printf("Enter size of reference string: ");
scanf("%d", &nr);
printf("Enter reference string: ");
for(i = 0; i < nr; i++) scanf("%d", &ref[i]);

for(i = 0; i < nf; i++) frm[i] = -1;


for(i = 0; i < nr; i++) rec[ref[i]] = 0;
printf("Ref\tFrames\n");
for(i = 0; i < nr; i++) {
int found = 0;
for(j = 0; j < nf; j++) if(frm[j] == ref[i]) found = 1;
if(!found) {
if(cnt < nf) victim = cnt++;
else victim = lruvictim(nf, frm, rec);
pf++;
frm[victim] = ref[i];
for(j = 0; j < nf; j++) printf("%d\t", frm[j]);
}
rec[ref[i]] = i;
printf("\n");
}
printf("Page Faults: %d\n", pf);
return 0;
}

int lruvictim(int nf, int frm[], int rec[]) {


int min = 9999, index = -1;
for(int i = 0; i < nf; i++) {
if(rec[frm[i]] < min) {
min = rec[frm[i]];
index = i;
}
}
return index;
}
8a
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct {
char dname[10], fname[10][10];
int fcnt;
} dir;

void main() {
int i, ch;
char f[30];
dir.fcnt = 0;
printf("Enter directory name: ");
scanf("%s", dir.dname);

while(1) {
printf("\n1.Create\t2.Delete\t3.Search\t4.Display\t5.Exit\nChoice: ");
scanf("%d", &ch);

if(ch == 1) {
printf("Enter file name: ");
scanf("%s", dir.fname[dir.fcnt++]);
} else if(ch == 2) {
printf("Enter file name: ");
scanf("%s", f);
for(i = 0; i < dir.fcnt; i++) {
if(strcmp(f, dir.fname[i]) == 0) {
strcpy(dir.fname[i], dir.fname[--dir.fcnt]);
printf("File %s deleted", f);
break;
}
}
if(i == dir.fcnt) printf("File %s not found", f);
} else if(ch == 3) {
printf("Enter file name: ");
scanf("%s", f);
for(i = 0; i < dir.fcnt; i++) {
if(strcmp(f, dir.fname[i]) == 0) {
printf("File %s found", f);
break;
}
}
if(i == dir.fcnt) printf("File %s not found", f);
} else if(ch == 4) {
if(dir.fcnt == 0) printf("Directory is empty");
else {
printf("Files: ");
for(i = 0; i < dir.fcnt; i++) printf("%s ", dir.fname[i]);
}
} else exit(0);
}
}
8b
#include <stdio.h>

struct File {
char filename[50];
int size;
};

struct Directory {
char dirname[50];
struct File files[10];
int file_count;
};
void list_files(struct Directory dir) {
printf("\nListing files in %s:\n", dir.dirname);
printf("Filename Size\n");
printf("--------------------------------------\n");
for (int i = 0; i < dir.file_count; i++) {
printf("%-30s %-10d\n", dir.files[i].filename, dir.files[i].size);
}
}

int main() {
struct Directory root = {"Root", {{"Directory1", 0}, {"Directory2", 0}, {"Directory3", 0}}, 3};
struct Directory directory1 = {"Directory1", {{"file1.txt", 1024}, {"file2.txt", 2048}, {"file3.txt", 512}},
3};
struct Directory directory2 = {"Directory2", {{"file4.txt", 1024}, {"file5.txt", 2048}}, 2};
struct Directory directory3 = {"Directory3", {{"file6.txt", 1024}, {"file7.txt", 2048}}, 2};

printf("Simulating file system:\n\n");

list_files(root);
list_files(directory1);
list_files(directory2);
list_files(directory3);

return 0;
}

9
#include<stdio.h>
#include<stdlib.h>
void main() {
int f[50] = {0}, p, i, st, len, j, c, k, a;
printf("Enter number of blocks already allocated: ");
scanf("%d", &p);
printf("Enter allocated blocks: ");
for(i = 0; i < p; i++) {
scanf("%d", &a);
f[a] = 1;
}
while(1) {
printf("Enter index starting block and length: ");
scanf("%d%d", &st, &len);
k = len;
if(f[st] == 0) {
for(j = st; j < (st + k); j++) {
if(f[j] == 0) {
f[j] = 1;
printf("%d --> %d\n", j, f[j]);
} else {
printf("%d Block is already allocated\n", j);
k++;
}
}
} else {
printf("%d starting block is already allocated\n", st);
}
printf("Do you want to enter more file (Yes - 1 / No - 0): ");
scanf("%d", &c);
if(c == 0) break;
}
exit(0);
}
10
#include <stdio.h>
int request[50];
int SIZE;
int pre;
int head;
int uptrack;
int downtrack;
struct max{
int up;
int down;
} kate[50];
int dist(int a, int b){
if (a > b)
return a - b;
return b - a;
}
void sort(int n){
int i, j;
for (i = 0; i < n - 1; i++){
for (j = 0; j < n - i - 1; j++){
if (request[j] > request[j + 1]){
int temp = request[j];
request[j] = request[j + 1];
request[j + 1] = temp;
}
}
}
j = 0;
i = 0;
while (request[i] != head){
kate[j].down = request[i];
j++;
i++;
}
downtrack = j;
i++;
j = 0;
while (i < n){
kate[j].up = request[i];
j++;
i++;
}
uptrack = j;
}
void scan(int n){
int i;
int seekcount = 0;
printf("SEEK SEQUENCE = ");
sort(n);
if (pre < head){
for (i = 0; i < uptrack; i++){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
for (i = downtrack - 1; i > 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
}
else{
for (i = downtrack - 1; i >= 0; i--){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].down);
head = kate[i].down;
}
for (i = 0; i < uptrack - 1; i++){
printf("%d ", head);
seekcount = seekcount + dist(head, kate[i].up);
head = kate[i].up;
}
}
printf(" %d\nTOTAL DISTANCE :%d", head, seekcount);
}
int main(){
int n, i;
printf("ENTER THE DISK SIZE :\n");
scanf("%d", &SIZE);
printf("ENTER THE NO OF REQUEST SEQUENCE :\n");
scanf("%d", &n);
printf("ENTER THE REQUEST SEQUENCE :\n");
for (i = 0; i < n; i++)
scanf("%d", &request[i]);
printf("ENTER THE CURRENT HEAD :\n");
scanf("%d", &head);
request[n] = head;
request[n + 1] = SIZE - 1;
request[n + 2] = 0;
printf("ENTER THE PRE REQUEST :\n");
scanf("%d", &pre);
scan(n + 3);
}

You might also like