0% found this document useful (0 votes)
4 views17 pages

os1b,2b

The document contains several C programs demonstrating various UNIX system calls and CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority scheduling. Each program includes code snippets along with sample outputs, illustrating how to manage processes and directories in a UNIX environment. The document serves as a practical guide for understanding process management and scheduling in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

os1b,2b

The document contains several C programs demonstrating various UNIX system calls and CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority scheduling. Each program includes code snippets along with sample outputs, illustrating how to manage processes and directories in a UNIX environment. The document serves as a practical guide for understanding process management and scheduling in operating systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Exp No: Page No:

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 {

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

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);

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

}
while(dptr=readdir(drip)) {
printf("%d\n",dptr->d_name);
}
closedir(drip);
}
Output:
Enter the directory name: music
7476480
7476480
7476480

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

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

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

completion_time[i] = completion_time[i - 1] + p[i].burst_time;


}
for (int i = 0; i < n; i++) {
p[i].turnaround_time = completion_time[i] - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
}
printf("\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time,
p[i].burst_time, 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 and burst time for process %d: ", i + 1);
scanf("%d %d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i + 1;
}
calculateTimes(p, n);
return 0;
}

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

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;

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:
int min_bt = 1e9;

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

for (int i = 0; i < n; i++) {


if (p[i].arrival_time <= time && p[i].completed == 0)
{ if (p[i].burst_time < min_bt) {
min_bt = p[i].burst_time;
shortest_job = i;
}
}
}
if (shortest_job == -1) {
time++;
continue;
}
time += p[shortest_job].burst_time;
p[shortest_job].completed = 1;
p[shortest_job].turnaround_time = time - p[shortest_job].arrival_time;
p[shortest_job].waiting_time = p[shortest_job].turnaround_time -
p[shortest_job].burst_time;
total_wt += p[shortest_job].waiting_time;
total_tat += p[shortest_job].turnaround_time;
completed_processes++;
}
printf("\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time,
p[i].burst_time, 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() {

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

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 {

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:
int pid; // Process ID

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

int burst_time; // Burst time


int remaining_time; // Remaining burst time
int waiting_time;
int turnaround_time;
int arrival_time; // Arrival time
};
void calculateTimes(struct Process p[], int n, int quantum)
{ int time = 0; // Current time
int completed = 0;
while (completed < n) {
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= time && p[i].remaining_time > 0) {
if (p[i].remaining_time > quantum) {
time += quantum;
p[i].remaining_time -= quantum;
} else {
time += p[i].remaining_time;
p[i].waiting_time = time - p[i].burst_time - p[i].arrival_time;
p[i].turnaround_time = time - p[i].arrival_time;
p[i].remaining_time = 0;
completed++;
}
}
}
}
printf("\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time,
p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

float total_wt = 0, total_tat = 0;


for (int i = 0; i < n; i++) {
total_wt += p[i].waiting_time;
total_tat += p[i].turnaround_time;
}
printf("\nAverage Waiting Time: %.2f\n", total_wt / n);
printf("Average Turnaround Time: %.2f\n", total_tat / n);
}
int main() {
int n, quantum;
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].remaining_time = p[i].burst_time;
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
calculateTimes(p, n, quantum);
return 0;
}
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 4 Enter arrival time and burst time for process
3: 2 2 Enter arrival time and burst time for

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:
process 4: 3 1

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

Enter time quantum: 2


Process Arrival Burst Waiting Turnaround
1 0 5 7 12
2 1 4 6 10
3 2 2 2 4
4 3 1 3 4
Average Waiting Time: 4.50
Average Turnaround Time: 7.50
D).Priority Scheduling Algorithm:
Program:
#include <stdio.h>
struct Process {
int pid; // Process ID
int burst_time; // Burst time
int priority; // Priority
int waiting_time;
int turnaround_time;
int arrival_time; // Arrival time
};
void calculateTimes(struct Process p[], int n) {
int completed = 0, time = 0;
int total_wt = 0, total_tat = 0;
while (completed < n) {
int idx = -1;
int highest_priority = 1e9; // Large number for comparison
for (int i = 0; i < n; i++) {
if (p[i].arrival_time <= time && p[i].waiting_time == -1)
{ if (p[i].priority < highest_priority) {
highest_priority = p[i].priority;
idx = i;

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

}
}
}
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);

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:
scanf("%d %d %d", &p[i].arrival_time, &p[i].burst_time, &p[i].priority);

ADITYA ENGINEERING COLLEGE(A) Roll No:


Exp No: Page No:
Date:

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

ADITYA ENGINEERING COLLEGE(A) Roll No:

You might also like