os

Download as pdf or txt
Download as pdf or txt
You are on page 1of 107

Slip no-1

Q.1 Write the simulation program to implement demand


paging and show the page scheduling and total number of
page faults according to the LFU page replacement
algorithm. Assume the memory of n frames.
Reference String : 3,4,5,4,3,4,7,2,4,5,6,7,2,4,6
---->
#include <stdio.h>
#define MAX 100
int findLFU(int freq[], int n, int frames[], int size) {
int min = freq[frames[0]], minlndex = 0;
for (inti= 1; i < size; i++) {
if (freq[frames[i]] < min) {
min = freq[frames[i]];
minlndex = i;
}
}
return minlndex;
}
int main() {
int n, frames[MAX], freq[MAX] = {0}, pages[MAX],
page_faults = 0, size = 0;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages: ");
int page_count;
scanf("%d", &page_count);
printf("Enter the reference string: ");
for (int i = 0; i < page_count; i++) {
scanf("%d", &pages[i]);

0 } Scanned with OKEN Scanner


}
for (int i =O; i < page_count; i++) {
int page = pages[i];
freq [page]++;
int found = O;
for (int j = O; j < size; j++) {
if (framesO] == page) {
found = 1 ·
'
break;
}
}
if (!found) {
if (size< n) {
frames[size++] = page;
} else {
int lfulndex = findLFU(freq, n, frames, size);
frames[lfulndex] = page;
}
page_faults++;
}
printf("Frames: ");
for (int j = O; j < size; j++) {
printf("%d ", frames0]);
}
printf("\n");
}
printf("Total page faults: %d\n", page_faults);

return O;
}

0 } Scanned with OKEN Scanner


Q.2 Write a C program to implement the shell which displays
the command prompt "myshell$". It accepts the command,
tokenize the command line and execute it by creating the
child process. Also implement the additional command
'typeline' as
typeline +n filename :- To print first n lines in the file.
typeline -a filename :- To print all lines in the file.
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX 100
void typeline{char *option, char *filename) {
FILE *file = fopen{filename, "r");
if {file == NULL) {
perror{"Error opening file");
return;
}
char line[MAX];
if ( strcmp(option, "-a") == 0) {
while {fgets{line, MAX, file) != NULL) {
printf{"%s", line);
}
} else if ( option[0] == '+' && strlen(option) > 1) {
int n = atoi(option + 1);
for (inti= 0; i < n && fgets(line, MAX, file)!= NULL; i++) {
printf{"%s", line);

0 }Scanned with OKEN Scanner


}
}
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strlen(input) - 1] ='\0'; // remove newline character
int i = o·I

args[i] = strtok(input, " ");


while (args[i] != NULL) {
i++·
I

args[i] = strtok(NULL, " ");


}
if ( strcmp( args[O], "exit") == 0) {
break;
} else if (strcmp(args[O], "typeline") == 0 && args[1] &&
args[2]) {
type Iine(args[1 ], args[2]);
} else {
pid_t pid = fork();
if (pid == O) {
execvp(args[O], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);

0 }Scanned with OKEN Scanner


}
}
}
return 0;
}

Slip no-2
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the FIFO page replacement algorithm. Assume the
memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6,7, 2, 4, 6
--->
#include <stdio.h>
#define MAX 100
int main() {
int n, frames[MAX], pages[MAX], page_faults = 0, index =0;
int page_count;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &page_count);
printf("Enter the reference string: ");
for (int i = 0; i < page_count; i++) {
scanf("%d", &pages[i]);
}
for (int i = 0; i < n; i++) {
frames[i] = -1;
}
for (int i = 0; i < page_count; i++) {

0 } Scanned with OKEN Scanner


int page = pages[i];
int found = O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1 ·
I

break;
}
}
if (!found) {
frames[index] = page;
index = (index+ 1) o/o n;
page_faults++;
printf("Page %d loaded into frame. Current frames: ",
page);
for (intj = O;j < n;j++) {
if (frames0] != -1) {
printf("%d ", framesU]);
} else {
pri ntf("- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

0 } Scanned with OKEN Scanner


Q.2 Write a program to implement the shell. It should display
the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following 'list'
commands as
myshell$ list f dirname :- To print names of all the files in
current directory.
myshell$ list n dirname :- To print the number of all entries in
the current directory
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX 100
void list_files(char *dirname) {
struct dirent *entry;
DIR *dir = opendir(dirname);
if (dir == NULL) {
perror("Error opening directory");
return;
}
while ((entry= readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {//Only list regular files
pr i ntf(" %s\ n", entry-> d_name);
}
}

0 }Scanned with OKEN Scanner


closedir{dir);
}
void count_entries{char *dirname) {
struct dirent *entry;
DIR *dir = opendir{dirname);
int count = O;
if {dir == NULL) {
perror{"Error opening directory");
return;
}
while ((entry= readdir{dir)) != NULL) {
count++;
}
printf{"Total entries: '3/od\n", count);
closedir( dir);
}
int main() {
char input[MAX];
char *args[MAX];
while {1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strlen(input) - 1] = '\0'; // Remove newline character
int i = o· I

args[i] = strtok{input, " ");


while {args[i] != NULL) {
i++·I

args[i] = strtok(NULL, " ");


}
if (args[O] == NULL) {

0 }Scanned with OKEN Scanner


continue;
}
if (strcmp(args[0], "exit") == 0) {
break;
} else if (strcmp(args[0], "list") == 0 && args[1] != NULL
&& args[2] != NULL) {
if (strcmp(args[1 ], "f'') == 0) {
Iist_fil es(args[2]);
} else if (strcmp(args[1 ], "n") == 0) {
count_entries(args[2]);
} else {
printf("lnvalid list command option.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-3
Q.1 Write the simulation program to implement demand
paging and show the page scheduling and total number of
page faults according to the LRU (using counter method)
page replacement algorithm. Assume the memory of n
frames.
Reference String : 3,5,7,2,5,1,2,3,1,3,5,3,1,6,2
-->
#include <stdio.h>
#define MAX 100
int findLRU(int time □, int n) {
int min = time[0], minlndex = 0;
for (int i = 1; i < n; i++) {
if (time[i] < min) {
min = time[i];
minlndex = i;
}
}
return minlndex;
}
int main() {
int frames[MAX], time[MAX], pages[MAX], page_faults = 0,
n, page_count, counter= 0;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &page_count);
printf("Enter the reference string: ");
for (int i = 0; i < page_count; i++) {
scanf("%d", &pages[i]);

0 } Scanned with OKEN Scanner


}
for (int i = O; i < n; i++) {
frames[i] = -1; II Initialize frames as empty
}
for (int i = O; i < page_count; i++) {
int page = pages[il found = O;
II Check if page is already in frames
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1 ·
I

time0] = ++counter; II Update time for LRU


break;
}
}
II If page not found, page fault occurs
if (!found) {
int index;
if (i < n) {
index= i; II Fill the frame initially
} else {
index= findLRU(time, n); II Find LRU page to replace
}
frames[index] = page;
time[index] = ++counter;
page_faults++;
II Print current state of frames
printf("Page %d loaded into frame. Current frames: ",
page);
for (intj = O;j < n;j++) {
if (frames0] != -1) {

0 } Scanned with OKEN Scanner


printf{"%d ", framesU]);
} else {
printf{"- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return 0;
}

Q.2 Write a programto implement the toy shell. It should


display the command prompt "myshell$". Tokenize the
command line and execute the given command by creating
the child process. Additionally it should interpret the
following commands.
count c filename :- To print number of characters in the file.
count w filename :- To print number of words in the file.
count I filename :- To print number of lines in the file.
->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <ctype.h>
#define MAX 100
void count_chars(char *filename) {
FILE *file = fopen(filename, "r");

0 }Scanned with OKEN Scanner


if (file == NULL) {
perror("Error opening file");
return;
}
int char_count = O;
char ch;
while ((ch= fgetc(file)) != EOF) {
char_count++;
}
fclose(file);
printf("Number of characters: %d\n", char_count);
}
void count_words(char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
int word_count = O;
char ch prev = ' '·
I I

while ((ch= fgetc(file)) != EOF) {


if (isspace(ch) && !isspace(prev)) {
word_count++;
}
prev = ch;
}
if (! isspace(prev)) {
word_count++;
}
fclose(file);

0 }Scanned with OKEN Scanner


printf("Number of words: %d\n", word_count);
}
void count_lines(char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
int line_count = O;
char ch;
while ((ch= fgetc(file)) != EOF) {
if ( ch == '\n') {
line_count++;
}
}
fclose(file);
printf("Number of lines: %d\n", line_count);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strlen(input) - 1] = '\O';
int i =o·
I

args[i] = strtok(input, " ");


while (args[i] != NULL) {
i++·I
args[i] = strtok(NULL, " ");

0 }Scanned with OKEN Scanner


}
if (args[0] == NULL) {
continue;
}
if (strcmp( args[0], "exit") == 0) {
break;
} else if (strcmp(args[0], "count")== 0 && args[1] != NULL
&& args[2] != NULL) {
if (strcmp(args[1 ], "c") == 0) {
count_chars( args[2]);
} else if (strcmp(args[1], "w") == 0) {
count_words( args[2]);
} else if (strcmp(args[1], "I")== 0) {
count_li nes(args[2]);
} else {
printf("lnvalid count option.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp( args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-4
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the MFU page replacement algorithm. Assume the
memory of n frames.
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
---->
#include <stdio.h>
#define MAX 100
int findMFU(int freq[], int n) {
int max = freq[0], maxlndex = 0;
for (int i = 1; i < n; i++) {
if (freq[i] > max) {
max = freq[i];
maxlndex = i;
}
}
return maxi ndex;
}
int main() {
int frames[MAX], pages[MAX], freq[MAX] = {0}, page_faults
= 0, n, page_count;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &page_count);
printf("Enter the reference string: ");
for (int i = 0; i < page_count; i++) {
scanf("%d", &pages[i]);
}

0 } Scanned with OKEN Scanner


for (int i = O; i < n; i++) {
frames[i] = -1;
}
for (int i = O; i < page_count; i++) {
int page = pages[i], found = O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1 ·
I

freqU]++;
break;
}
}
if (!found) {
int index;
if (i < n) {
index = i·
I

} else {
index= findMFU(freq, n);
}
frames[index] = page;
freq[index] = 1;
page_faults++;
printf("Page %d loaded into frame. Current frames: ",
page);
for (intj = O;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}

0 } Scanned with OKEN Scanner


}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return 0;
}

