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