OneCompiler

OS11

103

#include <stdio.h>
#include <unistd.h>
int main() {
pid_t child_pid = fork();
if (child_pid == 0) {
printf("Child Process ID: %d\n", getpid());
printf("Hello World\n");
} else if (child_pid > 0) {
printf("Parent Process ID: %d\n", getpid());
printf("Hi\n");
} else {
perror("Fork failed");
return 1;}
return 0;}

#2
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of memory frames: ");
scanf("%d", &n);
int referenceString[] = {0, 2, 1, 6, 4, 0, 1, 0, 3, 1, 2, 1};
int referenceStringLength = sizeof(referenceString) / sizeof(referenceString[0]);
int memory[n];
int pageFaults = 0;
int nextToReplace = 0;
for (int i = 0; i < n; i++) {
memory[i] = -1;}
printf("Page Scheduling using FIFO:\n");
for (int i = 0; i < referenceStringLength; i++) {
int page = referenceString[i];
int found = 0;
for (int j = 0; j < n; j++) {
if (memory[j] == page) {
found = 1;
break;}}
if (!found) {
pageFaults++;
if (memory[nextToReplace] != -1) {
printf("Page %d replaced by Page %d\n", memory[nextToReplace], page);}
memory[nextToReplace] = page;
nextToReplace = (nextToReplace + 1) % n;
printf("Page %d -> Page Fault\n", page);}}
printf("Total Page Faults: %d\n", pageFaults);
return 0;