Q.2 Write a program to implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following
commands.
myshell$ search a filename pattern :- To search all the
occurrence of pattern in the file.
myshell$ search c filename pattern :- To count the number of
occurrence of pattern in the file.
---->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 100
void search_all(char *filename, char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}

0 }Scanned with OKEN Scanner


char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file)!= NULL) {
if (strstr(line, pattern)!= NULL) {
printf("Pattern found at line %d: %s", line_number, line);
}
line_number++;
}
fclose(file);
}
void search_count(char *filename, char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int count = O;
while (fgets(line, sizeof(line), file) != NULL) {
char *ptr = line;
while ((ptr = strstr(ptr, pattern))!= NULL) {
count++;
ptr += strlen(pattern);
}
}
printf("Pattern '%s' found %d times\n", pattern, count);
fclose(file);
}
int main() {
char input[MAX];

0 }Scanned with OKEN Scanner


char *args[MAX];
while (1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strlen(input) - 1] ='\0';
int i =O;
args[i] = strtok(in put, " ");
while (args[i] != NULL) {
i++·
I

args[i] =strtok(NULL, " ");


}
if (args[O] == NULL) {
continue;
}
if (strcmp( args[O], "exit") == 0) {
break;
} else if (strcmp(args[O], "search") == O && args[1] !=
NULL && args[2] != NULL && args[3] != NULL) {
if (strcmp(args[1 ], "a") == 0) {
search_all(args[2], args[3]);
} else if (strcmp(args[1 ], "c") == 0) {
search_count( args[2], args[3]);
} else {
printf("lnvalid search option.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp(args[O], args);
perror("Error executing command");

0 }Scanned with OKEN Scanner


exit{1 );
} else {
wait{NULL);
}
}
}
return O;
}

Slip no-5
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the optimal page replacement algorithm. Assume
the memory of n frames.
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
--->
#include <stdio.h>
#define MAX 100
int findOptimal{int frames □, int ref□, int n, int currentlndex, int
ref_len) {
int farthest = currentlndex, index = -1;
for (int i = O; i < n; i++) {
int j;
for U= cu rrentl ndex + 1; j < ref_I en; j++) {
if {frames[i] == refU]) {
if U> farthest) {
farthest = j;
index = i•
'
}
break;

0 }Scanned with OKEN Scanner


}
}
if U== ref_len) {
return i;
}
}
return (index == -1) ? 0 : index;
}
int main() {
int frames[MAX], ref[MAX], page_faults = 0, n, ref_len;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = O; i < ref_len; i++) {
scanf("%d", &ref[i]);
}
for (int i = O; i < n; i++) {
frames[i] = -1;
}
for (int i = O; i < ref_len; i++) {
int page =ref[i], found =O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1,·
break;
}
}
if (!found) {

0 } Scanned with OKEN Scanner


if (i < n) {
frames[i] = page;
} else {
int index = findOptimal(frames, ref, n, i, ref_len);
frames[index] = page;
}
page_faults++;
printf("Page %d loaded. Current frames: ", page);
for (intj = 0;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return 0;
}

Q.2 Write a program to implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following
commands.
myshe11$ search f filename pattern :- To display first
occurrence of pattern in the file.
myshell$ search c filename pattern :- To count the number of

0 }Scanned with OKEN Scanner


occurrence of pattern in the file.
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 100
void search_first( char *filename, char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file)!= NULL) {
if (strstr(line, pattern)!= NULL) {
printf("First occurrence of '%s' found at line %d: %s",
pattern, line_number, line);
fclose(file);
return;
}
line_number++;
}
printf("Pattern '%s' not found in the file.\n", pattern);
fclose(file);
}
void search_count(char *filename, char *pattern) {
FILE *file = fopen(filename, "r");

0 } Scanned with OKEN Scanner


if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int count = O;
while (fgets(line, sizeof(line), file)!= NULL) {
char *ptr = line;
while ((ptr = strstr(ptr, pattern))!= NULL) {
count++;
ptr += strlen(pattern);
}
}
printf("Pattern '%s' found %d times\n", pattern, count);
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strlen(input) - 1] = '\0'; // Remove newline character
int i = o·
I

args[i] = strtok(input, " ");


while (args[i] != NULL) {
i++·
I

args[i] = strtok(NULL, " ");


}
if (args[O] == NULL) {

0 }Scanned with OKEN Scanner


continue; // Empty command
}
if (strcmp(args[0], "exit") == 0) {
break; // Exit the shell
} else if (strcmp(args[0], "search") == 0 && args[1] !=
NULL && args[2] != NULL && args[3] != NULL) {
if (strcmp(args[1 ], "f'') == 0) {
search_first( args[2], args[3]);
} else if (strcmp(args[1 ], "c") == 0) {
search_cou nt( args[2], args[3]);
} else {
printf("lnvalid search option.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-6
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the MRU page replacement algorithm. Assume the
memory of n frames.
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
--->
#include <stdio.h>
#define MAX 100
int findMRU(int frames[], int ref□, int n, int currentlndex, int
ref_len) {
int mrulndex = 0, maxTime = -1;
for (int i = O; i < n; i++) {
int j;
for U= currentlndex - 1; j >= O; j--) {
if (frames[i] == refU]) {
if U> maxTime) {
maxTime = j;
mrulndex = i;
}
break;
}
}
}
return mrulndex;
}
int main() {
int frames[MAX], ref[MAX], page_faults = 0, n, ref_len;
printf("Enter the number of frames: ");
scanf("%d", &n);

0 }Scanned with OKEN Scanner


printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = O; i < ref_len; i++) {
scanf("%d", &ref[i]);
}

for(int i = O; i < n; i++) {


frames[i] = -1;
}
for (int i = O; i < ref_len; i++) {
int page = ref[i], found = O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1· I

break;
}
}
if (!found) {
if (i < n) {
frames[i] = page;
} else {
int index = findMRU(frames, ref, n, i, ref_len);
frames[index] = page;
}
page_faults++;
printf("Page %d loaded. Current frames: ", page);
for (intj = O;j < n;j++) {
if (frames0] != -1) {
printf("%d ", framesU]);

0 }Scanned with OKEN Scanner


} else {
printf{"- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

Q.2 Write a programto implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following
commands.
myshell$ search f filename pattern :- To display first
occurrence of
pattern in the file.
myshell$ search a filename pattern :- To search all the
occurrence of
pattern in the file.
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024
void search_first( con st char *filename, canst char *pattern) {

0 }Scanned with OKEN Scanner


FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file) != NULL) {
if (strstr(line, pattern)!= NULL) {
printf("First occurrence of '%s' found at line %d: %s",
pattern, line_number, line);
fclose(file);
return;
}
line_number++;
}
printf("Pattern '%s' not found in the file.\n", pattern);
fclose(file);
}
void search_all(const char *filename, canst char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
int found = O;
while (fgets(line, sizeof(line), file)!= NULL) {
if (strstr(line, pattern)!= NULL) {

0 }Scanned with OKEN Scanner


printf("Occurrence of '%s' found at line %d: o/os",
pattern, line_number, line);
found =1 · I

}
line_number++;
}
if (!found) {
printf("Pattern '%s' not found in the file.\n", pattern);
}
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshel1$ ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = 0; // Remove newline
character
int i = o· I

args[i] = strtok(input, " ");


