Uploaded by Shubhi Shyamsukha

lab cat merged

advertisement
CODES
1)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/wait.h>
#define MAX_LENGTH 100
int countVowels(char sentence[]) {
int count = 0;
int length = strlen(sentence);
for (int i = 0; i < length; i++) {
char c = tolower(sentence[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
return count;
}
int countWords(char sentence[]) {
int count = 0;
int length = strlen(sentence);
int isWord = 0;
for (int i = 0; i < length; i++) {
if (isalpha(sentence[i]) && !isWord) {
count++;
isWord = 1;
} else if (!isalpha(sentence[i]) && isWord)
{ isWord = 0;
}
}
return count;
}
int main() {
char sentence[MAX_LENGTH];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0'; // Remove trailing newline character
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
// Child process
int numWords = countWords(sentence);
printf("Child Process:\n");
printf("Number of words in the sentence: %d\n",
numWords); } else {
//
Parent process int
status; waitpid(pid,
&status, 0);
if (WIFEXITED(status)) {
int numVowels = countVowels(sentence);
printf("Parent Process:\n");
printf("Number of vowels in the sentence: %d\n", numVowels);
} else {
fprintf(stderr, "Child process did not terminate normally\n");
return 1;
}
}
return 0;
}
1.
1.
Kaushal
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int count_vowels(const char *sentence) {
int vowels = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
char ch = tolower(sentence[i]);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
}
return vowels;
}
int count_words(const char *sentence) {
int words = 0;
int is_word = 0;
for (int i = 0; sentence[i] != '\0'; i++) {
if (isalpha(sentence[i])) {
if (!is_word) {
is_word = 1;
words++;
}
} else {
is_word = 0;
}
}
return words;
}
int main() {
char sentence[100];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0'; // remove trailing
newline
pid_t pid = fork();
if (pid < 0) {
fprintf(stderr, "Fork failed\n");
return 1;
} else if (pid == 0) {
// Child process
int words = count_words(sentence);
printf("Child process: Number of words = %d\n", words);
} else {
// Parent process
int status;
wait(&status);
if (WIFEXITED(status)) {
int vowels = count_vowels(sentence);
printf("Parent process: Number of vowels = %d\n", vowels);
}
}
return 0;
}
2)
Client.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
while (1)
{
int a, b;
char op;
printf("Enter two integers and an operation (+ or -): ");
scanf("%d %d %c", &a, &b, &op);
pid_t pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0)
{
char arg1[10], arg2[10], arg3[2];
sprintf(arg1, "%d", a);
sprintf(arg2, "%d", b);
sprintf(arg3, "%c", op);
execl("./server", "server", arg1, arg2, arg3, NULL);
}
else
{
int result_status;
waitpid(pid, &result_status, 0);
printf("The result is: %d\n", WEXITSTATUS(result_status));
}
}
return 0;
}
Server.c
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
char op = argv[3][0];
if (op == '+')
{
exit(a+b);
}
else if (op == '-')
{
exit(a - b);
}
else
{
exit(0);
}
}
result;
}
3)
#include <stdio.h>
void fifo(int pages[], int n, int frames) {
int frame[frames];
int faults = 0;
int front = 0, rear = 0;
for (int i = 0; i < frames; i++) {
frame[i] = -1;
}
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == page) {
found = 1;
break;
}
}
if (found == 0) {
frame[rear] = page;
rear = (rear + 1) % frames;
faults++;
}
printf("Page %d -> ", page);
for (int j = 0; j < frames; j++) {
printf("%d ", frame[j]);
}
printf("\n");
}
printf("FIFO Total Page Faults: %d\n", faults);
}
void lru(int pages[], int n, int frames) {
int frame[frames];
int faults = 0;
int counter = 0;
int used[frames];
for (int i = 0; i < frames; i++) {
frame[i] = -1;
used[i] = 0;
}
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == page) {
found = 1;
used[j] = ++counter;
break;
}
}
if (found == 0) {
int min = used[0];
int replace_index = 0;
for (int j = 1; j < frames; j++) {
if (used[j] < min) {
min = used[j];
replace_index = j;
}
}
frame[replace_index] = page;
used[replace_index] = ++counter;
faults++;
}
printf("Page %d -> ", page);
for (int j = 0; j < frames; j++) {
printf("%d ", frame[j]);
}
printf("\n");
}
printf("LRU Total Page Faults: %d\n", faults);
}
void optimal(int pages[], int n, int frames) {
int frame[frames];
int faults = 0;
for (int i = 0; i < frames; i++) {
frame[i] = -1;
}
for (int i = 0; i < n; i++) {
int page = pages[i];
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == page) {
found = 1;
break;
}
}
if (found == 0) {
int replace_index = -1;
for (int j = 0; j < frames; j++) {
int future = -1;
for (int k = i + 1; k < n; k++) {
if (pages[k] == frame[j]) {
future = k;
break;
}
}
if (future == -1) {
replace_index = j;
break;
}
if (replace_index == -1 || future > replace_index) {
replace_index = j;
}
}
frame[replace_index] = page;
faults++;
}
printf("Page %d -> ", page);
for (int j = 0; j < frames; j++) {
printf("%d ", frame[j]);
}
printf("\n");
}
printf("Optimal Total Page Faults: %d\n", faults);
}
int main() {
int pages[] = {1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3};
int n = sizeof(pages) / sizeof(pages[0]);
int frames;
printf("Enter the number of frames: ");
scanf("%d", &frames);
printf("Page Reference Sequence: ");
for (int i = 0; i < n; i++) {
printf("%d ", pages[i]);
}
printf("\n\n");
printf("FIFO Algorithm:\n");
fifo(pages, n, frames);
printf("\n");
printf("LRU Algorithm:\n");
lru(pages, n, frames);
printf("\n");
printf("Optimal Algorithm:\n");
optimal(pages, n, frames);
printf("\n");
return 0;
}
4)
(x.c)
#include <stdio.h>
#include <unistd.h>
int main() {
printf("Process X is running.\n");
// I/O operations for process X
printf("Process X is waiting for input: ");
char input[100];
fgets(input, sizeof(input), stdin);
printf("Process X received input: %s",
input); printf("Process X is done.\n");
return 0;
}
(a.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
printf("Process A is running.\n");
// Create child process B
pid_t pid = fork();
if (pid == 0)
{
// Child process B
printf("Process B is running.\n");
//
Load and execute
program X execl("./x", "x",
NULL);
//
This line will be reached only if the execution of X
fails printf("Failed to execute program X.\n");
exit(1);
}
else if (pid > 0)
{
// Parent process A
// Wait for child B to finish
wait(NULL);
printf("Process B has finished.\n");
}
else
{
// Fork failed
printf("Failed to create child process B.\n");
exit(1);
}
printf("Process A is done.\n");
return 0;
}
5)
#include <stdio.h>
#define NUM_PARTITIONS 5
#define NUM_PROCESSES 4
void firstFit(int partitions[], int processes[], int allocation[])
{ for (int i = 0; i < NUM_PROCESSES; i++) {
allocation[i] = -1;
for (int j = 0; j < NUM_PARTITIONS; j++) {
if (processes[i] <= partitions[j]) {
allocation[i] = j;
partitions[j] -= processes[i];
break;
}
}
}
}
void bestFit(int partitions[], int processes[], int allocation[]) {
for (int i = 0; i < NUM_PROCESSES; i++) {
allocation[i] = -1;
int bestFitIndex = -1;
for (int j = 0; j < NUM_PARTITIONS; j++) {
if (processes[i] <= partitions[j]) {
if (bestFitIndex == -1 || partitions[j] < partitions[bestFitIndex]) {
bestFitIndex = j;
}
}
}
if (bestFitIndex != -1) {
allocation[i] = bestFitIndex;
partitions[bestFitIndex] -= processes[i];
}
}
}
void worstFit(int partitions[], int processes[], int allocation[])
{ for (int i = 0; i < NUM_PROCESSES; i++) {
allocation[i] = -1;
int worstFitIndex = -1;
for (int j = 0; j < NUM_PARTITIONS; j++) {
if (processes[i] <= partitions[j]) {
if (worstFitIndex == -1 || partitions[j] > partitions[worstFitIndex]) {
worstFitIndex = j;
}
}
}
if (worstFitIndex != -1) {
allocation[i] = worstFitIndex;
partitions[worstFitIndex] -= processes[i];
}
}
}
void printMemoryAllocation(int partitions[], int processes[], int allocation[])
{ printf("Partition Sizes: ");
for (int i = 0; i < NUM_PARTITIONS; i++) {
printf("%dKB ", partitions[i]);
}
printf("\n");
printf("Process Sizes: ");
for (int i = 0; i < NUM_PROCESSES; i++) {
printf("%dKB ", processes[i]);
}
printf("\n");
printf("Memory Allocation:\n");
for (int i = 0; i < NUM_PROCESSES; i++) {
if (allocation[i] == -1) {
printf("Process %d: Not Allocated\n", i + 1);
} else {
printf("Process %d: Allocated to Partition %d\n", i + 1, allocation[i] + 1);
}
}
printf("\n");
}
int main() {
int partitions[] = {100, 500, 200, 300,
600}; int processes[] = {212, 417, 112,
426}; int allocation[NUM_PROCESSES];
printf("First Fit Algorithm:\n");
firstFit(partitions, processes, allocation);
printMemoryAllocation(partitions, processes, allocation);
printf("Best Fit Algorithm:\n");
bestFit(partitions, processes, allocation);
printMemoryAllocation(partitions, processes, allocation);
printf("Worst Fit Algorithm:\n");
worstFit(partitions, processes, allocation);
printMemoryAllocation(partitions, processes, allocation);
return 0;
}
5
5) Kaushal
#include <stdio.h>
void firstFit(int processes[], int m, int partitions[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (partitions[j] >= processes[i]) {
allocation[j] = i;
partitions[j] -= processes[i];
break;
}
}
}
printf("First-Fit Allocation:\n");
printf("Process No.\tProcess Size\tPartition No.\n");
for (int i = 0; i < m; i++) {
printf("%d\t\t%d KB\t\t", i, processes[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
void bestFit(int processes[], int m, int partitions[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < m; i++) {
int bestIdx = -1;
for (int j = 0; j < n; j++) {
if (partitions[j] >= processes[i]) {
if (bestIdx == -1 || partitions[j] < partitions[bestIdx])
bestIdx = j;
}
}
if (bestIdx != -1) {
allocation[bestIdx] = i;
partitions[bestIdx] -= processes[i];
}
}
printf("Best-Fit Allocation:\n");
printf("Process No.\tProcess Size\tPartition No.\n");
for (int i = 0; i < m; i++) {
printf("%d\t\t%d KB\t\t", i, processes[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
void worstFit(int processes[], int m, int partitions[], int n) {
int allocation[n];
for (int i = 0; i < n; i++)
allocation[i] = -1;
for (int i = 0; i < m; i++) {
int worstIdx = -1;
for (int j = 0; j < n; j++) {
if (partitions[j] >= processes[i]) {
if (worstIdx == -1 || partitions[j] > partitions[worstIdx])
worstIdx = j;
}
}
if (worstIdx != -1) {
allocation[worstIdx] = i;
partitions[worstIdx] -= processes[i];
}
}
printf("Worst-Fit Allocation:\n");
printf("Process No.\tProcess Size\tPartition No.\n");
for (int i = 0; i < m; i++) {
printf("%d\t\t%d KB\t\t", i, processes[i]);
if (allocation[i] != -1)
printf("%d\n", allocation[i]);
else
printf("Not Allocated\n");
}
}
int main() {
int partitions[] = {100, 500, 200, 300, 600};
int processes[] = {212, 417, 112, 426};
int n = sizeof(partitions) / sizeof(partitions[0]);
int m = sizeof(processes) / sizeof(processes[0]);
firstFit(processes, m, partitions, n);
printf("\n");
int partitions2[] = {100, 500, 200, 300, 600};
int processes2[] = {212, 417, 112, 426};
bestFit(processes2, m, partitions2, n);
printf("\n");
int partitions3[] = {100, 500, 200, 300, 600};
int processes3[] = {212, 417, 112, 426};
worstFit(processes3, m, partitions3, n);
return 0;
}
6)
#include <stdio.h>
#define MAX_PROCESSES 4
#define QUANTUM_SLICE 2
struct Process {
int executionTime;
int arrivalTime;
int waitingTime;
int remainingTime;
};
void calculateWaitingTime(struct Process processes[], int n) {
int remainingProcesses = n;
int currentTime = 0;
//
Initialize remaining time and waiting time for each
process for (int i = 0; i < n; i++) {
processes[i].remainingTime =
processes[i].executionTime; processes[i].waitingTime =
0; }
while (remainingProcesses > 0) {
for (int i = 0; i < n; i++) {
if (processes[i].remainingTime > 0) {
//
Execute the process for the quantum slice or until it finishes
int executionTime = (processes[i].remainingTime < QUANTUM_SLICE) ?
processes[i].remainingTime : QUANTUM_SLICE;
// Update the remaining time of the process
processes[i].remainingTime -= executionTime;
//
Update the current time and waiting
time currentTime += executionTime;
processes[i].waitingTime += currentTime - processes[i].arrivalTime;
if (processes[i].remainingTime == 0)
remainingProcesses--;
}
}
}
}
void printWaitingTime(struct Process processes[], int n)
{ int totalWaitingTime = 0;
printf("Process\tWaiting Time\n");
printf("--------------------\n");
for (int i = 0; i < n; i++) {
printf("P%d\t\t%d\n", i + 1,
processes[i].waitingTime); totalWaitingTime +=
processes[i].waitingTime; }
printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime / n);
}
int main() {
struct Process processes[MAX_PROCESSES] = {
{20, 0, 0, 0},
{25, 15, 0, 0},
{10, 30, 0, 0},
{15, 45, 0, 0}
};
int numProcesses = sizeof(processes) / sizeof(processes[0]);
calculateWaitingTime(processes, numProcesses);
printWaitingTime(processes, numProcesses);
return 0;
}
Round Robin scheduling algorithm
6. Kaushal
#include <stdio.h>
void findWaitingTime(int processes[], int n, int quantum, int
arrival_time[], int burst_time[], int waiting_time[]) {
int remaining_burst_time[n];
for (int i = 0; i < n; i++)
remaining_burst_time[i] = burst_time[i];
int t = 0;
while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0 && arrival_time[i] <= t) {
done = 0;
if (remaining_burst_time[i] > quantum) {
t += quantum;
remaining_burst_time[i] -= quantum;
}
else {
t = t + remaining_burst_time[i];
waiting_time[i] = t - burst_time[i] - arrival_time[i];
remaining_burst_time[i] = 0;
}
}
}
if (done == 1)
break;
t++;
// Increment current time if no process was executed
}
}
void findTurnAroundTime(int processes[], int n, int burst_time[], int
waiting_time[], int turn_around_time[]) {
for (int i = 0 ; i < n ; i++)
turn_around_time[i] = burst_time[i] + waiting_time[i];
}
void findAverageTime(int processes[], int n, int quantum, int
arrival_time[], int burst_time[]) {
int waiting_time[n], turn_around_time[n];
findWaitingTime(processes, n, quantum, arrival_time, burst_time,
waiting_time);
findTurnAroundTime(processes, n, burst_time, waiting_time,
turn_around_time);
int total_wt = 0, total_tat = 0;
printf("Processes
Arrival Time
Burst Time
Waiting Time
Turn Around Time\n");
for (int i = 0; i < n; i++) {
total_wt = total_wt + waiting_time[i];
total_tat = total_tat + turn_around_time[i];
printf(" %d\t\t %d\t\t %d\t\t %d\t\t\t %d\n", i + 1,
arrival_time[i], burst_time[i], waiting_time[i], turn_around_time[i]);
}
printf("Average waiting time = %f", (float)total_wt / n);
printf("\nAverage turn around time = %f\n", (float)total_tat / n);
}
int main() {
int processes[] = {1, 2, 3, 4};
int n = sizeof processes / sizeof processes[0];
int arrival_time[] = {0, 15, 30, 45};
int burst_time[] = {20, 25, 10, 15};
int quantum = 2;
findAverageTime(processes, n, quantum, arrival_time, burst_time);
return 0;
}
ALGORITHM:
1. Initialize the remaining_time array with the burst time of each process.
2. Set the time and complete variables to 0.
3.
While the complete variable is less than the total number of processes, do the
following: ● 3.1. Iterate through each process.
● 3.1.1. If the remaining_time of the process is greater than 0, do
the following:
● 3.1.1.1. If the remaining_time is greater than the time slice,
subtract the time slice from the remaining_time and increment
the time by the time slice.
● 3.1.1.2. If the remaining_time is less than or equal to the
time slice, add the remaining_time to the time and set the
remaining_time of the process to 0.
● 3.1.1.3. Calculate the waiting time of the process by subtracting
the arrival time and the CPU time from the current time.
● 3.1.1.4. If the waiting time is negative, set it to
0. ● 3.1.1.5. Increment the complete variable.
4. Calculate the turnaround time for each process by adding the CPU time and the waiting time.
5. Calculate the average waiting time and average turnaround time for all the processes.
6. Display the process ID, waiting time, and turnaround time for each process.
7. Display the average waiting time and average turnaround time.
OPERATING SYSTEM MIDTERM
ADITI SRIVASTAVA
21BCB0167
QUESTION 1AIMWrite a program where parent process counts number of vowels in the given sentence and child
process will count number of words in the same sentence. Use FORK and JOIN construct.
ALGORITHMStart the program.
Create a pipe to establish communication between the parent and child processes.
Create a child process using the FORK system call.
In the child process:
a. Close the write end of the pipe.
b. Read the sentence from the read end of the pipe.
c. Count the number of words in the sentence.
d. Send the count of words back to the parent process through the pipe.
e. Close the read end of the pipe.
f. Exit the child process.
In the parent process:
a. Close the read end of the pipe.
b. Read the sentence from the user.
c. Count the number of vowels in the sentence.
d. Wait for the child process to finish.
e. Receive the count of words from the child process through the pipe.
f. Close the write end of the pipe. g. Print the count of vowels and words.
h. End the program.
CODE-
OUTPUT-
Question 2AIM-Write two programs one called client.c, the other called server.c. The client program lists a
prompter and reads from the keyboard two integers and one of the characters '+' or '-'. The read
information is transmitted with the help of the system call execl to a child process, which executes
the server code. After the child (server) process finishes the operation, it transmits the result to
parent process (client) with the help of the system call exit. The client process prints the result on the
screen and also reprints the prompter, ready for a new reading.
Algorithm:
1. Start the client program (client.c):
● Display a prompter to the user.
● Read two integers and an operator ('+' or '-') from the keyboard.
● Store the input values in variables.
● Fork a child process using the fork function.
2. In the child process (server.c):
● Use the execl system call to execute the server code.
● Pass the two integers and the operator as command-line arguments to the server
program.
● Calculate the result of the operation.
● Use the exit system call to transmit the result to the parent process.
3. In the parent process (client.c):
● Wait for the child process to finish using the wait function.
● Retrieve the result transmitted by the child process using the WEXITSTATUS macro. ● Print
the result on the screen.
● Display the prompter again, ready for a new reading.
● Go back to step 1 to repeat the process.
Code:
OUTPUT-
QUESTION 3Consider the following virtual page reference sequence: page 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3.
This indicates that these particular pages need to be accessed by the computer in the order
shown. Consider each of the following algorithm-frame combinations:
LRU, FIFO and Oprimal with 3 and 4 frames Write a C program for implementing the FIFO,
LRU and Optimal Page replacement Algorithms. Check it out for Belady’s Anomaly in any of
the page replacement algorithm.
ALGORITHMStart the program.
Initialize the page reference sequence: pageRefSeq[] = {1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3}.
Initialize the number of frames for each algorithm:
FIFO: numFrames = 3 or 4
LRU: numFrames = 3 or 4
Optimal: numFrames = 3 or 4
Implement the FIFO algorithm:
a. Initialize an empty frame[] array of size numFrames.
b. Initialize a pageFaults counter to 0.
c. Iterate through the page reference sequence:
Check if the current page is already present in any frame.
If yes, continue to the next page.
If no, check if there is an empty frame available.
If yes, allocate the page to the empty frame.
If no, use the FIFO replacement policy to replace the oldest page in a frame with the current page.
Increment the pageFaults counter.
d. Print the total pageFaults for the FIFO algorithm.
Implement the LRU algorithm:
a. Initialize an empty frame[] array of size numFrames
b. Initialize a pageFaults counter to 0.
c. Initialize a useCount[] array of size numFrames, initially set to 0.
d. Iterate through the page reference sequence:
Check if the current page is already present in any frame.
If yes, update the useCount for that frame.
If no, check if there is an empty frame available.
If yes, allocate the page to the empty frame.
If no, use the LRU replacement policy to replace the frame with the lowest useCount with
the current page.
Increment the pageFaults counter.
e. Print the total pageFaults for the LRU algorithm.
Implement the Optimal algorithm:
a. Initialize an empty frame[] array of size numFrames.
b. Initialize a pageFaults counter to 0.
c. Iterate through the page reference sequence:
Check if the current page is already present in any frame.
If yes, continue to the next page.
If no, check if there is an empty frame available.
If yes, allocate the page to the empty frame.
If no, use the Optimal replacement policy to find the frame that will not be used for the
longest time in the future.
Replace that frame with the current page.
Increment the pageFaults counter.
d. Print the total pageFaults for the Optimal algorithm.
Check for Belady's Anomaly by comparing the pageFaults of FIFO and Optimal algorithms for
numFrames = 3 and numFrames = 4.
If the pageFaults increase with more frames, Belady's Anomaly is observed.
CODE-
OUTPUT-
QUESTION 4
AIM-Write a C Program X that create two child process Y and Z and write a program A that
creates child process B such that the child B loads and executes the program X. You should make
each of the process to do some I/O Operations like Waiting for some input or writing something
to the output screen.
Algorithm:
1. Start Program A:
● Fork a child process B.
● In the child process B:
● Load and execute Program X using the exec family of functions.
● In the parent process A: ● Continue with any additional tasks or terminate.
2. Start Program X:
● Fork child processes Y and Z.
● In the parent process X:
● Perform I/O operations such as waiting for input or writing to the output screen.
● Continue with any additional tasks or terminate.
3. Start child process Y:
● Perform I/O operations as required.
● Continue with any additional tasks or terminate.
4. Start child process Z:
● Perform I/O operations as required.
● Continue with any additional tasks or terminate.
CODE-
OUTPUT-
QUESTION 5AIM Given five memory partitions of 100Kb, 500Kb, 200Kb, 300Kb, 600Kb (in order), how would the
first-fit, best-fit, and worst-fit algorithms place processes of 212 Kb, 417 Kb, 112 Kb, and 426 Kb
(in order)? Which algorithm makes the most efficient use of memory? Write a C Program for
computing First fit, Best Fit and Worst Fit.
ALGORITHMStart the program.
Initialize the memory partitions and processes:
Memory partitions: [100Kb, 500Kb, 200Kb, 300Kb, 600Kb]
Processes: [212Kb, 417Kb, 112Kb, 426Kb]
Implement the First Fit algorithm:
a. Create an allocation[] array of the same size as the number of processes.
b. Iterate through each process:
For each process, iterate through the memory partitions from the beginning.
Find the first memory partition that is large enough to accommodate the process.
Update the allocation[] array with the index of the allocated partition.
Reduce the size of the allocated partition by the size of the process.
c. Print the allocation[] array to display the allocated memory partitions for each process.
Implement the Best Fit algorithm:
a. Create an allocation[] array of the same size as the number of processes.
b. Iterate through each process:
For each process, iterate through the memory partitions and find the smallest partition that
can accommodate the process.
Update the allocation[] array with the index of the allocated partition.
Reduce the size of the allocated partition by the size of the process.
c. Print the allocation[] array to display the allocated memory partitions for each process.
Implement the Worst Fit algorithm:
a. Create an allocation[] array of the same size as the number of processes.
b. Iterate through each process:
For each process, iterate through the memory partitions and find the largest partition that
can accommodate the process.
Update the allocation[] array with the index of the allocated partition.
Reduce the size of the allocated partition by the size of the process.
c. Print the allocation[] array to display the allocated memory partitions for each process.
Calculate the total wasted memory for each algorithm:
a. Iterate through the memory partitions and sum up the remaining memory in each
partition.
b. Print the total wasted memory for each algorithm.
Compare the total wasted memory for each algorithm:
The algorithm with the least total wasted memory makes the most efficient use of memory.
End the program.
CODE-
OUTPUT-
QUESTION 6AIMWrite a program to schedule the process to the CPU using Round Robin scheduling algorithm.
Consider the quantum slice as 2 seconds. Compute the waiting time of each process and
average waiting time.
ALGORITHM-
1. Start the program.
2. Define the process details:
• Create a data structure or arrays to store the process details including
execution time and arrival time.
• Initialize the process details:
• Process P1: Execution time = 20, Arrival time = 0
• Process P2: Execution time = 25, Arrival time = 15
• Process P3: Execution time = 10, Arrival time = 30
• Process P4: Execution time = 15, Arrival time = 45
3. Sort the processes based on their arrival time in ascending order.
4. Create a queue to hold the processes.
5. Initialize the time quantum slice as 2 seconds.
6. Set the current time to 0.
7. Initialize the waiting time and turnaround time arrays to store the waiting time
and turnaround time for each process.
8. While there are processes in the queue or the current process is not finished:
a. Check if there are any arriving processes at the current time and enqueue
them.
b. Dequeue the next process from the queue.
c. If the remaining execution time of the process is less than or equal to the time quantum slice:
• Update the waiting time for the current process:
waiting_time[current_process] += current_time arrival_time[current_process]
• Update the current time: current_time += remaining_execution_time
• Update the remaining execution time for the current process:
remaining_execution_time = 0 d. If the remaining execution time of the
process is greater than the time quantum slice:
• Update the waiting time for the current process:
waiting_time[current_process] += current_time arrival_time[current_process]
• Update the current time: current_time += time_quantum
• Update the remaining execution time for the current process:
remaining_execution_time -= time_quantum
• Enqueue the current process back to the queue. e. If the remaining
execution time of the current process is not zero, enqueue it back to
the queue.
9. Calculate the average waiting time:
• Initialize the total_waiting_time as 0.
• Iterate through each process and add its waiting time to the
total_waiting_time.
• Calculate the average_waiting_time as total_waiting_time /
number_of_processes.
10. Print the waiting time for each process and the average waiting time.
11. End the program.
CODE-
OUTPUT-
Download