OneCompiler

OS19

175

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t child_pid = fork();
if (child_pid < 0) {
perror("Fork failed");
exit(1);}
if (child_pid == 0) {
execl("/bin/ls", "ls", NULL);
perror("Execl failed");
exit(1);
} else {
printf("Parent process is going to sleep for 3 seconds...\n");
sleep(3);
wait(NULL);
printf("Parent process has woken up.\n");}
return 0;

#2

#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
int priority;};
void sort_by_priority(struct Process processes[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (processes[i].priority > processes[j].priority) {
struct Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;}}}}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
int total_turnaround_time = 0;
int total_waiting_time = 0;
for (int i = 0; i < n; i++) {
processes[i].pid = i + 1;
printf("Enter arrival time for Process %d: ", i + 1);
scanf("%d", &processes[i].arrival_time);
printf("Enter burst time for Process %d: ", i + 1);
scanf("%d", &processes[i].burst_time);
printf("Enter priority for Process %d: ", i + 1);
scanf("%d", &processes[i].priority);}
sort_by_priority(processes, n);
printf("\nGantt Chart:\n");
int current_time = 0;
for (int i = 0; i < n; i++) {
printf("P%d -> ", processes[i].pid);
total_waiting_time += current_time - processes[i].arrival_time;
current_time += processes[i].burst_time;
total_turnaround_time += current_time - processes[i].arrival_time;}
printf("\n\nAverage Turnaround Time: %.2f", (float)total_turnaround_time / n);
printf("\nAverage Waiting Time: %.2f\n", (float)total_waiting_time / n);
return 0;