while (args[i] != NULL) {
i++·
I

args[i] = strtok(NULL, " ");


}
if (args[0] == NULL) {
continue; // Empty command
}
if (strcmp( args[0], "exit") ==0) {
break; // Exit the shell

0 }Scanned with OKEN Scanner


} else if (strcmp(args[0], "search") == 0 && args[1] !=
NULL && args[2] != NULL) {
if (strcmp(args[1 ], "f'') == 0) {
search_first( args[2], args[3]);
} else if (strcmp(args[1 ], "a") == 0) {
search_all(args[2], args[3]);
} else {
printf("lnvalid search option. Use 'f' for first
occurrence or 'a' for all occurrences.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-7
Q.1 Write the simulation program for demand paging and
show the pagescheduling and total number of page faults
according the Optimal page replacement algorithm. Assume
the memory of n frames.
Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6, 2
--->
#include <stdio.h>
#define MAX 100
int findOptimal(int frames □, int n, int ref□, int currentlndex, int
ref_len) {
int optimallndex = -1, farthest= currentlndex;
for (int i = O; i < n; i++) {
int j;
for U= current Index; j < ref_len; j++) {
if (frames[i] == refU]) {
if U> farthest) {
farthest = j;
optimallndex = i;
}
break;
}
}
if U== ref_len) {
return i; // This frame is not going to be used again
}
}
return (optimal Index!= -1)? optimal Index: O; // Return the
index to replace
}

0 }Scanned with OKEN Scanner


int main() {
int frames[MAX], ref[MAX], page_faults = 0, n, ref_len;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = O; i < ref_len; i++) {
scanf("%d", &ref[i]);
}
for(int i = O; i < n; i++) {
frames[i] = -1; // Initialize frames to -1 ( empty)
}
for (int i = O; i < ref_len; i++) {
int page = ref[i], found = O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1; // Page hit
break;
}
}
if (!found) {
if (i < n) {
frames[i] =page;// Fill empty frames
} else {
int index = findOptimal(frames, n, ref, i + 1, ref_len);
frames[index] = page;// Replace the page
}
page_faults++;
printf("Page %d loaded. Current frames: ", page);

0 }Scanned with OKEN Scanner


for (intj = O;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

Q.2 Write a program to implement shell. It should display the


command prompt "myshell$". Tokenize the command line
and execute the given command by creating the child
process. Additionally it should interpret the following
commands.
myshell$ search a filename pattern :- To search all the
occurrence of pattern in the file.
myshell$ search c filename pattern :- To count the number of
occurrence of pattern in the file.
---->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024

0 }Scanned with OKEN Scanner


void search_all(const char *filename, canst char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
int found = O;
while (fgets(line, sizeof(line), file)!= NULL) {
if (strstr(line, pattern)!= NULL) {
printf("Occurrence of '%s' found at line %d: %s",
pattern, line_number, line);
found = 1·
I

}
line_number++;
}
if (!found) {
printf("Pattern '%s' not found in the file.\n", pattern);
}
fclose(file);
}
void count_occurrences( canst char *filename, canst char
*pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];

0 }Scanned with OKEN Scanner


int count = O;
while (fgets(line, sizeof(line), file)!= NULL) {
char *ptr = line;
while ((ptr = strstr(ptr, pattern))!= NULL) {
count++;
ptr++;
}
}
printf("Pattern '%s' found %d times in the file.\n", pattern,
count);
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshel1$ ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = O; // Remove newline
character
int i = o·
I

a rgs[i] = strtok(in put, " ");


while (args[i] != NULL) {
i++· I

args[i] = strtok(NULL, " ");


}
if (args[O] == NULL) {
continue;// Empty command
}
if ( strcmp( args[O], "exit") == 0) {

0 }Scanned with OKEN Scanner


break; // Exit the shell
} else if (strcmp(args[0], "search") == 0 && args[1] !=
NULL && args[2] != NULL) {
if (strcmp(args[1 ], "a") == 0) {
search_all(args[2], args[3]);
} else if (strcmp(args[1 ], "c") == 0) {
count_occurrences( args[2], args[3]);
} else {
printf("lnvalid search option. Use 'a' for all
occurrences or 'c' for count.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp( args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-8
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the LRU page replacement algorithm. Assume the
memory of n frames. Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7,
3,5,9,4,6,2
--->
#include <stdio.h>
#define MAX 100
int findLRU(int frames □, int n, int time □, int currentlndex) {
int lrulndex =0, minTime =time[0];
for (int i = 1; i < n; i++) {
if (time[i] < minTime) {
minTime = time[i];
lrulndex = i;
}
}
return lrulndex;
}
int main() {
int frames[MAX], ref[MAX], time[MAX];
int page_faults = 0, n, ref_len;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
scanf("%d", &ref[i]);
}

0 } Scanned with OKEN Scanner


for (int i = O; i < n; i++) {
frames[i] = -1; // Initialize frames to -1 ( empty)
time[i] = O; // Initialize time for LRU tracking
}
for (int i = O; i < ref_len; i++) {
int page = ref[i], found = O;
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1; // Page hit
time0] = i; // Update the time of the page
break;
}
}
if (!found) {
int lrulndex;
for (intj = O;j < n;j++) {
if (frames0] == -1) {
lrulndex = j; // Empty frame found
break;
}
ifU==n-1){
lrulndex = findLRU(frames, n, time, i);
}
}
frames[lrulndex] = page;// Replace the page
time[lrulndex] = i; // Update the time for the
replaced page
page_faults++;
printf("Page %d loaded. Current frames: ", page);
for (intj = O;j < n;j++) {

0 } Scanned with OKEN Scanner


if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

Q.2 Write a programto implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following
commands.
myshe11$ search f filename pattern :- To display first
occurrence of pattern in the file.
myshell$ search c filename pattern :- To count the number of
occurrence of pattern in the file.
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024
void search_first( con st char *filename, canst char *pattern) {

0 }Scanned with OKEN Scanner


FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file) != NULL) {
if (strstr(line, pattern)!= NULL) {
printf("First occurrence of '%s' found at line %d: %s",
pattern, line_number, line);
fclose(file);
return;
}
line_number++;
}
printf("Pattern '%s' not found in the file.\n", pattern);
fclose(file);
}
void count_occurrences( canst char *filename, canst char
*pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int count = O;
while (fgets(line, sizeof(line), file)!= NULL) {
char *ptr = line;

0 }Scanned with OKEN Scanner


while ((ptr = strstr(ptr, pattern))!= NULL) {
count++;
ptr++;
}
}
printf{"Pattern '%s' found %d times in the file.\n", pattern,
count);
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while {1) {
printf{"myshell$ ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = 0;
int i = o·
I

args[i] = strtok{input, " ");


while {args[i] != NULL) {
i++· I

args[i] = strtok{NULL, " ");


}
if {args[0] == NULL) {
continue;
}
if ( strcmp( args[0], "exit") == 0) {
break;
11
} else if (strcmp(args[0], search") == 0 && args[1] !=
NULL && args[2] != NULL) {
if (strcmp(args[1 ], "f") == 0) {

0 }Scanned with OKEN Scanner


search_first( args[2], args[3]);
} else if (strcmp(args[1 ], "c") == 0) {
count_occurrences( args[2], args[3]);
} else {
printf("lnvalid search option. Use 'f' for first
occurrence or 'c' for count.\n");
}
} else {
pid_t pid =fork();
if (pid == 0) {
execvp(args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-9
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the FIFO page replacement algorithm. Assume the
memory of n frames.
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
--->
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main() {
int frames[MAX], ref[MAX], n, ref_len;
int page_faults =0, front =0, rear =0;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
scanf("%d", &ref[i]);
}
for (int i = 0; i < n; i++) {
frames[i] = -1;
}
for (int i = 0; i < ref_len; i++) {
int page = ref[i];
int found = 0;
for (int j = 0; j < n; j++) {
if (framesO] == page) {
found = 1,·

0 } Scanned with OKEN Scanner


break;
}
}
if (!found) {
frames[rear] = page;
rear= (rear+ 1) % n;
if (front == rear) {
front = (front+ 1) % n;
}
page_faults++;
printf("Page %d loaded. Current frames: ", page);
for (intj = O;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");
}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

Q.2 Write a program to implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following
commands.

0 }Scanned with OKEN Scanner


myshell$ search f filename pattern :- To display first
occurrence of pattern in the file.
myshell$ search a filename pattern :- To search all the
occurrence of pattern in the file.
--->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024
void search_first( con st char *filename, canst char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file)!= NULL) {
if (strstr(line, pattern)!= NULL) {
printf("First occurrence of '%s' found at line %d: %s",
pattern, line_number, line);
fclose(file);
return;
}
line_number++;
}

printf("Pattern '%s' not found in the file.\n", pattern);

0 } Scanned with OKEN Scanner


fclose(file);
}
void search_all(const char *filename, const char *pattern) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Error opening file");
return;
}
char line[MAX];
int line_number = 1;
int found = O;
while (fgets(line, sizeof(line), file) != NULL) {
if (strstr(line, pattern)!= NULL) {
printf("Occurrence of '%s' found at line %d: '3/os",
pattern, line_number, line);
found = 1 ·I

}
line_number++;
}
if (!found) {
printf("Pattern '%s' not found in the file.\n", pattern);
}
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (7) {
printf("myshell$ ");
fgets(input, MAX, stdin);

0 }Scanned with OKEN Scanner


input[strcspn(input, "\n")] = 0;
int i = 0;
args[i] = strtok(input, " ");
while (args[i] != NULL) {
i++·,
args[i] = strtok(NULL, " ");
}
if (args[0] == NULL) {
continue;
}
if (strcmp(args[0], "exit") == 0) {
break;
} else if (strcmp(args[0], "search") == 0 && args[1] !=
NULL && args[2] != NULL) {
if (strcmp(args[1 ], "f") == 0) {
search_first( args[2], args[3]);
} else if (strcmp(args[1], "a")== 0) {
search_all(args[2], args[3]);
} else {
printf("lnvalid search option. Use 'f for first
occurrence or 'a' for all occurrences.\n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp(args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);

0 }Scanned with OKEN Scanner


}
}
}
return 0;
}

