0% found this document useful (0 votes)
37 views2 pages

Os Chit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Os Chit

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Ques 1 : Write a Program to create a child process Q- Write a Program to show working of execlp() Q-FCFS (First Come First

(First Come First Served)


using system call fork(). system call by executing ls command. 1. Sort processes based on their arrival times.
PROGRAM : PROGRAM : 2. Execute the processes in order of their arrival.
#include <stdio.h> #include <stdio.h> 3. Calculate waiting time and turnaround time.
PROGRAM :
#include <unistd.h> #include <unistd.h>
#include <stdio.h>
int main() int main()
void findWaitingTime(int n, int burst_time[], int
{ pid_t pid = fork(); { execlp("ls", "ls", NULL); arrival_time[], int waiting_time[])
if (pid < 0) printf("execlp failed.\n"); { int service_time[n];
{ printf("Fork failed.\n"); printf("\n"); service_time[0] = arrival_time[0];
} else if (pid == 0) return 0; } waiting_time[0] = 0;
{ printf("This is the child process.\n"); for (int i = 1; i < n; i++)
} else {service_time[i] = service_time[i - 1] +
{ printf("This is the parent process.\n"); Q- Program to read a file and store your details in burst_time[i -1];
} printf("\n"); that file. Your Program should also create one more waiting_time[i] = service_time[i] - arrival_time[i];
return 0; } file and store your friends details in that file. Once if (waiting_time[i] < 0)
waiting_time[i] = 0; }}
created, print lines which match in both files.
void findTurnaroundTime(int n, int burst_time[], int
PROGRAM :
waiting_time[], int turnaround_time[])
Ques 2 : Write a Program to print process Id's of #include <stdio.h> { for (int i = 0; i < n; i++)
parent and child process i.e. parent should print its #include <fcntl.h> { turnaround_time[i] = burst_time[i] + waiting_time[i]; } }
own and its child process id while child process #include <unistd.h> void findAverageTimes(int n, int burst_time[], int
should print its own and its parent process id. (use #include <string.h> arrival_time[])
getpid(), getppid()) int main() {int waiting_time[n], turnaround_time[n];
PROGRAM : { int fd1 = open("my_details.txt", O_WRONLY | int total_waiting_time = 0,
#include <stdio.h> O_CREAT | O_TRUNC, 0644); total_turnaround_time = 0;
#include <unistd.h> int fd2 = open("friend_details.txt", O_WRONLY | findWaitingTime(n, burst_time, arrival_time,
int main() O_CREAT | O_TRUNC, 0644); waiting_time);
findTurnaroundTime(n, burst_time, waiting_time,
{ pid_t pid = fork(); if (fd1 < 0 || fd2 < 0)
turnaround_time);
if (pid < 0) { printf("Failed to open or create files.\n");
printf("\nGantt Chart: ");
{ printf("Fork failed.\n"); return 1; for (int i = 0; i < n; i++)
} else if (pid == 0) } char my_details[] = "Name: Alice\nAge: 20\nCity: { printf("P%d ", i + 1); }
{ printf("Child Process: My PID is %d, My Parent PID is New York\n"; printf("\n");
%d.\n", getpid(), getppid()); char friend_details[] = "Name: Bob\nAge: 21\nCity: printf("\nProcess\tBurst Time\tArrival Time\tWaiting
} else New York\n"; Time\tTurnaround Time\n");
{ printf("Parent Process: My PID is %d, My Child PID is write(fd1, my_details, strlen(my_details)); for (int i = 0; i < n; i++)
%d.\n", getpid(), pid); write(fd2, friend_details, strlen(friend_details)); { total_waiting_time += waiting_time[i];
} printf("\n"); close(fd1); total_turnaround_time += turnaround_time[i];
return 0; } close(fd2); printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1,
burst_time[i], arrival_time[i], waiting_time[i],
fd1 = open("my_details.txt", O_RDONLY);
turnaround_time[i]); }
fd2 = open("friend_details.txt", O_RDONLY);
float avg_waiting_time =
Ques 3(b) : What will happen if parent process dies if (fd1 < 0 || fd2 < 0) (float)total_waiting_time / n;
before child process? Illustrate it by creating one { printf("Failed to open files for reading.\n"); float avg_turnaround_time =
more child of parent process. return 1; } (float)total_turnaround_time / n;
PROGRAM : char buffer1[100], buffer2[100]; printf("\nAverage Waiting Time: %.2f\n",
#include <stdio.h> int bytes_read1 = read(fd1, buffer1, sizeof(buffer1)); avg_waiting_time);
#include <unistd.h> int bytes_read2 = read(fd2, buffer2, sizeof(buffer2)); printf("Average Turnaround Time: %.2f\n",
#include <stdlib.h> buffer1[bytes_read1] = '\0'; avg_turnaround_time);
int main() buffer2[bytes_read2] = '\0'; } int main() { int n;
{ pid_t first_child = fork(); printf("Matching lines:\n"); printf("Enter the number of processes: ");
scanf("%d", &n);
if (first_child < 0) char *line1 = strtok(buffer1, "\n");
int burst_time[n], arrival_time[n];
{ printf("Fork failed.\n"); char *line2 = strtok(buffer2, "\n");
printf("Enter the burst times of the processes: ");
} else if (first_child == 0) { while (line1 != NULL && line2 != NULL) for (int i = 0; i < n; i++)
printf("First Child: My PID is %d, My Parent PID is { if (strcmp(line1, line2) == 0) { scanf("%d", &burst_time[i]); }
%d.\n", getpid(), getppid()); { printf("%s\n", line1); printf("Enter arrival times of the processes: ");
fflush(stdout); } for (int i = 0; i < n; i++)
sleep(3); line1 = strtok(NULL, "\n"); { scanf("%d", &arrival_time[i]); }
printf("First Child (After Parent Death): My Parent line2 = strtok(NULL, "\n"); findAverageTimes(n, burst_time, arrival_time);
PID is %d.\n", getppid()); } return 0; }
fflush(stdout); close(fd1);
exit(0); } close(fd2);
else { printf("\nhello...this os program to check matched
printf("Parent Process: My PID is %d, lines");
terminating.\n", getpid()); return 0;
fflush(stdout);
sleep(1);
exit(0);
} return 0;

Q-Write a Program to open a directory and list its


contents. (use opendir(), readdir(), closedir() )
PROGRAM :
#include <stdio.h>
#include <dirent.h>
int main()
{ DIR *dir = opendir(".");
if (dir == NULL)
{ printf("Could not open current directory.\n");
return 1; }
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{ printf("%s\n", entry->d_name);
} closedir(dir);
printf("\n");
return 0; }
Q-) SJF Non-Preemptive (Shortest Job First Non- Q-SJF Preemptive Q-Round Robin
Preemptive) #include <stdio.h> #include <stdio.h>
PROGRAM : int findShortestJob(int remainingTime[], int n) { #define MAX_PROCESSES 100
#include <stdio.h> int minIndex = -1; void round_robin(int process_ids[], int burst_times[], int n,
#include <stdbool.h> int minTime = 10000; int quantum) {
void findWaitingTime(int n, int burst_time[], int for (int i = 0; i < n; i++) { int remaining_times[MAX_PROCESSES];
arrival_time[], int waiting_time[]) if (remainingTime[i] > 0 && remainingTime[i] < for (int i = 0; i < n; i++) {
{ int completed[n], finish_time[n]; minTime) { remaining_times[i] = burst_times[i];
int current_time = 0; minTime = remainingTime[i]; } int time = 0; int completed = 0;
int total_completed = 0; minIndex = i; }} printf("Process execution order:\n");
for (int i = 0; i < n; i++) return minIndex; } while (completed < n) {
{ completed[i] = 0; } void sjfPreemptive(int processes[], int n, for (int i = 0; i < n; i++) {
while (total_completed < n) int burstTime[]) { if (remaining_times[i] > 0) {
{ int shortest = -1; int remainingTime[10], waitingTime[10], if (remaining_times[i] > quantum) {
int min_burst_time = 10000; turnaroundTime[10]; time += quantum;
for (int i = 0; i < n; i++) int totalWaitingTime = 0, totalTurnaroundTime = 0; remaining_times[i] -= quantum;
{ if (arrival_time[i] <= current_time && for (int i = 0; i < n; i++) { } else {
!completed[i] && burst_time[i] < remainingTime[i] = burstTime[i]; time += remaining_times[i];
min_burst_time) waitingTime[i] = 0; } printf("Process %d completed at time %d\n",
{ min_burst_time = burst_time[i]; int time = 0; int completed = 0; process_ids[i], time);
shortest = i; }} while (completed < n) { remaining_times[i] = 0;
if (shortest != -1) int idx = findShortestJob(remainingTime, n); completed++;
{ current_time += burst_time[shortest]; if (idx != -1) { } } } } } int main() {
finish_time[shortest] = current_time; remainingTime[idx]--; time++; int n, quantum;
waiting_time[shortest] = finish_time[shortest] - if (remainingTime[idx] == 0) { int process_ids[MAX_PROCESSES];
arrival_time[shortest] - burst_time[shortest]; completed++; int burst_times[MAX_PROCESSES];
if (waiting_time[shortest] < 0) turnaroundTime[idx] = time; printf("Enter number of processes: ");
{ waiting_time[shortest] = 0; waitingTime[idx] = turnaroundTime[idx] – burstTime[idx]; scanf("%d", &n);
} completed[shortest] = 1; totalWaitingTime += waitingTime[idx]; for (int i = 0; i < n; i++) {
total_completed++; } totalTurnaroundTime += turnaroundTime[idx]; process_ids[i] = i + 1;
else { current_time++; }}} } } else { time++; } } printf("Enter burst time for process %d: ",
void findTurnaroundTime(int n, int burst_time[], printf("Process\tBurst Time\tWaiting Time\tTurnaround process_ids[i]);
int waiting_time[], int turnaround_time[]) Time\n"); scanf("%d", &burst_times[i]);
{ for (int i = 0; i < n; i++) for (int i = 0; i < n; i++) { } printf("Enter time quantum: ");
{ turnaround_time[i] = burst_time[i] + printf("%d\t%d\t\t%d\t\t%d\n", processes[i], burstTime[i], scanf("%d", &quantum);
waiting_time[i] } } waitingTime[i], turnaroundTime[i]); } round_robin(process_ids, burst_times,
void findAverageTimes(int n, int burst_time[], int printf("\nAverage Waiting Time: %.2f\n", n, quantum);
arrival_time[]) (float)totalWaitingTime / n); return 0; }
{ int waiting_time[n], turnaround_time[n]; printf("Average Turnaround Time: %.2f\n",
int total_waiting_time = 0, (float)totalTurnaroundTime / n); }
total_turnaround_time = 0; int main() {
findWaitingTime(n, burst_time, int processes[10], burstTime[10], n;
arrival_time, waiting_time); printf("Enter number of processes: ");
findTurnaroundTime(n, burst_time, waiting_time, scanf("%d", &n);
turnaround_time); for (int i = 0; i < n; i++) {
printf("\nProcess\tBurst Time\tArrival Time processes[i] = i + 1;
\tWaiting Time\tTurnaround Time\n"); printf("Enter burst time for process %d: ", processes[i]);
for (int i = 0; i < n; i++) scanf("%d", &burstTime[i]);
{ total_waiting_time += waiting_time[i]; } sjfPreemptive(processes, n, burstTime);
total_turnaround_time += turnaround_time[i]; return 0; }
printf("P%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1,
burst_time[i], arrival_time[i], waiting_time[i],
turnaround_time[i]); }
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time =
(float)total_turnaround_time / n;
printf("\nAverage Waiting Time: %.2f\n",
avg_waiting_time);
printf("Average Turnaround Time: %.2f\n",
avg_turnaround_time);
} int main() { int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
int burst_time[n], arrival_time[n];
printf("Enter the burst times of the processes: ");
for (int i = 0; i < n; i++)
{ scanf("%d", &burst_time[i]);
} printf("Enter the arrival times of the processes: ");
for (int i = 0; i < n; i++)
{ scanf("%d", &arrival_time[i]); }
findAverageTimes(n, burst_time, arrival_time);
return 0; }

You might also like