0% found this document useful (0 votes)
6 views2 pages

Backlog Slip 12

The first document implements a Least Recently Used (LRU) page replacement algorithm in C, allowing the user to input the number of frames and simulating page faults with a predefined reference string. The second document creates a simple shell program that executes commands and lists directory contents based on user input, handling errors and providing usage instructions. Both programs demonstrate fundamental concepts in C programming, including memory management and process control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Backlog Slip 12

The first document implements a Least Recently Used (LRU) page replacement algorithm in C, allowing the user to input the number of frames and simulating page faults with a predefined reference string. The second document creates a simple shell program that executes commands and lists directory contents based on user input, handling errors and providing usage instructions. Both programs demonstrate fundamental concepts in C programming, including memory management and process control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1

#include <stdio.h>
#define MAX_FRAMES 10
#define MAX_REF 15

typedef struct { int page, last_used; } Frame;

void display(Frame f[], int n) { for (int i = 0; i < n; i++) printf(f[i].page != -1


? "%d " : "- ", f[i].page); printf("\n"); }
int find_LRU(Frame f[], int n) { int min = 0; for (int i = 1; i < n; i++) if
(f[i].last_used < f[min].last_used) min = i; return min; }

void LRU(int ref[], int size, int n) {


Frame f[MAX_FRAMES] = {{-1, 0}}; int faults = 0;
for (int i = 0; i < size; i++) {
int p = ref[i], found = 0;
for (int j = 0; j < n; j++) if (f[j].page == p) { f[j].last_used = i; found
= 1; break; }
if (!found) { int r = (i < n) ? i : find_LRU(f, n); f[r] = (Frame){p, i};
faults++; }
printf("%-3d | ", p); display(f, n);
}
printf("Total Page Faults: %d\n", faults);
}

int main() {
int ref[MAX_REF] = {3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6}, n;
printf("Frames: "); scanf("%d", &n);
if (n <= 0 || n > MAX_FRAMES) return printf("Invalid frames!\n"), 1;
LRU(ref, MAX_REF, n);
}

Q2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>

void execute(char *args[]) {


if (fork() == 0) { execvp(args[0], args); perror("exec failed"); exit(1); }
wait(NULL);
}

void list_command(char *args[]) {


if (!args[1] || !args[2]) {
fprintf(stderr, "Usage: list [f|n] dirname\n");
return;
}
DIR *dir = opendir(args[2]);
if (!dir) { perror("Directory error"); return; }
struct dirent *entry; int count = 0;
while ((entry = readdir(dir))) {
if (args[1][0] == 'f') printf("%s\n", entry->d_name);
count++;
}
closedir(dir);
if (args[1][0] == 'n') printf("Total entries: %d\n", count);
}

int main() {
char input[1024], *args[10];
while (printf("myshell$ "), fgets(input, sizeof(input), stdin)) {
input[strcspn(input, "\n")] = 0; int i = 0;
for (char *t = strtok(input, " "); t; t = strtok(NULL, " ")) args[i++] = t;
args[i] = NULL; if (!i || !strcmp(args[0], "exit")) break;
!strcmp(args[0], "list") ? list_command(args) : execute(args);
}
return 0;
}

You might also like