Slip no-10
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the FIFO page replacement algorithm. Assume the
memory of n frames.
Reference String : 2, 4, 5, 6, 9, 4, 7, 3, 4, 5, 6, 7, 2, 4, 7, 1
--->
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main() {
int frames[MAX], ref[MAX], n, ref_len;
int page_faults =0, front =0, rear =0;
printf("Enter the number of frames: ");
scanf("%d", &n);
printf("Enter the number of pages in the reference string: ");
scanf("%d", &ref_len);
printf("Enter the reference string: ");
for (int i = 0; i < ref_len; i++) {
scanf("%d", &ref[i]);
}
for (int i = 0; i < n; i++) {
frames[i] = -1; // Initialize frames to -1 (empty)
}

0 } Scanned with OKEN Scanner


for (int i = O; i < ref_len; i++) {
int page = ref[i];
int found = O;
// Check if the page is already in the frames
for (int j = O; j < n; j++) {
if (frames0] == page) {
found = 1 · I

break;
}
}
// Page fault occurred
if (!found) {
frames[rear] = page; // Add the new page to the
frames
rear= (rear+ 1) % n; // Move to the next frame
if (front == rear) {
front = (front+ 1) % n; // Adjust front if frames are
full
}
page_faults++;
// Print current state of frames
printf("Page %d loaded. Current frames: ", page);
for (intj = O;j < n;j++) {
if (frames0] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");

0 } Scanned with OKEN Scanner


}
}
printf("Total page faults: %d\n", page_faults);
return O;
}

Q.2 Write a program to implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following 'list'
commands as
myshell$ list f dirname :- To print names of all the files in
current directory.
myshell$ list i dirname :- To print names and inodes of the
files in the current directory.
---->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX 1024
void list_files(const char *dirname, int show_inodes) {
struct dirent *entry;
struct stat file_stat;
char filepath[MAX];
DIR *dir =opendir(dirname);
if (dir == NULL) {
perror("Error opening directory");

0 }Scanned with OKEN Scanner


return;
}
while ((entry= readdir(dir)) != NULL) {
if (entry->d_name[O] != '.') {//Skip hidden files
snprintf(filepath, sizeof(filepath), "%s/%s", dirname,
entry->d_name);
if (show_inodes) {
stat(filepath, &file_stat);
printf("lnode: %1u - Name: %s\n", file_stat.st_ino,
entry->d_name);
} else {
printf("%s\n", entry->d_name);
}
}
}
closedir(dir);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
fgets(input, MAX, stdin);
input[strcspn(input, "\n")] = O;
int i = o·
I

args[i] = strtok(input, " ");


while (args[i] != NULL) {
i++·
I

args[i] = strtok(NULL, " ");


}

0 }Scanned with OKEN Scanner


if (args[0] == NULL) {
continue;
}
if (strcmp( args[0], "exit") == 0) {
break;
} else if (strcmp(args[0], "list") == 0 && args[1] != NULL
&& args[2] != NULL) {
if (strcmp(args[1 ], "f'') == 0) {
list_files(args[2], 0); // List files
} else if (strcmp(args[1], 'T') == 0) {
list_files(args[2], 1); // List files with inodes
} else {
printf("lnvalid list option. Use 'f' for filenames or 'i'
for i nodes. \n");
}
} else {
pid_t pid = fork();
if (pid == 0) {
execvp( args[0], args);
perror("Error executing command");
exit(1 );
} else {
wait(NULL);
}
}
}
return 0;
}

0 }Scanned with OKEN Scanner


Slip no-11
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the LFU page replacement algorithm. Assume the
memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
->
#include <stdio.h>
#define MAX FRAMES 10
#define MAX REFERENCES 100
// Function to find the least frequently used page
int findLFU(int frames[], int freq □, int n) {
int minFreq =freq[0], minlndex =0;
for (int i = 1; i < n; i++) {
if (freq[i] < minFreq) {
minFreq = freq[i];
minlndex = i;
}
}
return min Index;
}
int main() {
int n, referenceString[MAX_REFERENCES],
frames[MAX_FRAM ES], freq[MAX_FRAM ES];
int pageFaults = 0, found, index;
printf("Enter number of frames: ");
scanf("%d", &n);
printf("Enter reference string (-1 to stop): ");
int i = o·
I

while (1) {

0 }Scanned with OKEN Scanner


int page;
sea nf("%d ", &page);
if (page == -1) break;
referenceString[i++] = page;
}
int refCount = i;
II Initialize frames and frequency arrays
for (i = O; i < n; i++) {
frames[i] = -1;
freq[i] = O;
}
II Simulate LFU page replacement
for (i = O; i < refCount; i++) {
int page = referenceString[i];
found= o·,
II Check if the page is already in a frame
for (int j = O; j < n; j++) {
if (framesO] == page) {
found = 1,·
freqLJ]++; II Increment frequency
break;
}
}
if (!found) {
pageFaults++;
II Find a free frame or replace LFU page
if (frames[n -1] == -1) {
for (int j = O; j < n; j++) {
if (framesO] == -1) {
f ramesU] = page;

0 } Scanned with OKEN Scanner


frequ] = 1;
break;
}
}
} else {
index = findLFU(frames, freq, n); // Find the least
frequently used page
frames[index] = page; // Replace it with the
new page
freq[index] = 1;
}
}
// Display current frames status
printf("\nFrames: ");
for (int j = O; j < n; j++) {
if (framesu] != -1)
printf("%d ", framesu]);
else
pri ntf("- ");
}
}
printf("\n\nTotal page faults: %d\n", pageFaults);
return O;
}

Q.2 Write a C program to implement the shell. It should


display the command prompt "myshell$". Tokenize the
command line and execute the given command by creating
the child process. Additionally it should interpret the
following 'list' commands as

0 }Scanned with OKEN Scanner


myshell$ list f dirname :- To print names of all the files in
current directory.
myshell$ list n dirname :- To print the number of all entries in
the current directory
->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX INPUT 100
#define MAX ARGS 10
void list_files(canst char *dirname) {
struct dirent *entry;
DIR *dp = opendir(dirname);
if (dp == NULL) {
perror("opend ir");
return;
}
while ((entry= readdir(dp)) != NULL) {
if ( entry->d_name[0] != '.') {//Skip hidden files
printf("%s\n", entry->d_name);
}
}
closedir( dp);
}
void list_count( canst char *dirname) {
struct dirent *entry;
DIR *dp = opendir(dirname);

0 }Scanned with OKEN Scanner


int count = O;
if (dp == NULL) {
perror("opend ir");
return;
}
while ((entry= readdir(dp)) != NULL) {
if ( entry->d_name[O] != '.') {//Skip hidden files
count++;
}
}
closedir( dp);
printf("Total entries: %d\n", count);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
printf("myshel1$ ");
fgets(input, sizeof(input), stdin); // Read input
// Remove newline character at the end
input[strcspn(input, "\n")] = O;
// Tokenize the input
int i =o·
I

token = strtok(input, " ");


while (token!= NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;// Null terminate the argument list

0 }Scanned with OKEN Scanner


// Exit command
if (args[0] != NULL && strcmp(args[0], "exit") == 0) {
break;
}
// Handle "list" command
if (args[0] != NULL && strcmp(args[0], "list")== 0) {
if (args[1] != NULL && args[2] != NULL) {
if (strcmp(args[1], "f') == 0) {
list_fi les( args[2]);
} else if (strcmp(args[1], "n") == 0) {
list_count( args[2]);
} else {
printf("lnvalid option for list command\n");
}
} else {
printf("Usage: list <fin> <dirname>\n");
}
continue;
}
// Fork and execute other commands
pid_t pid = fork();
if (pid == 0) { // Child process
if (execvp(args[0], args) == -1) {
perror("execvp");
}
exit(EXIT_FAILURE);
} else if (pid < 0) { // Fork failed
perror("fork");
} else { // Parent process
wait(NULL); // Wait for child to finish

0 }Scanned with OKEN Scanner


}
}
return 0;
}

Slip no-12
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the LRU page replacement algorithm. Assume the
memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
->
#include <stdio.h>
#define MAX FRAMES 10
#define MAX REFERENCES 20
// Function to find the least recently used page
int findLRU(int time □, int n) {
int i, min =time[0], pos = 0;
for (i = 1; i < n; i++) {
if (time[i] < min) {
min = time[i];
pos = 1;
}
}
return pos;
}
int main() {
int n, frames[MAX_FRAM ES],
referenceString[MAX_REFERENCES], time[MAX_FRAMES];
int pageFaults =0, counter =0;

0 }Scanned with OKEN Scanner


