0% found this document useful (0 votes)
5 views

Process Scheduling

The document outlines the implementation of various process scheduling algorithms in C, including First-Come-First-Serve (FCFS), Shortest Job First (SJF), Round Robin, Priority Scheduling, and Priority Scheduling with Round Robin. Each algorithm is accompanied by code snippets, example outputs, and calculations for average waiting and turnaround times. The document serves as a comprehensive guide for understanding and implementing these scheduling techniques.

Uploaded by

bodarukmini2
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)
5 views

Process Scheduling

The document outlines the implementation of various process scheduling algorithms in C, including First-Come-First-Serve (FCFS), Shortest Job First (SJF), Round Robin, Priority Scheduling, and Priority Scheduling with Round Robin. Each algorithm is accompanied by code snippets, example outputs, and calculations for average waiting and turnaround times. The document serves as a comprehensive guide for understanding and implementing these scheduling techniques.

Uploaded by

bodarukmini2
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/ 14

Assignment

Implement a Process Scheduler.

FirstComeFirstServe :
#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

void fcfs(int n, int burst[], int process[]) {

int wait[n], turnAround[n], completion[n];

float avgWait = 0, avgTurnAround = 0;

// Calculate Completion Time

completion[0] = burst[0];

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

completion[i] = completion[i - 1] + burst[i];

// Calculate Turn Around Time and Waiting Time

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

turnAround[i] = completion[i];

wait[i] = turnAround[i] - burst[i];

avgWait += wait[i];

avgTurnAround += turnAround[i];

// Output Table

printf("\nFCFS Scheduling:\n");

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");

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

printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", process[i], burst[i], wait[i], turnAround[i],


completion[i]);
}

printf("Average Waiting Time: %.2f\n", avgWait / n);

printf("Average Turnaround Time: %.2f\n", avgTurnAround / n);

