os
os
os
return O;
}
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++) {
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;
}
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("- ");
}
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;
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]);
}
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
}
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];
}
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);
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)
}
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");
while (1) {
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;
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.
----->
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");
}
}
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);
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) {
}
}
}
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 -
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>
}
}
}
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++;
}
}
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
}
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) {