int i, j, pos, flag1, flag2;
II Define the number of frames
printf("Enter the number of frames: ");
scanf("%d", &n);
II Initialize frames
for (i = O; i < n; i++) {
frames[i] = -1;
}
II Define the reference string
int referenceStringlength = 15;
int referenceStringlnput[] = {3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2,
4, 6};
printf("Reference String: ");
for (i = O; i < referenceStringlength; i++) {
referenceStri ng [i] = referenceString Input[i];
printf("%d ", referenceString[i]);
}
printf("\n");
II Simulate LRU page replacement
for (i = O; i < referenceStringlength; i++) {
flag1 =flag2 =O;
II Check if the page is already in a frame
for U= O; j < n; j++) {
if (framesO] == referenceString[i]) {
counter++;
timeO] = counter; II Update time for LRU
flag1 =flag2 = 1;
break;
}
}

0 } Scanned with OKEN Scanner


// If page is not in a frame (page fault occurs)
if (flag1 == 0) {
for U=O; j < n; j++) {
if (framesU] == -1) { // Empty frame available
counter++;
pageFaults++;
framesU] = referenceString[i];
timeU] = counter;
flag2 = 1;
break;
}
}
}
// Replace the least recently used page
if (flag2 == 0) {
pas = findLRU(time, n);
counter++;
pageFau Its++;
frames[pos] = referenceString[i];
time[pos] = counter;
}
// Display current frames status
printf("\nFrames: ");
for U= O; j < n; j++) {
if (frames0] != -1)
printf("%d ", framesU]);
else
pri ntf("- ");
}
}

0 } Scanned with OKEN Scanner


printf("\n\nTotal number of page faults: %d\n", pageFaults);
return 0;
}

Q.2 Write a program to implement the shell. It should display


the command prompt "myshell$". Tokenize the command
line and execute the given command by creating the child
process. Additionally it should interpret the following 'list'
commands as
myshell$ list f dirname :- To print names of all the files in
current directory.
myshell$ list n dirname :- To print the number of all entries in
the current directory
->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX INPUT 100
#define MAX ARGS 10
void list_files(const char *dirname) {
struct dirent *entry;
DIR *dp = opendir(dirname);
if (dp == NULL) return;
while ((entry= readdir(dp)) != NULL) {
if ( entry->d_name[0] != '.') {
pri ntf("%s\n", entry->d_name);
}

0 }Scanned with OKEN Scanner


}
closedir( dp);
}
void list_count(const char *dirname) {
struct dirent *entry;
DIR *dp = opendir{dirname);
int count = O;
if ( dp == NULL) return;
while ((entry= readdir{dp)) != NULL) {
if ( entry->d_name[O] != '.') {
count++;
}
}
closedir( dp);
printf{"%d\n", count);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while {1) {
printf{"myshell$ ");
fgets(input, sizeof{input), stdin);
input[strcspn(input, "\n")] = O;
int i = o·
I

token = strtok{input, " ");


while {token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok{NULL, " ");
}

0 }Scanned with OKEN Scanner


args[i] =NULL;
if (args[0] == NULL) continue;
if (strcmp( args[0], "exit") == 0) break;
if (strcmp(args[0], "list") == 0 && args[1] && args[2]) {
if (strcmp(args[1 ], "f") == 0) {
Iist_fil es(args[2]);
} else if (strcmp(args[1 ], "n") == 0) {
list_count(args[2]);
}
continue;
}
pid_t pid = fork();
if (pid == 0) {
if (execvp( args[0], args) = = -1) perror("execvp");
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
}
}
return 0;
}

Slip no-13
Q.1 Write a C program to implement the shell which displays
the command prompt "myshell$". It accepts the command,
tokenize the command line and execute it by creating the
child process. Also implement the additional command
'typeline' as
typeline -a filename :- To print all lines in the file.
----->

0 }Scanned with OKEN Scanner


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslwait.h>
#include <fcntl.h>
#define MAX INPUT 100
#define MAX ARGS 10
II Function to print all lines from the given file
void typeline_all{const char *filename) {
FILE *file = fopen{filename, "r");
char line[256];
if {file == NULL) {
perror{"Error opening file");
return;
}
II Read and print each line from the file
while {fgets{line, sizeof{line), file)) {
printf{"%s", line);
}
fclose{file);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while {1) {
// Display the prompt
printf("myshell$ ");
II Get input from the user

0 }Scanned with OKEN Scanner


fgets(input, sizeof(input), stdin);
II Remove newline character from the input
input[strcspn(input, "\n")] = 0;
II Tokenize the input
int i = o·
I

token = strtok(input, " ");


while (token != NULL && i < MAX_ARGS - 1) {
args[i++] =token;
token = strtok(NULL, " ");
}
args[i] = NULL;
II If no input is given, continue
if (args[0] == NULL) continue;
II Exit command
if ( strcmp( args[0], "exit") == 0) {
break;
}
II Custom 'typeline' command implementation
if (strcmp(args[0], "typeline") == 0) {
if (args[1] != NULL && strcmp(args[1 ], "-a") == 0 &&
args[2] != NULL) {
II Call function to print all lines in the file
type Ii ne_all(args[2]);
} else {
pri ntf("Usage: type Ii ne -a filename \n");
}
continue;
}
II Fork a child process to execute other commands
pid_t pid = fork();

0 }Scanned with OKEN Scanner


if (pid == 0) { // Child process
if (execvp(args[0], args) == -1) {
perror("Error executing command");
}
exit(EXIT_FAILURE);
} else if (pid > 0) { // Parent process
wait(NULL); // Wait for the child process to complete
} else {
perror("Fork failed");
}
}
return 0;
}

Q.2 Write the simulation program for Round Robin


scheduling for given time quantum. The arrival time and first
CPU-burst of different jobs should be input to the system.
Accept no. of Processes, arrival time and burst time. The
output should give the Gantt chart, turnaround time and
waiting time for each process. Also display the average
turnaround time and average waiting time.
---->
#include <stdio.h>
int main() {
int n, i, time = 0, remain, flag = 0, time_quantum;
int waiting_time =0, turnaround_time =0;
printf("Enter the number of processes: ");
scanf("%d", &n);
int at[n], bt[n], rt[n]; // Arrival time, burst time, remaining
time

0 } Scanned with OKEN Scanner


for (i = O; i < n; i++) {
printf{"Enter arrival time and burst time for process P%d:
II
I
i+1)• I

scanf{"%d %d", &at[i], &bt[i]);


rt[i] = bt[i];
}
printf{"Enter the time quantum: ");
scanf{"%d", &time_quantum);
remain = n; // Number of processes remaining
printf("\nGantt Chart:\n");
while (remain != 0) {
for (i = O; i < n; i++) {
if (rt[i] > O && at[i] <= time) {
if (rt[i] <= time_quantum) {
time += rt[i];
printf{"I P%d {%d) ", i+1, time);
rt[i] = O;
remain--;
waiting_time += time - at[i] - bt[i];
turnaround_time += time - at[i];
} else {
rt[i] -= time_quantum;
time+= time_quantum;
printf{"I P%d {%d) ", i+1, time);
}
}
}
}
pri ntf(" l\n");
printf("\n Process \tTurn around Time\tWa iti ng Ti me \n");

0 }Scanned with OKEN Scanner


for (i = 0; i < n; i++) {
int turn_time = (time - at[i]);
int wait_time = (turn_time - bt[i]);
printf("P%d\t%d\t\t%d\n", i+ 1, turn_time, wait_time);
}
printf("\nAverage Turnaround Time: %.2f",
(float)turnaround_time / n);
printf("\nAverage Waiting Time: %.2f", (float)waiting_time /
n);
return 0;
}

Slip no-14
Q.1 Write a C program to implement the shell which displays
the command prompt "myshell$". It accepts the command,
tokenize the command line and execute it by creating the
child process. Also implement the additional command
'typeline' as
typeline +n filename :- To print first n lines in the file.
->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX INPUT 100
#define MAX ARGS 10
// Function to print first 'n' lines from the file
void typeline_n(const char *filename, int n) {
FILE *file = fopen(filename, "r");

0 }Scanned with OKEN Scanner


char line[256];
int count =O;
if (file == NULL) {
perror("Error opening file");
return;
}
II Read and print the first 'n' lines from the file
while (fgets(line, sizeof(line), file) && count< n) {
printf("%s", line);
count++;
}
fclose(fi le);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
II Display the prompt
printf("myshel1$ ");
II Get input from the user
fgets(input, sizeof(input), stdin);
II Remove newline character from the input
input[strcspn(input, "\n")] = O;
II Tokenize the input
int i = o·
I

token = strtok(input, " ");


while (token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");

0 }Scanned with OKEN Scanner


}
args[i] =NULL;
II If no input is given, continue
if (args[0] == NULL) continue;
II Exit command
if (strcmp( args[0], "exit") == 0) {
break;
}
II Custom 'typeline' command implementation
if (strcmp(args[0], "typeline") == 0) {
if (args[1] != NULL && args[1 ][0] == '+' && args[2] !=
NULL) {
int n = atoi(&args[1 ][1 ]); II Extract number from +n
typeline_n(args[2], n); II Call function to print first
'n' lines
} else {
pri ntf("Usage: type Ii ne +n filename \n");
}
continue;
}
II Fork a child process to execute other commands
pid_t pid = fork();
if (pid == 0) { II Child process
if (execvp( args[0], args) ==-1) {
perror("Error executing command");
}
exit(EXIT_FAILURE);
} else if (pid > 0) { // Parent process
wait(NULL); II Wait for the child process to complete
} else {

0 }Scanned with OKEN Scanner


