0% found this document useful (0 votes)
9 views8 pages

Scheduling Algorithms in C

The document contains implementations of four different CPU scheduling algorithms in C: Round Robin, First-Come-First-Serve (FCFS), Shortest Job First (SJF), and Priority Scheduling. Each algorithm is defined in a separate section, detailing the structure of the process, the scheduling logic, and the calculation of waiting and turnaround times. The main function for each algorithm initializes process data and calls the respective scheduling function to execute the algorithm.

Uploaded by

mekashfetene89
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)
9 views8 pages

Scheduling Algorithms in C

The document contains implementations of four different CPU scheduling algorithms in C: Round Robin, First-Come-First-Serve (FCFS), Shortest Job First (SJF), and Priority Scheduling. Each algorithm is defined in a separate section, detailing the structure of the process, the scheduling logic, and the calculation of waiting and turnaround times. The main function for each algorithm initializes process data and calls the respective scheduling function to execute the algorithm.

Uploaded by

mekashfetene89
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

1.

Round Robin Scheduling Algorithm


c

#include <stdio.h>

struct Process {

int pid; // Process ID

int burst; // Burst time

int remaining; // Remaining time};

void roundRobin(struct Process processes[], int n, int quantum) {

int time = 0;

int completed = 0;

int waitingTime[n], turnaroundTime[n];

// Initialize waiting time array

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

waitingTime[i] = 0;

processes[i].remaining = processes[i].burst;

printf("\nRound Robin Scheduling:\n");

printf("Time\tProcess\n");

while(completed < n) {

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

if(processes[i].remaining > 0) {

printf("%d\tP%d\n", time, processes[i].pid);

if(processes[i].remaining > quantum) {

time += quantum;

processes[i].remaining -= quantum;

} else {

time += processes[i].remaining;
waitingTime[i] = time - processes[i].burst;

processes[i].remaining = 0;

completed++;

// Calculate turnaround time and display results

printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

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

printf("P%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst,

waitingTime[i], turnaroundTime[i]);

}}

int main() {

int n = 3;

int quantum = 4;

struct Process processes[] = {

{1, 24, 0},

{2, 3, 0},

{3, 3, 0}

};

roundRobin(processes, n, quantum);

return 0;}

2. FCFS (First-Come-First-Serve) Scheduling Algorithm


c

#include <stdio.h>

struct Process {
int pid; // Process ID

int burst; // Burst time

int arrival; // Arrival time};

void fcfs(struct Process processes[], int n) {

int waitingTime[n], turnaroundTime[n];

int totalTime = 0;

// Sort processes by arrival time (FCFS order)

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

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

if(processes[j].arrival > processes[j+1].arrival) {

struct Process temp = processes[j];

processes[j] = processes[j+1];

processes[j+1] = temp;

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

printf("Time\tProcess\n");

// Calculate waiting time and turnaround time

waitingTime[0] = 0;

totalTime = processes[0].arrival;

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

printf("%d\tP%d\n", totalTime, processes[i].pid);

if(i > 0) {

waitingTime[i] = totalTime - processes[i].arrival;

}
totalTime += processes[i].burst;

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

// Display results

printf("\nProcess\tArrival\tBurst\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].arrival,

processes[i].burst, waitingTime[i], turnaroundTime[i]);

}}

int main() {

int n = 3;

struct Process processes[] = {

{1, 24, 0},

{2, 3, 1},

{3, 3, 2}

};

fcfs(processes, n);

return 0;}

3. SJF (Shortest Job First) Scheduling Algorithm


c

#include <stdio.h>

struct Process {

int pid; // Process ID

int burst; // Burst time};

void sjf(struct Process processes[], int n) {

int waitingTime[n], turnaroundTime[n];

int completed[n];
// Initialize completed array

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

completed[i] = 0;

waitingTime[i] = 0;

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

printf("Time\tProcess\n");

int totalTime = 0;

int processesCompleted = 0;

while(processesCompleted < n) {

int shortest = -1;

int minBurst = 9999;

// Find process with shortest burst time that hasn't completed

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

if(!completed[i] && processes[i].burst < minBurst) {

minBurst = processes[i].burst;

shortest = i;

if(shortest == -1) break;

printf("%d\tP%d\n", totalTime, processes[shortest].pid);

waitingTime[shortest] = totalTime;

totalTime += processes[shortest].burst;
turnaroundTime[shortest] = totalTime;

completed[shortest] = 1;

processesCompleted++;

// Display results

printf("\nProcess\tBurst\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t%d\n", processes[i].pid, processes[i].burst,

waitingTime[i], turnaroundTime[i]);

}}

int main() {

int n = 4;

struct Process processes[] = {

{1, 6},

{2, 8},

{3, 7},

{4, 3}

};

sjf(processes, n);

return 0;}

4. Priority Scheduling Algorithm


c

#include <stdio.h>

struct Process {

int pid; // Process ID

int burst; // Burst time

int priority; // Priority (lower number = higher priority)};

void priorityScheduling(struct Process processes[], int n) {


int waitingTime[n], turnaroundTime[n];

int completed[n];

// Initialize arrays

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

completed[i] = 0;

waitingTime[i] = 0;

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

printf("Time\tProcess\n");

int totalTime = 0;

int processesCompleted = 0;

while(processesCompleted < n) {

int highestPriority = -1;

int minPriority = 9999;

// Find process with highest priority (lowest number) that hasn't completed

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

if(!completed[i] && processes[i].priority < minPriority) {

minPriority = processes[i].priority;

highestPriority = i;

if(highestPriority == -1) break;

printf("%d\tP%d\n", totalTime, processes[highestPriority].pid);


waitingTime[highestPriority] = totalTime;

totalTime += processes[highestPriority].burst;

turnaroundTime[highestPriority] = totalTime;

completed[highestPriority] = 1;

processesCompleted++;

// Display results

printf("\nProcess\tBurst\tPriority\tWaiting\tTurnaround\n");

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

printf("P%d\t%d\t%d\t\t%d\t%d\n", processes[i].pid, processes[i].burst,

processes[i].priority, waitingTime[i], turnaroundTime[i]);

}}

int main() {

int n = 4;

struct Process processes[] = {

{1, 10, 3},

{2, 1, 1},

{3, 2, 4},

{4, 1, 5},

{5, 5, 2}

};

priorityScheduling(processes, n);

return 0;}

You might also like