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

CPU Scheduling Algorithms in C

The document contains a C program that simulates various CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority Scheduling. Each algorithm is implemented in its own function, allowing users to select which algorithm to run through a menu interface. The program calculates and displays the burst time, waiting time, and turnaround time for each process based on the selected scheduling method.

Uploaded by

sudhatrisarma
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 views7 pages

CPU Scheduling Algorithms in C

The document contains a C program that simulates various CPU scheduling algorithms including FCFS, SJF, Round Robin, and Priority Scheduling. Each algorithm is implemented in its own function, allowing users to select which algorithm to run through a menu interface. The program calculates and displays the burst time, waiting time, and turnaround time for each process based on the selected scheduling method.

Uploaded by

sudhatrisarma
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

1.

Write C programs to simulate the following CPU Scheduling algorithms a) FCFS b) SJF c) Round
Robin d) priority

Programe:

#include <stdio.h>

/* Function Declarations */

void fcfs();

void sjf();

void roundRobin();

void priority();

int main() {

int choice;

while (1) {

printf("\n==============================");

printf("\n CPU SCHEDULING ALGORITHMS");

printf("\n==============================");

printf("\n1. FCFS");

printf("\n2. SJF (Non-Preemptive)");

printf("\n3. Round Robin");

printf("\n4. Priority Scheduling");

printf("\n5. Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);
switch (choice) {

case 1: fcfs(); break;

case 2: sjf(); break;

case 3: roundRobin(); break;

case 4: priority(); break;

case 5: return 0;

default: printf("\nInvalid choice! Try again.\n");

/* FCFS Scheduling */

void fcfs() {

int n, i;

int bt[20], wt[20], tat[20];

printf("\nEnter number of processes: ");

scanf("%d", &n);

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

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

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

wt[0] = 0;
for (i = 1; i < n; i++)

wt[i] = wt[i - 1] + bt[i - 1];

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

tat[i] = wt[i] + bt[i];

printf("\nProcess\tBT\tWT\tTAT\n");

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

printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);

/* SJF Scheduling (Non-Preemptive) */

void sjf() {

int n, i, j, temp;

int bt[20], wt[20], tat[20];

printf("\nEnter number of processes: ");

scanf("%d", &n);

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

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

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

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

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


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

temp = bt[i];

bt[i] = bt[j];

bt[j] = temp;

wt[0] = 0;

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

wt[i] = wt[i - 1] + bt[i - 1];

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

tat[i] = wt[i] + bt[i];

printf("\nProcess\tBT\tWT\tTAT\n");

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

printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);

/* Round Robin Scheduling */

void roundRobin() {

int n, i, tq, time = 0, done;

int bt[20], rt[20], wt[20] = {0}, tat[20];


printf("\nEnter number of processes: ");

scanf("%d", &n);

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

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

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

rt[i] = bt[i];

printf("Enter time quantum: ");

scanf("%d", &tq);

do {

done = 1;

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

if (rt[i] > 0) {

done = 0;

if (rt[i] > tq) {

time += tq;

rt[i] -= tq;

} else {

time += rt[i];

wt[i] = time - bt[i];

rt[i] = 0;

}
}

} while (!done);

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

tat[i] = bt[i] + wt[i];

printf("\nProcess\tBT\tWT\tTAT\n");

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

printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);

/* Priority Scheduling (Non-Preemptive) */

void priority() {

int n, i, j, temp;

int bt[20], pr[20], wt[20], tat[20];

printf("\nEnter number of processes: ");

scanf("%d", &n);

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

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

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

printf("Enter priorities (lower number = higher priority):\n");


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

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

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

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

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

temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;

temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;

wt[0] = 0;

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

wt[i] = wt[i - 1] + bt[i - 1];

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

tat[i] = wt[i] + bt[i];

printf("\nProcess\tBT\tPR\tWT\tTAT\n");

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

printf("P%d\t%d\t%d\t%d\t%d\n", i + 1, bt[i], pr[i], wt[i], tat[i]);

You might also like