perror{"Fork failed");
}
}
return 0;
}
Q.2 Write a C program to simulate Non-preemptive Shortest
Job First {SJF) - scheduling. The arrival time and first CPU-
burst of different jobs should be input to the system. Accept
no. of Processes, arrival time and burst time. The output
should give Gantt chart, turnaround time and waiting time for
each process. Also find the average waiting time and
turnaround time
---->
#include <stdio.h>
int main() {
int n, i, j, time = 0;
printf{"Enter the number of processes: ");
scanf{"%d", &n);
int at[n], bt[n], wt[n], tat[n], process[n];
int completed = 0, total_wt = 0, total_tat = 0, min_bt, index;
int finished[n], current_time = 0;
for (i = 0; i < n; i++) {
printf{"Enter arrival time and burst time for process P%d:
II
I
i + 1)•
I

scanf{"%d %d", &at[i], &bt[i]);


process[i] = i + 1;
finished[i] = 0;
}
printf("\nGantt Chart:\n");
while (completed < n) {

0 }Scanned with OKEN Scanner


min_bt = 9999;
index=-1;
for (i = O; i < n; i++) {
if (at[i] <= current_time && !finished[i] && bt[i] < min_bt)
{
min_bt = bt[i];
index = i·
I

}
}
if (index != -1) {
current_time += bt[index];
printf("I P%d (%d) ", process[index], current_time);
tat[index] = current_time - at[index];
wt[index] = tat[index] - bt[index];
total_tat += tat[index];
total_wt += wt[index];
finished[index] = 1;
completed++;
} else {
current_time++;
}
}
pri ntf(" l\n");
printf("\n Process \tTurn around Time\tWa iti ng Ti me \n");
for (i = O; i < n; i++) {
printf("P%d\to/od\t\t%d\n", process[i], tat[i], wt[i]);
}
printf("\nAverage Turnaround Time: %.2f", (float)total_tat /
n);
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);

0 } Scanned with OKEN Scanner


return 0;
}

Slip no-15
Q.1 Write a C program to implement the shell. It should
display the command prompt "myshell$". Tokenize the
command line and execute the given command by creating
the child process. Additionally it should interpret the
following 'list' commands as
myshell$ list f dirname :- To print names of all the files in
current directory.
---->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX INPUT 100
#define MAX ARGS 10
// Function to list all files in the directory
void list_files(const char *dirname) {
struct dirent *de;
DIR *dr = opendir(dirname);
if (dr == NULL) {
perror("Could not open directory");
return;
}
while ((de= readdir(dr)) != NULL) {

0 }Scanned with OKEN Scanner


if (de->d_type == DT_REG) {
printf("%s\n", de->d_name);
}
}
closedir( dr);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
printf("myshell$ ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")) = O;
int i =o·
I

token =strtok(input, " ");


while (token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
if (args[O] == NULL) continue;
if (strcmp( args[O], "exit") == 0) {
break;
}
if (strcmp(args[O], "list") == O && strcmp(args[1], "f") == O
&& args[2] != NULL) {
list_files( args[2]);
continue;
}

0 }Scanned with OKEN Scanner


pid_t pid =fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("Error executing command");
}
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
} else {
perror("Fork failed");
}
}
return 0;
}

Q.2 Write the program to simulate preemptive Shortest Job


First (SJF) -scheduling. The arrival time and first CPU-burst
of different jobs should be input to the system. Accept no. of
Processes, arrival time and burst time. The output should
give Gantt chart, turnaround time and waiting time for each
process. Also find the average waiting time and turnaround
time
--->
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
int arrival_time;
int burst_time;
int remaining_time;

0 } Scanned with OKEN Scanner


int waiting_time;
int turnaround_time;
} Process;
void find_sjf(int n, Process processes □) {
int total_time = O;
int completed = O;
int current_time = O;
int min_remaining_time, index;
while (completed < n) {
min_remaining_time = 9999;
index=-1· I

for (int i = O; i < n; i++) {


if (processes[i].arrival_time <= current_time &&
processes[i].remaining_time > 0) {
if (processes[i].remaining_time <
min_remaining_time) {
min_remaining_time =
processes[i].rem a in i ng_time;
index = i·
I

}
}
}
if (index != -1) {
processes[index].remaining_time--;
current_time++;
if (processes[index].remaining_time == 0) {
processes[index] .turnaround_time = current_time -
processes[index].arrival_time;
processes[index].waiting_time =
processes[index].turnaround_time -

0 } Scanned with OKEN Scanner


processes[index].burst_time;
completed++;
}
} else {
current_time++;
}
}
}
void print_gantt_chart(int n, Process processes □) {
printf("\nGantt Chart:\nl");
for(int i = O; i < n; i++) {
printf(" P%d I", processes[i].id);
}
printf("\n");
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process processes[n];
for (int i = O; i < n; i++) {
processes[i].id = i + 1;
printf("Enter arrival time and burst time for process P%d:
II
I
i + 1)•
I

scanf("%d %d", &processes[i].arrival_time,


&processes[i].burst_time);
processes[i].remaining_time = processes[i].burst_time;
processes[i]. waiting_time = O;
processes[i].turnaround_time = O;
}

0 } Scanned with OKEN Scanner


find_sjf(n, processes);
int total_waiting_time = O;
int total_turnaround_time = O;
printf("\n Process \tArriva I Ti me \tBu rst Ti me\tWa iti ng
Time\tTurnaround Time\n");
for(int i = O; i < n; i++) {
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
printf("P%d\t<1/od\t\t%d\t\t%d\t\t%d\n", processes[i].id,
processes[i] .arriva l_ti me, processes[i].burst_ti me,
processes[i].waiting_time, processes[i] .turnaround_time);
}
printf("\nAverage Waiting Time: %.2f',
(float)total_waiting_time / n);
printf("\nAverage Turnaround Time: %.2f\n",
(float)total_turnaround_time / n);
print_gantt_chart(n, processes);
return O;
}

Slip no-16
Q.1 Write a programto implement the toy shell. It should
display the command prompt "myshell$". Tokenize the
command line and execute the given command by creating
the child process. Additionally it should interpret the
following commands.
count c filename :- To print number of characters in the file.
count w filename :- To print number of words in the file.
---->
#include <stdio.h>

0 } Scanned with OKEN Scanner


#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX INPUT 100
#define MAX ARGS 10
// Function to count characters in a file
void count_characters(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Could not open file");
return;
}
int count = O;
char ch;
while ((ch= fgetc(file)) != EOF) {
count++;
}
fclose(file);
printf("Number of characters in '%s': %d\n", filename,
count);
}
// Function to count words in a file
void count_words(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
perror("Could not open file");
return;
}

0 }Scanned with OKEN Scanner


int count =0;
char word[MAX_I NPUT];
while (fscanf(file, "%s", word) == 1) {
count++;
}
fclose(file);
printf("Number of words in 'o/as': %d\n", filename, count);
}
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
while (1) {
printf("myshelI$ ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] =0;
int i = o·
I

char *token = strtok(input, " ");


while (token != NULL && i < MAX_ARGS - 1) {
args[i++] = token;
token = strtok(NULL, " ");
}
args[i] = NULL;
if (args[0] == NULL) continue;
if (strcmp( args[0], "exit") == 0) {
break;
}
if (strcmp( args[0], "count") == 0) {
if (strcmp(args[1 ], "c") == 0 && args[2] != NULL) {
count_characters( args[2]);
} else if (strcmp(args[1], "w") == 0 && args[2] != NULL) {

0 }Scanned with OKEN Scanner


count_words( args[2]);
} else {
printf("lnvalid command syntax.\n");
}
continue;
}
pid_t pid =fork();
if (pid == 0) {
if (execvp(args[0], args) == -1) {
perror("Error executing command");
}
exit(EXIT_FAILURE);
} else if (pid > 0) {
wait(NULL);
} else {
perror("Fork failed");
}
}
return 0;
}

Q.2 Write the program to simulate Non preemptive priority


scheduling. The arrival time and first CPU-burst of different
jobs should be input to the system. Accept no. of Processes,
arrival time and burst time. The output should give Gantt
chart, turnaround time and waiting time for each process.
Also find the average waiting time and turnaround time.
---->
#include <stdio.h>
struct Process {

0 }Scanned with OKEN Scanner


