Ans os
Ans os
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
#include <stdio.h>
min = freq[frames[i]];
minIndex = i;
return minIndex;
int main() {
scanf("%d", &n);
int page_count;
scanf("%d", &page_count);
scanf("%d", &pages[i]);
freq[page]++;
int found = 0;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
if (size < n) {
frames[size++] = page;
} else {
frames[lfuIndex] = page;
page_faults++;
}
printf("Frames: ");
printf("\n");
return 0;
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
if (file == NULL) {
return;
char line[MAX];
if (strcmp(option, "-a") == 0) {
printf("%s", line);
printf("%s", line);
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
i++;
if (strcmp(args[0], "exit") == 0) {
break;
typeline(args[1], args[2]);
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
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
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
#include <stdio.h>
int main() {
int page_count;
scanf("%d", &n);
scanf("%d", &page_count);
scanf("%d", &pages[i]);
frames[i] = -1;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
frames[index] = page;
index = (index + 1) % n;
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
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
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>
if (dir == NULL) {
return;
}
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
closedir(dir);
int count = 0;
if (dir == NULL) {
return;
count++;
closedir(dir);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "f") == 0) {
list_files(args[2]);
count_entries(args[2]);
} else {
}
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
return 0;
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
frames.
#include <stdio.h>
min = time[i];
minIndex = i;
return minIndex;
int main() {
scanf("%d", &n);
scanf("%d", &page_count);
scanf("%d", &pages[i]);
if (frames[j] == page) {
found = 1;
break;
if (!found) {
int index;
if (i < n) {
} else {
frames[index] = page;
time[index] = ++counter;
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
}
printf("\n");
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
following commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <ctype.h>
return;
int char_count = 0;
char ch;
char_count++;
fclose(file);
if (file == NULL) {
return;
int word_count = 0;
word_count++;
prev = ch;
if (!isspace(prev)) {
word_count++;
}
fclose(file);
if (file == NULL) {
return;
int line_count = 0;
char ch;
if (ch == '\n') {
line_count++;
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
input[strlen(input) - 1] = '\0';
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "c") == 0) {
count_chars(args[2]);
count_words(args[2]);
count_lines(args[2]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
} else {
wait(NULL);
return 0;
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
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
#include <stdio.h>
max = freq[i];
maxIndex = i;
}
return maxIndex;
int main() {
scanf("%d", &n);
scanf("%d", &page_count);
scanf("%d", &pages[i]);
frames[i] = -1;
if (frames[j] == page) {
found = 1;
freq[j]++;
break;
}
}
if (!found) {
int index;
if (i < n) {
index = i;
} else {
frames[index] = page;
freq[index] = 1;
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
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
following commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
while (fgets(line, sizeof(line), file) != NULL) {
line_number++;
fclose(file);
if (file == NULL) {
return;
char line[MAX];
int count = 0;
count++;
ptr += strlen(pattern);
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
input[strlen(input) - 1] = '\0';
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "a") == 0) {
search_all(args[2], args[3]);
search_count(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
return 0;
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>
int findOptimal(int frames[], int ref[], int n, int currentIndex, int ref_len) {
int farthest = currentIndex, index = -1;
int j;
if (frames[i] == ref[j]) {
if (j > farthest) {
farthest = j;
index = i;
break;
if (j == ref_len) {
return i;
int main() {
scanf("%d", &n);
scanf("%d", &ref_len);
frames[i] = -1;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
if (i < n) {
frames[i] = page;
} else {
frames[index] = page;
page_faults++;
if (frames[j] != -1) {
printf("%d ", frames[j]);
} else {
printf("- ");
printf("\n");
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
following commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 100
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
fclose(file);
return;
line_number++;
fclose(file);
if (file == NULL) {
char line[MAX];
int count = 0;
count++;
ptr += strlen(pattern);
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
int i = 0;
i++;
if (args[0] == NULL) {
if (strcmp(args[0], "exit") == 0) {
if (strcmp(args[1], "f") == 0) {
search_first(args[2], args[3]);
search_count(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
}
}
return 0;
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
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
#include <stdio.h>
int findMRU(int frames[], int ref[], int n, int currentIndex, int ref_len) {
int j;
if (frames[i] == ref[j]) {
if (j > maxTime) {
maxTime = j;
mruIndex = i;
break;
}
}
return mruIndex;
int main() {
scanf("%d", &n);
scanf("%d", &ref_len);
scanf("%d", &ref[i]);
frames[i] = -1;
if (frames[j] == page) {
found = 1;
break;
}
if (!found) {
if (i < n) {
frames[i] = page;
} else {
frames[index] = page;
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
return 0;
}
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
commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
fclose(file);
return;
line_number++;
fclose(file);
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
int found = 0;
found = 1;
line_number++;
}
if (!found) {
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
int i = 0;
i++;
if (args[0] == NULL) {
if (strcmp(args[0], "exit") == 0) {
break; // Exit the shell
if (strcmp(args[1], "f") == 0) {
search_first(args[2], args[3]);
search_all(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
return 0;
Slip no-7
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, 2
#include <stdio.h>
int findOptimal(int frames[], int n, int ref[], int currentIndex, int ref_len) {
int j;
if (frames[i] == ref[j]) {
if (j > farthest) {
farthest = j;
optimalIndex = i;
break;
if (j == ref_len) {
int main() {
scanf("%d", &n);
scanf("%d", &ref_len);
scanf("%d", &ref[i]);
if (frames[j] == page) {
break;
if (!found) {
if (i < n) {
} else {
int index = findOptimal(frames, n, ref, i + 1, ref_len);
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
return 0;
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
commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
int found = 0;
found = 1;
line_number++;
}
if (!found) {
fclose(file);
if (file == NULL) {
return;
char line[MAX];
int count = 0;
count++;
ptr++;
fclose(file);
}
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
int i = 0;
i++;
if (args[0] == NULL) {
if (strcmp(args[0], "exit") == 0) {
if (strcmp(args[1], "a") == 0) {
search_all(args[2], args[3]);
count_occurrences(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
return 0;
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
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
#include <stdio.h>
minTime = time[i];
lruIndex = i;
return lruIndex;
int main() {
scanf("%d", &n);
scanf("%d", &ref_len);
scanf("%d", &ref[i]);
}
for (int i = 0; i < ref_len; i++) {
if (frames[j] == page) {
break;
if (!found) {
int lruIndex;
if (frames[j] == -1) {
break;
if (j == n - 1) {
page_faults++;
if (frames[j] != -1) {
printf("%d ", frames[j]);
} else {
printf("- ");
printf("\n");
return 0;
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
commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
fclose(file);
return;
line_number++;
fclose(file);
if (file == NULL) {
char line[MAX];
int count = 0;
count++;
ptr++;
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "f") == 0) {
search_first(args[2], args[3]);
count_occurrences(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
}
}
return 0;
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
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &n);
scanf("%d", &ref_len);
frames[i] = -1;
int found = 0;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
frames[rear] = page;
rear = (rear + 1) % n;
if (front == rear) {
front = (front + 1) % n;
page_faults++;
if (frames[j] != -1) {
printf("- ");
printf("\n");
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX 1024
if (file == NULL) {
return;
char line[MAX];
int line_number = 1;
fclose(file);
return;
line_number++;
fclose(file);
if (file == NULL) {
char line[MAX];
int line_number = 1;
int found = 0;
found = 1;
line_number++;
if (!found) {
fclose(file);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "f") == 0) {
search_first(args[2], args[3]);
search_all(args[2], args[3]);
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
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
Reference String : 2, 4, 5, 6, 9, 4, 7, 3, 4, 5, 6, 7, 2, 4, 7, 1
#include <stdio.h>
#include <stdlib.h>
int main() {
scanf("%d", &n);
scanf("%d", &ref[i]);
int found = 0;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
if (front == rear) {
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
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
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>
char filepath[MAX];
if (dir == NULL) {
return;
if (show_inodes) {
stat(filepath, &file_stat);
} else {
printf("%s\n", entry->d_name);
}
}
closedir(dir);
int main() {
char input[MAX];
char *args[MAX];
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
i++;
if (args[0] == NULL) {
continue;
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[1], "f") == 0) {
list_files(args[2], 0); // List files
} else {
} else {
if (pid == 0) {
execvp(args[0], args);
exit(1);
} else {
wait(NULL);
return 0;
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
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
#include <stdio.h>
#define MAX_FRAMES 10
minFreq = freq[i];
minIndex = i;
return minIndex;
int main() {
scanf("%d", &n);
int i = 0;
while (1) {
int page;
scanf("%d", &page);
referenceString[i++] = page;
}
int refCount = i;
frames[i] = -1;
freq[i] = 0;
found = 0;
if (frames[j] == page) {
found = 1;
break;
if (!found) {
pageFaults++;
if (frames[n - 1] == -1) {
frames[j] = page;
freq[j] = 1;
break;
} else {
freq[index] = 1;
printf("\nFrames: ");
if (frames[j] != -1)
else
printf("- ");
return 0;
}
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
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_ARGS 10
if (dp == NULL) {
perror("opendir");
return;
closedir(dp);
int count = 0;
if (dp == NULL) {
perror("opendir");
return;
count++;
closedir(dp);
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
// Exit command
break;
if (strcmp(args[1], "f") == 0) {
list_files(args[2]);
} else {
} else {
continue;
perror("execvp");
exit(EXIT_FAILURE);
perror("fork");
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
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
min = time[i];
pos = i;
return pos;
int main() {
scanf("%d", &n);
// Initialize frames
frames[i] = -1;
referenceString[i] = referenceStringInput[i];
printf("\n");
flag1 = flag2 = 0;
if (frames[j] == referenceString[i]) {
counter++;
flag1 = flag2 = 1;
break;
}
}
if (flag1 == 0) {
counter++;
pageFaults++;
frames[j] = referenceString[i];
time[j] = counter;
flag2 = 1;
break;
if (flag2 == 0) {
counter++;
pageFaults++;
frames[pos] = referenceString[i];
time[pos] = counter;
printf("\nFrames: ");
if (frames[j] != -1)
printf("- ");
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
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_ARGS 10
void list_files(const char *dirname) {
if (entry->d_name[0] != '.') {
printf("%s\n", entry->d_name);
closedir(dp);
int count = 0;
if (entry->d_name[0] != '.') {
count++;
closedir(dp);
printf("%d\n", count);
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
args[i] = NULL;
if (strcmp(args[1], "f") == 0) {
list_files(args[2]);
list_count(args[2]);
}
continue;
if (pid == 0) {
exit(EXIT_FAILURE);
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
command ‘typeline’ as
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MAX_ARGS 10
char line[256];
if (file == NULL) {
return;
printf("%s", line);
fclose(file);
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
// Display the prompt
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
args[i] = NULL;
// Exit command
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[0], "typeline") == 0) {
typeline_all(args[2]);
} else {
printf("Usage: typeline -a filename\n");
continue;
exit(EXIT_FAILURE);
} 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() {
scanf("%d", &n);
int at[n], bt[n], rt[n]; // Arrival time, burst time, remaining time
printf("Enter arrival time and burst time for process P%d: ", i+1);
rt[i] = bt[i];
scanf("%d", &time_quantum);
printf("\nGantt Chart:\n");
while (remain != 0) {
time += rt[i];
rt[i] = 0;
remain--;
} else {
rt[i] -= time_quantum;
time += time_quantum;
printf("|\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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_ARGS 10
char line[256];
int count = 0;
if (file == NULL) {
return;
// Read and print the first 'n' lines from the file
printf("%s", line);
count++;
}
fclose(file);
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
char *token;
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
args[i] = NULL;
// Exit command
if (strcmp(args[0], "exit") == 0) {
break;
if (strcmp(args[0], "typeline") == 0) {
} else {
continue;
exit(EXIT_FAILURE);
} else {
perror("Fork failed");
return 0;
}
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;
scanf("%d", &n);
printf("Enter arrival time and burst time for process P%d: ", i + 1);
process[i] = i + 1;
finished[i] = 0;
printf("\nGantt Chart:\n");
min_bt = 9999;
index = -1;
min_bt = bt[i];
index = i;
if (index != -1) {
current_time += bt[index];
total_tat += tat[index];
total_wt += wt[index];
finished[index] = 1;
completed++;
} else {
current_time++;
printf("|\n");
}
printf("\nAverage Turnaround Time: %.2f", (float)total_tat / n);
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
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_ARGS 10
return;
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$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
args[i] = NULL;
if (strcmp(args[0], "exit") == 0) {
break;
list_files(args[2]);
continue;
if (pid == 0) {
exit(EXIT_FAILURE);
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;
int waiting_time;
int turnaround_time;
} Process;
int total_time = 0;
int completed = 0;
int current_time = 0;
min_remaining_time = 9999;
index = -1;
min_remaining_time = processes[i].remaining_time;
index = 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 -
processes[index].burst_time;
completed++;
} else {
current_time++;
printf("\nGantt Chart:\n|");
}
printf("\n");
int main() {
int n;
scanf("%d", &n);
Process processes[n];
processes[i].id = i + 1;
printf("Enter arrival time and burst time for process P%d: ", i + 1);
processes[i].remaining_time = processes[i].burst_time;
processes[i].waiting_time = 0;
processes[i].turnaround_time = 0;
find_sjf(n, processes);
int total_waiting_time = 0;
int total_turnaround_time = 0;
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time,
processes[i].turnaround_time);
}
print_gantt_chart(n, processes);
return 0;
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
following commands.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MAX_ARGS 10
if (file == NULL) {
return;
int count = 0;
char ch;
count++;
fclose(file);
if (file == NULL) {
return;
int count = 0;
char word[MAX_INPUT];
fclose(file);
int main() {
char input[MAX_INPUT];
char *args[MAX_ARGS];
while (1) {
printf("myshell$ ");
input[strcspn(input, "\n")] = 0;
int i = 0;
args[i++] = token;
args[i] = NULL;
if (strcmp(args[0], "exit") == 0) {
break;
}
if (strcmp(args[0], "count") == 0) {
count_characters(args[2]);
count_words(args[2]);
} else {
continue;
if (pid == 0) {
exit(EXIT_FAILURE);
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.
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int priority;
int completion_time;
int turnaround_time;
int waiting_time;
};
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
int current_time = 0;
int completed = 0;
min_priority = p[i].priority;
idx = i;
if (idx != -1) {
gantt_chart[gantt_count][0] = 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_time;
completed++;
} else {
current_time++;
printf("\nGantt Chart:\n");
printf("Finish\n");
printf("\nProcess\tArrival\tBurst\tPriority\tTAT\tWT\n");
avg_turnaround_time += p[i].turnaround_time;
avg_waiting_time += p[i].waiting_time;
}
printf("\nAverage Turnaround Time: %.2f", avg_turnaround_time / n);
int main() {
int n;
scanf("%d", &n);
p[i].pid = i + 1;
scanf("%d", &p[i].arrival_time);
scanf("%d", &p[i].burst_time);
scanf("%d", &p[i].priority);
sort_by_arrival_priority(p, n);
priority_scheduling(p, n);
calculate_average(p, n);
return 0;
}
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
Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,
#include <stdio.h>
if (frames[i] == page) {
int j;
if (frames[i] == reference_string[j]) {
if (j > farthest) {
farthest = j;
replace_index = i;
break;
if (j == reference_length) {
int page_faults = 0;
printf("Page scheduling:\n");
page_faults++;
if (index < n) {
// If there is still space in memory, simply add the page
frames[index++] = current_page;
} else {
frames[replace_index] = current_page;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
} else {
if (frames[j] != -1) {
} else {
printf("- ");
}
printf("\nTotal number of page faults: %d\n", page_faults);
int main() {
int n;
scanf("%d", &n);
int frames[n];
return 0;
Q.2 Write the 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
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
temp = p[i];
p[i] = p[j];
p[j] = temp;
int current_time = 0;
printf("\nGantt Chart:\n");
current_time = p[i].arrival_time;
}
printf("P%d [%d - %d] -> ", p[i].pid, current_time, current_time +
p[i].burst_time);
current_time += p[i].burst_time;
printf("Finish\n");
printf("\nProcess\tArrival\tBurst\tCompletion\tTAT\tWT\n");
total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
int main() {
int n;
scanf("%d", &n);
scanf("%d", &p[i].arrival_time);
scanf("%d", &p[i].burst_time);
sort_by_arrival(p, n);
fcfs_scheduling(p, n);
calculate_and_display_avg_times(p, n);
return 0;
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
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
#include <stdio.h>
minimum = time[i];
pos = i;
return pos;
}
printf("Page scheduling:\n");
flag1 = flag2 = 0;
flag1 = flag2 = 1;
break;
if (flag1 == 0) {
page_faults++;
frames[j] = reference_string[i];
time[j] = ++current_time;
flag2 = 1;
break;
frames[pos] = reference_string[i];
time[pos] = ++current_time;
page_faults++;
if (frames[j] != -1) {
} else {
printf("- ");
printf("\n");
int main() {
scanf("%d", &n);
int frames[n];
return 0;
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
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int completion_time;
int turnaround_time;
int waiting_time;
};
temp = p[i];
p[i] = p[j];
p[j] = temp;
int current_time = 0;
printf("\nGantt Chart:\n");
current_time = p[i].arrival_time;
current_time += p[i].burst_time;
printf("Finish\n");
printf("\nProcess\tArrival\tBurst\tCompletion\tTAT\tWT\n");
total_turnaround_time += p[i].turnaround_time;
total_waiting_time += p[i].waiting_time;
int main() {
int n;
scanf("%d", &n);
p[i].pid = i + 1;
scanf("%d", &p[i].arrival_time);
scanf("%d", &p[i].burst_time);
sort_by_arrival(p, n);
fcfs_scheduling(p, n);
calculate_and_display_avg_times(p, n);
return 0;
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
directory.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <dirent.h>
if (dr == NULL) {
printf("%s\n", de->d_name);
closedir(dr);
if (pid < 0) {
perror("myshell");
exit(EXIT_FAILURE);
wait(NULL);
int main() {
char command[MAX_CMD_LEN];
char *args[MAX_ARGS];
char *token;
int should_run = 1;
while (should_run) {
printf("myshell$ ");
int arg_count = 0;
args[arg_count++] = token;
args[arg_count] = NULL;
if (arg_count == 0) {
if (strcmp(args[0], "exit") == 0) {
should_run = 0;
list_files_in_directory(args[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;
int turnaround_time;
int waiting_time;
};
printf("\nGantt Chart:\n");
while (completed != n) {
p[i].remaining_time -= time_spent;
current_time += time_spent;
if (p[i].remaining_time == 0) {
p[i].completion_time = current_time;
wait_time += p[i].waiting_time;
tat += p[i].turnaround_time;
completed++;
i = (i + 1) % n;
printf("Finish\n");
printf("\nProcess\tArrival\tBurst\tCompletion\tTAT\tWT\n");
}
int main() {
int n, time_quantum;
scanf("%d", &n);
p[i].pid = i + 1;
scanf("%d", &p[i].arrival_time);
scanf("%d", &p[i].burst_time);
scanf("%d", &time_quantum);
round_robin(p, n, time_quantum);
return 0;
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
command ‘typeline’ as
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
char line[256];
if (file == NULL) {
perror("myshell");
return;
printf("%s", line);
fclose(file);
}
// Function to execute regular shell commands
if (pid < 0) {
perror("myshell");
perror("myshell");
exit(EXIT_FAILURE);
wait(NULL);
int main() {
char command[MAX_CMD_LEN];
char *args[MAX_ARGS];
char *token;
int should_run = 1;
while (should_run) {
printf("myshell$ ");
int arg_count = 0;
token = strtok(command, " ");
args[arg_count++] = token;
args[arg_count] = NULL;
if (arg_count == 0) {
if (strcmp(args[0], "exit") == 0) {
should_run = 0;
typeline_all(args[2]);
else {
execute_command(args);
return 0;