OS2
#include <stdio.h>
#include <unistd.h>
int main() {
pid_t child_pid = fork();
if (child_pid == 0) {
printf("Hello World\n");
} else if (child_pid > 0) {
printf("Hi\n");
} else {
perror("Fork failed");
return 1;}
return 0;}
#2
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESSES 10
typedef struct {
int arrival, burst, turnaround, waiting;
} Process;
int cmpArrival(const void *a, const void *b) { return ((Process *)a)->arrival - ((Process *)b)->arrival; }
int main() {
int n; scanf("%d", &n);
Process p[MAX_PROCESSES] = {0};
srand(time(0));
for (int i = 0; i < n; i++) p[i].arrival = i, p[i].burst = rand() % 10 + 1;
qsort(p, n, sizeof(Process), cmpArrival);
printf("Gantt Chart:\n");
float tTAT = 0, tWT = 0, t = 0;
for (int i = 0; i < n; i++) {
printf("P%d ", i + 1);
p[i].turnaround = t + p[i].burst - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
t += p[i].burst + 2;
tTAT += p[i].turnaround;
tWT += p[i].waiting;}
printf("\nProcess\tTurnaround\tWaiting\n");
for (int i = 0; i < n; i++) printf("P%d\t%d\t\t%d\n", i + 1, p[i].turnaround, p[i].waiting);
printf("Avg TAT: %.2f\nAvg WT: %.2f\n", tTAT / n, tWT / n);