int pid;
int arrival_time;
int burst_time;
int priority;
int completion_time;
int turnaround_time;
int waiting_time;
};
// Function to sort processes by arrival time, then priority
void sort_by_arrival_priority( struct Process pO, int n) {
struct Process temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i].arrival_time > pU].arrival_time II
(p[i].arrival_time == pU].arrival_time && p[i].priority >
pU].priority)) {
temp = p[i];
p[i] = PU];
PU] = temp;
}
}
}
}
// Function to simulate Non-preemptive Priority Scheduling
void priority_scheduling(struct Process pO, int n) {
int current_time = O;
int completed= O;
int gantt_chart[100][2], gantt_count = O;
while (completed< n) {
int idx = -1 ·
1

0 }Scanned with OKEN Scanner


int min_priority = 9999;
II Select process with the highest priority that has
arrived
for (int i = O; i < n; i++) {
if (p[i].arrival_time <= current_time &&
p[i].completion_time == 0) {
if (p[i].priority < min_priority) {
min_priority = p[i].priority;
idx = i·
I

}
}
}
if (idx != -1) {
gantt_chart[gantt_count][O] = p[idx].pid;
gantt_chart[gantt_count][1] = current_time;
gantt_count++;
current_time += p[idx].burst_time;
p[idx].completion_time = current_time;
p[idx].turnaround_time = p[idx].completion_time -
p[idx] .arrival_ti me;
p[idx].waiting_time = p[idx].turnaround_time -
p[idx] .burst_ti me;
completed++;
} else {
current_time++;
}
}

II Print Gantt Chart


printf("\nGantt Chart:\n");

0 }Scanned with OKEN Scanner


for (int i = 0; i < gantt_count; i++) {
printf("P%d [%d] -> ", gantt_chart[i][0], gantt_chart[i][1 ]);
}
printf("Fini sh \n");
// Display turnaround and waiting time for each process
printf("\n Process \tArriva I\tBurst\tPriority\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t<3/od\t%d\t%d\t\t%d\t%d\n", p[i].pid,
p[i].arrival_time, p[i].burst_time, p[i].priority,
p[i].turnaround_time, p[i].waiting_time);
}
}
// Function to calculate average waiting and turnaround times
void calculate_average(struct Process pO, int n) {
float avg_turnaround_time = 0.0, avg_waiting_time = 0.0;
for (int i = 0; i < n; i++) {
avg_turnaround_time += p[i].turnaround_time;
avg_waiting_time += p[i].waiting_time;
}
printf("\nAverage Turnaround Time: %.2f",
avg_turnaround_time / n);
printf("\nAverage Waiting Time: %.2f\n", avg_waiting_time
/ n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i = 0; i < n; i++) {

0 } Scanned with OKEN Scanner


p[i].pid =i + 1;
printf{"Enter arrival time of process P%d: ", i + 1);
sea nf{"%d ", &p [i]. arriva I_ti me);
printf{"Enter burst time of process P%d: ", i + 1);
scanf{"%d", &p[i] .burst_time);
printf{"Enter priority of process P%d: ", i + 1);
scanf{"%d", &p[i].priority);
p[i].completion_time = O; // Initialize completion time to
0
}
sort_by_arrival_priority(p, n);
priority_schedu Ii ng (p, n);
calculate_average(p, n);
return O;
}

Slip no-17
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the Optimal page replacement algorithm. Assume
the memory of n frames.
Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,
---->
#include <stdio.h>
// Function to check if a page is already in a frame
int is_page_in_memory(int page, int frames □, int n) {
for (int i = O; i < n; i++) {
if {frames[i] == page) {
return 1; // Page found
}

0 } Scanned with OKEN Scanner


}
return O; // Page not found
}
// Function to find the optimal page to replace
int find_optimal_page(int reference_stringO, int frames □, int n,
int index, int reference_length) {
int farthest = index;
int replace_index = -1;
for(int i =O; i < n; i++) {
int j;
for U= index; j < reference_length; j++) {
if (frames[i] == reference_stringfj]) {
if U> farthest) {
farthest = j;
replace_index = i;
}
break;
}
}
if U== reference_length) {
return i; // If the page is not referenced in the future,
return it for replacement
}
}
return (replace_index == -1) ? 0 : replace_index;
}
// Function to simulate Optimal Page Replacement
void optimal_page_replacement(int reference_string[], int
reference_length, int frames□, int n) {
int page_faults = O;

0 } Scanned with OKEN Scanner


int index = O; // To keep track of the current page being
referenced
printf("Page scheduling:\n");
for (int i = O; i < reference_length; i++) {
int current_page = reference_string[i];
// Check if the page is already in memory
if (!is_page_in_memory(current_page, frames, n)) {
page_faults++;
if (index< n) {
// If there is still space in memory, simply add the
page
frames[index++] = current_page;
} else {
// Find the optimal page to replace
int replace_index =
find_optimal_page(reference_string, frames, n, i + 1,
reference_length);
frames[replace_index] = current_page;
}
// Display the current state of frames
for (intj = O;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
pri ntf("\n");
} else {
// Display the current state if there is no page fault

0 } Scanned with OKEN Scanner


for (intj = O;j < n;j++) {
if (framesU] != -1) {
printf("%d ", framesU]);
} else {
printf("- ");
}
}
printf(" (No page fault)\n");
}
}
printf("\nTotal number of page faults: %d\n", page_faults);
}
int main() {
int reference_string[] = {7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6};
int reference_length = sizeof(reference_string) /
sizeof(reference_string[O]);
int n;
printf("Enter the number of frames: ");
scanf("%d", &n);
int frames[n];
for (int i = O; i < n; i++) {
frames[i] = -1; // Initialize frames with -1 (empty)
}
optima l_page_replacement( reference_stri ng,
reference_length, frames, n);
return O;
}

Q.2 Write the program to simulate FCFS CPU-scheduling. The


arrival time andfirst CPU-burst of different jobs should be

0 }Scanned with OKEN Scanner


input to the system. Accept no. of Processes, arrival time
and burst time. The output should give Gantt chart,
turnaround time and waiting time for each process. Also find
the average waiting time and turnaround time.
----->
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
void sort_by_arrival(struct Process p[], int n) {
struct Process temp;
for (int i = O; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (p[i].arrival_time > pU].arrival_time) {
temp = p[i];
p[i] = PU];
PU] = temp;
}
}
}
}
void fcfs_scheduling(struct Process p[], int n) {
int current_time = O;
printf("\nGantt Chart:\n");
for(int i = O; i < n; i++) {

0 } Scanned with OKEN Scanner


if (current_time < p[i].arrival_time) {
current_time = p[i].arrival_time;
}
printf("P%d [%d - %d] -> ", p[i].pid, current_time,
current_time + p[i].burst_time);
p[i].completion_time = current_time + p[i].burst_time;
p[i].turnaround_time = p[i].completion_time -
p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
current_time += p[i].burst_time;
}
printf("Fini sh \n");
}
void calculate_and_display_avg_times(struct Process pO, int
n) {
float total_turnaround_time = 0, total_waiting_time = O;
printf("\nProcess\tArrival\tBurst\tCompletion\tTAT\tWT\n");
for(int i = O; i < n; i++) {
total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
printf("P%d\to/od\t%d\t%d\t\t%d\t%d\n", p[i].pid,
p[i].arrival_time, p[i].burst_time,
p[i].completion_time, p[i].turnaround_time,
p[i].waiting_time);
}
printf("\nAverage Turnaround Time: %.2f",
total_turnaround_time / n);
printf("\nAverage Waiting Time: %.2f\n", total_waiting_time
/ n);
}

0 } Scanned with OKEN Scanner


int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i = O; i < n; i++) {
p[i].pid = i + 1;
printf("Enter arrival time for process P%d: ", i + 1);
scanf("%d", &p[i] .arrival_time);
printf("Enter burst time for process P%d: ", i + 1 );
scanf("%d", &p[i] .burst_time);
}
sort_by_arrival(p, n);
fcfs_scheduling(p, n);
calculate_and_display_avg_times(p, n);
return O;
}

Slip no-18
Q.1 Write the simulation program for demand paging and
show the page scheduling and total number of page faults
according the LRU page replacement algorithm. Assume the
memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
---->
#include <stdio.h>
int find_lru(int time □, int n) {
int minimum =time[O], pas =O;
for (int i = 1; i < n; ++i) {
if (time[i] < minimum) {

0 }Scanned with OKEN Scanner


minimum =time[i];
pos =.1;
}
}
return pos;
}
void lru_page_replacement(int reference_stringO, int
reference_length, int frames[], int n) {
int time[n], flag1, flag2, pos, page_faults =0, current_time =
o· I

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


frames[i] = -1; II Initialize frames as empty
}
printf("Page scheduling:\n");
for (int i = 0; i < reference_length; ++i) {
flag1 =flag2 =0;
for (int j = 0; j < n; ++j) {
if (framesO] == reference_string[i]) { II Page hit
flag1 =flag2 =1;
timeO] = ++current_time; II Update time for LRU
tracking
break;
}
}
if (flag1 == 0) {
for (int j = 0; j < n; ++j) {
if (frames0] == -1) { II Empty frame found
page_faults++;
framesO] = reference_string[i];
timeu] = ++current_time;

0 } Scanned with OKEN Scanner


flag2 = 1;
break;
}
}
}
if (flag2 == 0) { // Page replacement needed
pos = find_lru(time, n);
frames[pos] = reference_string[i];
time[pos] = ++current_time;
page_faults++;
}
// Display current frame status
for (int j = O; j < n; ++j) {
if (frames0] != -1) {
printf("%d ", framesU]);
} else {
pri ntf("- ");
}
}
printf("\n");
}
printf("\nTotal number of page faults: %d\n", page_faults);
}
int main() {
int reference_string[] = {3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6};
int reference_length = sizeof(reference_string) /
sizeof(reference_string[O]);
int n;
printf("Enter the number of frames: ");
scanf("%d", &n);

0 }Scanned with OKEN Scanner