int main() {

int n, quantum;

printf("Enter number of processes: ");

scanf("%d", &n);

int burst[n], process[n];

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

printf("Enter burst time for process P%d: ", i + 1);

scanf("%d", &burst[i]);

process[i] = i + 1;

// FCFS

fcfs(n, burst, process);

return 0;

Output :-
Enter number of processes: 3

Enter burst time for process P1: 24

Enter burst time for process P2: 3

Enter burst time for process P3: 4

FCFS Scheduling:

Process Burst Time Waiting Time Turnaround Time Completion Time


P1 24 0 24 24
P2 3 24 27 27
P3 4 27 31 32
Average Waiting Time: 17.00

Average Turnaround Time: 27.33

Shortest Job First :


#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

void sjf(int n, int burst[], int process[]) {

int wait[n], turnAround[n], completion[n], burstCopy[n], order[n];

float avgWait = 0, avgTurnAround = 0;

// Copy burst times and process numbers

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

burstCopy[i] = burst[i];

order[i] = i;

// Sort by burst time

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

for (int j = i + 1; j < n; j++) {

if (burstCopy[i] > burstCopy[j]) {

// Swap burst times

int temp = burstCopy[i];

burstCopy[i] = burstCopy[j];

burstCopy[j] = temp;

// Swap process order

temp = order[i];

order[i] = order[j];

order[j] = temp;
}

// Calculate Completion Time

completion[0] = burstCopy[0];

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

completion[i] = completion[i - 1] + burstCopy[i];

// Calculate Turn Around Time and Waiting Time

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

turnAround[i] = completion[i];

wait[i] = turnAround[i] - burstCopy[i];

avgWait += wait[i];

avgTurnAround += turnAround[i];

// Output Table

printf("\nSJF Scheduling:\n");

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");

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

printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", process[order[i]], burstCopy[i], wait[i], turnAround[i],


completion[i]);

printf("Average Waiting Time: %.2f\n", avgWait / n);

printf("Average Turnaround Time: %.2f\n", avgTurnAround / n);

int main() {

int n, quantum;

printf("Enter number of processes: ");

scanf("%d", &n);

int burst[n], process[n];


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

printf("Enter burst time for process P%d: ", i + 1);

scanf("%d", &burst[i]);

process[i] = i + 1;

// SJF

sjf(n, burst, process);

return 0;

Output :
Enter number of processes: 3

Enter burst time for process P1: 24

Enter burst time for process P2: 3

Enter burst time for process P3: 3

SJF Scheduling:

Process Burst Time Waiting Time Turnaround Time Completion Time


P2 3 0 3 3
P3 3 3 6 6
P1 24 6 30 30

Average Waiting Time: 3.00

Average Turnaround Time: 13.00

Round Robin :
#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

void roundRobin(int n, int burst[], int process[], int quantum) {

int remaining[n], wait[n], turnAround[n], completion[n];

float avgWait = 0, avgTurnAround = 0;


int time = 0, done = 0;

// Initialize remaining burst times

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

remaining[i] = burst[i];

// Round Robin execution

while (done < n) {

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

if (remaining[i] > 0) {

if (remaining[i] <= quantum) {

time += remaining[i];

remaining[i] = 0;

completion[i] = time;

done++;

} else {

time += quantum;

remaining[i] -= quantum;

// Calculate Turn Around Time and Waiting Time

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

turnAround[i] = completion[i];

wait[i] = turnAround[i] - burst[i];

avgWait += wait[i];

avgTurnAround += turnAround[i];

}
// Output Table

printf("\nRound Robin Scheduling (Quantum: %d):\n", quantum);

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");

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

printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", process[i], burst[i], wait[i], turnAround[i],


completion[i]);

printf("Average Waiting Time: %.2f\n", avgWait / n);

printf("Average Turnaround Time: %.2f\n", avgTurnAround / n);

int main() {

int n, quantum;

printf("Enter number of processes: ");

scanf("%d", &n);

int burst[n], process[n];

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

printf("Enter burst time for process P%d: ", i + 1);

scanf("%d", &burst[i]);

process[i] = i + 1;

// Round Robin

printf("\nEnter time quantum for Round Robin: ");

scanf("%d", &quantum);

roundRobin(n, burst, process, quantum);

return 0;

Output :
Enter number of processes: 3
Enter burst time for process P1: 24

Enter burst time for process P2: 3

Enter burst time for process P3: 3

Enter time quantum for Round Robin: 4

Round Robin Scheduling (Quantum: 4):

Process Burst Time Waiting Time Turnaround Time Completion Time


P1 24 6 30 30
P2 3 4 7 7
P3 3 7 10 10

Average Waiting Time : 5.67

Average Turnaround Time : 15.67

Priority Scheduling(Non-Preemptive) :
#include <stdio.h>

void priorityScheduling(int n, int burst[], int priority[]) {

int waitingTime[n], turnaroundTime[n], totalWT = 0, totalTAT = 0;

int p[n];

// Initialize process numbers

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

p[i] = i + 1;

// Sort processes by priority (lower value = higher priority)

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

for (int j = i + 1; j < n; j++) {

if (priority[i] > priority[j]) {

// Swap priority, burst time, and process number


int temp = priority[i]; priority[i] = priority[j]; priority[j] = temp;

temp = burst[i]; burst[i] = burst[j]; burst[j] = temp;

temp = p[i]; p[i] = p[j]; p[j] = temp;

// Calculate Waiting Time and Turnaround Time

waitingTime[0] = 0; // First process has no waiting time

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

waitingTime[i] = waitingTime[i - 1] + burst[i - 1];

totalWT += waitingTime[i];

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

turnaroundTime[i] = waitingTime[i] + burst[i];

totalTAT += turnaroundTime[i];

// Print results

printf("Process\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i], priority[i], burst[i], waitingTime[i],


turnaroundTime[i]);

printf("\nAverage Waiting Time: %.2f", (float)totalWT / n);

printf("\nAverage Turnaround Time: %.2f\n", (float)totalTAT / n);

int main() {

int n;

printf("Enter number of processes: ");

scanf("%d", &n);

int burst[n], priority[n];


printf("Enter burst times and priorities:\n");

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

printf("P%d Burst Time: ", i + 1);

scanf("%d", &burst[i]);

printf("P%d Priority: ", i + 1);

scanf("%d", &priority[i]);

priorityScheduling(n, burst, priority);

return 0;

Output :
Enter number of processes: 5

Enter burst times and priorities:

P1 Burst Time: 10

P1 Priority: 3

P2 Burst Time: 1

P2 Priority: 1

P3 Burst Time: 2

P3 Priority: 4

P4 Burst Time: 1

P4 Priority: 5

P5 Burst Time: 5

P5 Priority: 2

Priority Scheduling :

Process Priority Burst Time Waiting Time Turnaround Time


P2 1 1 0 1
P5 2 5 1 6
P1 3 10 6 16
P3 4 2 16 18
P4 5 1 18 19

Average Waiting Time : 8.20

Average Turnaround Time : 12.00


Priority Scheduling with Round Robin :
#include <stdio.h>

struct Process {
int id, burst, remaining, priority, arrival, waiting, turnaround, completed;
};

void priorityRoundRobin(struct Process p[], int n, int timeQuantum) {


int currentTime = 0, completed = 0;
float totalWT = 0, totalTAT = 0;

while (completed < n) {


int idle = 1;

for (int priority = 1; priority <= n; priority++) { // Priority-based selection


for (int i = 0; i < n; i++) {
if (p[i].priority == priority && p[i].remaining > 0 && p[i].arrival <= currentTime) {
idle = 0;
int timeSlice = (p[i].remaining < timeQuantum) ? p[i].remaining : timeQuantum;
p[i].remaining -= timeSlice;
currentTime += timeSlice;

if (p[i].remaining == 0) {
p[i].completed = 1;
p[i].turnaround = currentTime - p[i].arrival;
p[i].waiting = p[i].turnaround - p[i].burst;
totalWT += p[i].waiting;
totalTAT += p[i].turnaround;
completed++;
}
}
}
}

if (idle) currentTime++; // Increment time for idle periods


}
// Output results
printf("\nProcess\tPriority\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", p[i].id, p[i].priority, p[i].burst, p[i].arrival,
p[i].waiting, p[i].turnaround);
}

printf("\nAverage Waiting Time: %.2f", totalWT / n);


printf("\nAverage Turnaround Time: %.2f\n", totalTAT / n);
}
int main() {
int n, timeQuantum;

printf("Enter number of processes: ");


scanf("%d", &n);

struct Process p[n];

printf("Enter time quantum: ");


scanf("%d", &timeQuantum);

printf("Enter burst times, priorities, and arrival times:\n");


for (int i = 0; i < n; i++) {
p[i].id = i + 1;
printf("P%d Burst Time: ", i + 1);
scanf("%d", &p[i].burst);
printf("P%d Priority (lower = higher priority): ", i + 1);
scanf("%d", &p[i].priority);
printf("P%d Arrival Time: ", i + 1);
scanf("%d", &p[i].arrival);
p[i].remaining = p[i].burst;
p[i].completed = 0;
}

priorityRoundRobin(p, n, timeQuantum);
return 0;
}

Output :
Enter number of processes: 5

Enter time quantum: 2

Enter burst times, priorities, and arrival times:

P1 Burst Time: 4

P1 Priority (lower = higher priority): 3

P1 Arrival Time: 2

P2 Burst Time: 5

P2 Priority (lower = higher priority): 2

P2 Arrival Time: 1

P3 Burst Time: 8

P3 Priority (lower = higher priority): 2

P3 Arrival Time: 4

P4 Burst Time: 7

P4 Priority (lower = higher priority): 1

P4 Arrival Time: 5

P5 Burst Time: 3

P5 Priority (lower = higher priority): 3

P5 Arrival Time: 3

Priority Scheduling :
Process Priority Burst Time Arrival Time Waiting Time Turnaround
Time
P1 3 4 2 9 13
P2 2 5 1 13 18
P3 2 8 4 16 27
P4 1 7 5 14 21
P5 3 3 3 10 13

Average Waiting Time: 12.40

Average Turnaround Time: 17.80

You might also like