os1b,2b
os1b,2b
Date:
1b).Write programs using the following UNIX operating system calls fork, exec,
getpid, exit, wait, close, stat, opendir and readdir
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h>
#include<sys/wait.h>
#include<sys/stat.h>
int main() {
pid_t pid;
struct stat file_info;
if(stat("pid1.c",&file_info)==0) {
printf("File 'pid1.c' exists\n");
printf("File size:%Id bytes\n",file_info.st_size);
} else {
perror("stat");
}
pid=fork();
if(pid==-1) {
perror("fork");
exit(1);
}
if(pid==0) {
printf("Child process: Executing 'ls' command\n");
execlp("ls","ls","-1",NULL);
perror("execlp");
exit(1);
}
else {
int status;
wait(&status);
if(WIFEXITED(status)) {
printf("Child process finished sucessfully with status:%d\n",WEXITSTATUS(status));
}
else {
printf("Child process did not terminate sucessfully\n");
}
}
return 0;
}
Output:
stat: No such file or directory
Child process: Executing 'ls' command
execlp: No such file or directory
Child process finished sucessfully with status:1
Program:
#include<stdio.h>
#include<stdlib.h>
#include<dirent.h>
struct dirent *dptr;
int main(int argc,char *argv[])
{ char buff[100];
DIR *drip;
printf("\n Enter the directory name:");
scanf("%s",buff);
if((drip=opendir(buff))==NULL)
{ printf("The given directorydoes not
exists"); exit(1);
}
while(dptr=readdir(drip)) {
printf("%d\n",dptr->d_name);
}
closedir(drip);
}
Output:
Enter the directory name: music
7476480
7476480
7476480
Week-2
2b).Write C programs to simulate the following CPU Scheduling algorithms a)FCFS
b) SJF c)Round Robin d) Priority.
a) First Come First Serve (FCFS) Scheduling
Algorithm Progarm:
#include <stdio.h>
struct Process {
int pid;
int burst_time;
int arrival_time;
int waiting_time;
int turnaround_time;
};
void calculateTimes(struct Process p[], int n) {
int completion_time[n];
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].arrival_time > p[j + 1].arrival_time) {
struct Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
completion_time[0] = p[0].arrival_time + p[0].burst_time;
for (int i = 1; i < n; i++) {
if (completion_time[i - 1] < p[i].arrival_time)
completion_time[i] = p[i].arrival_time + p[i].burst_time;
else
Output:
Enter the number of processes: 4
Enter arrival time and burst time for process 1: 0
5 Enter arrival time and burst time for process 2:
1 3 Enter arrival time and burst time for process
3: 2 8 Enter arrival time and burst time for
process 4: 3 6 Process Arrival Burst Waiting
Turnaround
1 0 5 0 5
2 1 3 4 7
3 2 8 6 14
4 3 6 13 19
Average Waiting Time: 5.75
Average Turnaround Time: 11.25
b) Shortest Job First (SJF) Scheduling
Algorithm Program:
#include <stdio.h>
struct Process {
int pid;
int burst_time;
int arrival_time;
int waiting_time;
int turnaround_time;
int completed; // To track completion status
};
void calculateTimes(struct Process p[], int n) {
int time = 0, completed_processes = 0;
int total_wt = 0, total_tat = 0;
while (completed_processes < n) {
int shortest_job = -1;
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("\nEnter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i + 1;
p[i].completed = 0;
}
calculateTimes(p, n);
return 0;
}
Output:
Enter the number of processes: 4
Enter arrival time and burst time for process 1: 0
6 Enter arrival time and burst time for process 2:
1 8 Enter arrival time and burst time for process
3: 2 7 Enter arrival time and burst time for
process 4: 3 3 Process Arrival Burst Waiting
Turnaround
1 0 6 0 6
2 1 8 15 23
3 2 7 7 14
4 3 3 3 6
Average Waiting Time: 6.25
Average Turnaround Time: 12.25
C) Round Robin Scheduling Algorithm
#include <stdio.h>
struct Process {
}
}
}
if (idx != -1) {
time += p[idx].burst_time;
p[idx].waiting_time = time - p[idx].arrival_time - p[idx].burst_time;
p[idx].turnaround_time = time - p[idx].arrival_time;
total_wt += p[idx].waiting_time;
total_tat += p[idx].turnaround_time;
completed++;
} else {
time++;
}
}
printf("\nProcess\tArrival\tBurst\tPriority\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t\t%d\t%d\n", p[i].pid, p[i].arrival_time,
p[i].burst_time, p[i].priority, p[i].waiting_time,
p[i].turnaround_time);
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_wt / n);
printf("Average Turnaround Time: %.2f\n", (float)total_tat / n);
}
int main()
{ int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process p[n];
for (int i = 0; i < n; i++) {
printf("\nEnter arrival time, burst time, and priority for process %d: ", i + 1);
p[i].pid = i + 1;
p[i].waiting_time = -1; // Indicates that the process has not been executed yet
}
calculateTimes(p, n);
return 0;
}
Output:
Enter the number of processes: 4
Enter arrival time, burst time, and priority for process 1: 0 6 2
Enter arrival time, burst time, and priority for process 2: 1 8 1
Enter arrival time, burst time, and priority for process 3: 2 7 3
Enter arrival time, burst time, and priority for process 4: 3 3
2 Process Arrival Burst Priority Waiting Turnaround
1 0 6 2 0 6
2 1 8 1 5 13
3 2 7 3 15 22
4 3 3 2 11 14
Average Waiting Time: 7.75
Average Turnaround Time: 13.75