int frames[n];
lru_page_replacement(reference_string, reference_length,
frames, n);
return O;
}

Q.2 Write a C program to simulate FCFS CPU-scheduling. The


arrival time and first CPU-burst of different jobs should be
input to the system. Accept no. of Processes, arrival time
and burst time. The output should give Gantt chart,
turnaround time and waiting time for each process. Also find
the average waiting time and turnaround time.
----->
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
void sort_by_arrival{struct Process p[], int n) {
struct Process temp;
for {int i = O; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if {p[i].arrival_time > pU].arrival_time) {
temp = p[i];
p[i] = PU];
pU] = temp;

0 } Scanned with OKEN Scanner


}
}
}
}
void fcfs_scheduling(struct Process p[], int n) {
int current_time = O;
printf("\nGantt Chart:\n");
for(int i = O; i < n; i++) {
if (current_time < p[i].arrival_time) {
current_time = p[i].arrival_time;
}
printf("P%d [%d - %d] -> ", p[i] .pid, current_time,
current_time + p[i].burst_time);
p[i].completion_time = current_time + p[i].burst_time;
p[i].turnaround_time = p[i].completion_time -
p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i] .burst_time;
current_time += p[i].burst_time;
}
printf("Fini sh \n");
}
void calculate_and_display_avg_times(struct Process pO, int
n) {
float total_turnaround_time = 0, total_waiting_time = O;
pri ntf("\n Process\tArriva 1\tBurst\tCom pletion \tTAT\tWT\n");
for (int i = O; i < n; i++) {
total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
printf("P%d\to/od\t%d\t%d\t\t%d\t%d\n", p[i].pid,
p[i].arrival_time, p[i].burst_time,

0 }Scanned with OKEN Scanner


p[i].completion_time, p[i].turnaround_time,
p[i]. waiting_time);
}
printf("\nAverage Turnaround Time: %.2f",
total_turnaround_time / n);
printf("\nAverage Waiting Time: %.2f\n", total_waiting_time
/ n);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i = O; i < n; i++) {
p[i].pid = i + 1;
printf("Enter arrival time for process Po/od: ", i + 1);
scanf("%d", &p[i] .arrival_ti me);
printf("Enter burst time for process P%d: ", i + 1);
scanf("%d", &p[i].burst_time);
}
sort_by_arrival(p, n);
fcfs_scheduling(p, n);
calculate_and_display_avg_times(p, n);
return O;
}

0 } Scanned with OKEN Scanner


Slip no-19
Q.1 Write a C program to implement the shell. It should
display the command prompt "myshell$". Tokenize the
command line and execute the given command by creating
the child process. Additionally it should interpret the
following 'list' commands as
myshell$ list f dirname :- To print names of all the files in
current directory.
----->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
#define MAX- CMD- LEN 1024
#define MAX ARGS 100
void list_files_in_directory(const char *dirname) {
struct dirent *de;
DIR *dr = opendir(dirname);
if (dr == NULL) {
printf("Could not open current directory\n");
return;
}
printf("Files in directory %s:\n", dirname);
while ((de= readdir(dr)) != NULL) {
printf("%s\n", de->d_name);
}
closedir( dr);
}

0 }Scanned with OKEN Scanner


void execute_command{char **args) {
pid_t pid =fork();
if (pid < 0) {
printf{"Failed to create a new process\n");
} else if (pid == 0) {//Child process
if (execvp(args[0], args) == -1) {
perror("myshell");
}
exit{EXIT_FAILURE);
} else { // Parent process
wait{NULL);
}
}
int main() {
char command[MAX_CMD_LEN];
char *args[MAX_ARGS];
char *token;
int should_run = 1;
while {should_run) {
printf{"myshel1$ ");
fgets(command, MAX_CMD_LEN, stdin);
command[strlen(command) - 1] = '\0'; // Remove
newline character
int arg_count = 0;
token = strtok{command, '
1
");

while {token != NULL) {


args[arg_count++] = token;
token = strtok(NULL, " ");
}
args[arg_count] = NULL;

0 }Scanned with OKEN Scanner


if (arg_count == 0) {
continue;// No input, prompt again
}
if (strcmp( args[0], "exit") == 0) {
should_run = 0;
} else if (strcmp(args[0], "list") == 0 && arg_count == 3 &&
strcmp( args[1 ], "f") == 0) {
Ii st_f i Ies_in_directory(arg s [2]);
} else {
execute_command(args);
}
}
return 0;
}

Q.2 Write the simulation program for Round Robin


scheduling for given time quantum. The arrival time and first
CPU-burst of different jobs should be input to the system.
Accept no. of Processes, arrival time and burst time. The
output should give the Gantt chart, turnaround time and
waiting time for each process. Also display the average
turnaround time and average waiting time.
----->
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int remaining_time;
int completion_time;

0 }Scanned with OKEN Scanner


int turnaround_time;
int waiting_time;
};
void round_robin(struct Process pO, int n, int time_quantum) {
int current_time = 0, completed = 0, i = O;
int wait_time = 0, tat = O;
printf("\nGantt Chart:\n");
while (completed != n) {
if (p[i].remaining_time > O && p[i].arrival_time <=
current_time) {
int time_spent = (p[i].remaining_time > time_quantum)
? time_quantum : p[i].remaining_time;
printf("P%d [%d - %d] -> ", p[i].pid, current_time,
current_time + time_spent);
p[i].remaining_time -= time_spent;
current_time += time_spent;
if (p[i].remaining_time == 0) {
p[i].completion_time = current_time;
p[i].turnaround_time = p[i].completion_time -
p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time -
p[i].burst_time;
wait_time += p[i].waiting_time;
tat+= p[i].turnaround_time;
completed++;
}
}
i = (i + 1) % n;
}
printf("Fini sh \n");

0 } Scanned with OKEN Scanner


printf{"\nProcess\tArrival\tBurst\tCompletion\tTAT\tWT\n");
for (int i = O; i < n; i++) {
printf{"P%d\to/od\t%d\t%d\t\t%d\t%d\n", p[i].pid,
p[i].arrival_time, p[i].burst_time,
p[i].completion_time, p[i].turnaround_time,
p[i]. waiting_time);
}
printf{"\nAverage Turnaround Time: %.2f", {float)tat / n);
printf{"\nAverage Waiting Time: %.2f\n", {float)wait_time /
n);
}
int main() {
int n, time_quantum;
printf{"Enter the number of processes: ");
scanf{"%d", &n);
struct Process p[n];
for (int i = O; i < n; i++) {
p[i].pid = i + 1;
printf{"Enter arrival time for process P%d: ", i + 1);
scanf{"%d", &p[i] .arrival_ti me);
printf("Enter burst time for process P%d: ", i + 1);
scanf("%d", &p[i].burst_time);
p[i].remaining_time = p[i].burst_time; // Initialize
remaining time
}
printf{"Enter the time quantum: ");
scanf{"%d", &time_quantum);
round_robin(p, n, time_quantum);
return O;
}

0 } Scanned with OKEN Scanner


Slip no-20
Q.1 Write a C program to implement the shell which displays
the command prompt "myshell$". It accepts the command,
tokenize the command line and execute it by creating the
child process. Also implement the additional command
'typeline' as
typeline -a filename :- To print all lines in the file.
->
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX- CMD- LEN 1024
#define MAX ARGS 100
// Function to implement the 'typeline -a filename' command
void typeline_all{char *filename) {
FILE *file =fopen{filename, "r");
char line[256];
if {file == NULL) {
perror("myshel I");
return;
}
// Print each line of the file
while {fgets{line, sizeof{line), file)) {
printf{"%s", line);
}
fclose(file);
}

0 }Scanned with OKEN Scanner


// Function to execute regular shell commands
void execute_command(char **args) {
pid_t pid =fork();
if (pid < 0) {
perror("myshel I");
} else if (pid == 0) {//Child process
if (execvp(args[0], args) == -1) {
perror("myshell");
}
exit(EXIT_FAILURE);
} else { // Parent process
wait(NULL);
}
}
int main() {
char command[MAX_CMD_LEN];
char *args[MAX_ARGS];
char *token;
int should_run = 1;
while (should_run) {
printf("myshell$ ");
fgets(command, MAX_CMD_LEN, stdin);
command[strlen(command) - 1] = '\0'; // Remove
newline character
int arg_count = 0;
1
token = strtok(command, ' ");

while (token != NULL) {


args[arg_count++] = token;
token = strtok(NULL, " ");
}

0 }Scanned with OKEN Scanner


args[arg_count] = NULL;
if (arg_count == 0) {
continue;// No input, prompt again
}
// Check for exit command
if (strcmp( args[0], "exit") == 0) {
should_run = 0;
}
// Implement 'typeline -a filename'
else if (strcmp(args[0], "typeline") == 0 && arg_count ==
3 && strcmp(args[1 ], "-a") == 0) {
type Iine_a 11(args[2]);
}
// Execute other commands
else {
execute_command(args);
}
}
return 0;
}

0 } Scanned with OKEN Scanner

You might also like