0% found this document useful (0 votes)
5 views4 pages

Non-Preemptive CPU Scheduling C Program

The document is an assignment for a C programming task focused on simulating non-preemptive CPU scheduling algorithms: FCFS, SJF, and Priority. It includes code to calculate turnaround and waiting times for processes, with structured input for process details. The main function collects user input for processes and calls the respective scheduling functions to display results.
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)
5 views4 pages

Non-Preemptive CPU Scheduling C Program

The document is an assignment for a C programming task focused on simulating non-preemptive CPU scheduling algorithms: FCFS, SJF, and Priority. It includes code to calculate turnaround and waiting times for processes, with structured input for process details. The main function collects user input for processes and calls the respective scheduling functions to display results.
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

Assignment - 3

OPERATING SYSTEM
TOPIC: Process Scheduling, - PART1
____________________________________________________________
QS. Write a C programme to simulate the following non-preemptive CPU scheduling
algorithms to find the turnaround time and waiting time for the above problem.
a. FCFS
b. SJF
c. Priority

Input :
#include <stdio.h>
struct Process {
int pid;
int burst;
int arrival;
int priority;
int waiting;
int turnaround;
};
void calculateAverage(struct Process p[], int n) {
float totalWT = 0, totalTAT = 0;
for (int i = 0; i < n; i++) {
totalWT += p[i].waiting;
totalTAT += p[i].turnaround;
}
printf("\nAverage Waiting Time: %.2f", totalWT / n);
printf("\nAverage Turnaround Time: %.2f\n", totalTAT / n);
}
void FCFS(struct Process p[], int n) {
printf("\n---- FCFS Scheduling --------------------------------------------\n");
int time = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].arrival > p[j + 1].arrival) {
struct Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
time = p[0].arrival;
for (int i = 0; i < n; i++) {
if (time < p[i].arrival)
time = p[i].arrival;
p[i].waiting = time - p[i].arrival;
time += p[i].burst;
p[i].turnaround = p[i].waiting + p[i].burst;
}
printf("PID\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst, p[i].waiting, p[i].turnaround);
calculateAverage(p, n);
}
void SJF(struct Process p[], int n) {
printf("\n---- SJF Scheduling ------------------------------------------\n");
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].burst > p[j + 1].burst ||
(p[j].burst == p[j + 1].burst && p[j].arrival > p[j + 1].arrival)) {
struct Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
int time = p[0].arrival;
for (int i = 0; i < n; i++) {
if (time < p[i].arrival)
time = p[i].arrival;
p[i].waiting = time - p[i].arrival;
time += p[i].burst;
p[i].turnaround = p[i].waiting + p[i].burst;
}
printf("PID\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst, p[i].waiting, p[i].turnaround);
calculateAverage(p, n);
}
void PriorityScheduling(struct Process p[], int n) {
printf("\n---- Priority Scheduling ------------------------------------------------------------------\n");
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].priority > p[j + 1].priority ||
(p[j].priority == p[j + 1].priority && p[j].arrival > p[j + 1].arrival)) {
struct Process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
int time = p[0].arrival;
for (int i = 0; i < n; i++) {
if (time < p[i].arrival)
time = p[i].arrival;
p[i].waiting = time - p[i].arrival;
time += p[i].burst;
p[i].turnaround = p[i].waiting + p[i].burst;
}
printf("PID\tPriority\tArrival\tBurst\tWaiting\tTurnaround\n");
for (int i = 0; i < n; i++)
printf("P%d\t%d\t\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].priority, p[i].arrival, p[i].burst, p[i].waiting,
p[i].turnaround);
calculateAverage(p, n);
}
int main() {
struct Process p1[10], p2[10], p3[10];
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Process %d Arrival Time: ", i + 1);
scanf("%d", &p1[i].arrival);
printf("Process %d Burst Time: ", i + 1);
scanf("%d", &p1[i].burst);
printf("Process %d Priority: ", i + 1);
scanf("%d", &p1[i].priority);
p1[i].pid = i + 1;
p2[i] = p3[i] = p1[i];
}
FCFS(p1, n);
SJF(p2, n);
PriorityScheduling(p3, n);
return 0;
}
Output :

You might also like