0% found this document useful (0 votes)
11 views14 pages

C Programs for CPU Scheduling Algorithms

The document outlines a series of programming assignments for the Department of Computer Science and Engineering at Comilla University, focusing on various CPU scheduling algorithms implemented in C. Each experiment includes source code for First-Come-First-Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Round Robin, Priority, and Multilevel Queue scheduling, along with sample inputs and outputs. The assignments aim to calculate turnaround time and waiting time for different job scheduling scenarios.

Uploaded by

hero90960
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)
11 views14 pages

C Programs for CPU Scheduling Algorithms

The document outlines a series of programming assignments for the Department of Computer Science and Engineering at Comilla University, focusing on various CPU scheduling algorithms implemented in C. Each experiment includes source code for First-Come-First-Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), Round Robin, Priority, and Multilevel Queue scheduling, along with sample inputs and outputs. The assignments aim to calculate turnaround time and waiting time for different job scheduling scenarios.

Uploaded by

hero90960
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

Comilla University

Department of Computer Science and


Engineering

Set A: Assignment 1 (Scheduling)

Submitted To:
Md. Zahidur Rahman
Lecturer
Dept. of CSE
Comilla University

Submitted By:
Mahtab Uddin Mahi
ID: 12208025
Sesson: 2021-2022

Date of Submission:15/9/25
Experiment No.: 1

Experiment Name: Write a C program to simulate the FCFS CPU scheduling algorithms to
find turnaround time and waiting time for a problem.

Source Code:
#include <stdio.h>

int main() {
int at[10] = {0}, st[10] = {0}, ft[10] = {0}, tat[10] = {0}, wt[10] = {0};
int n, i, j, sum = 0;
float totalTAT = 0, totalWT = 0;

printf("Enter number of Jobs: ");


scanf("%d", &n);

// Input Arrival time and Service Time


for (i = 0; i < n; i++) {
printf("Arrival time of Job[%d]: ", i + 1);
scanf("%d", &at[i]);

printf("Service time of Job[%d]: ", i + 1);


scanf("%d", &st[i]);
printf("\n");
}

// Calculating Finish Time (with idle time handling)


sum = 0;
for (j = 0; j < n; j++) {
if (sum < at[j]) {
sum = at[j]; // CPU was idle, jump to arrival time
}
ft[j] = sum + st[j];
sum = ft[j];
}

// Calculating Turnaround Time (TAT) & Waiting Time (WT)


for (i = 0; i < n; i++) {
tat[i] = ft[i] - at[i];
wt[i] = tat[i] - st[i];
totalTAT += tat[i];
totalWT += wt[i];
}

// Display results
printf("\nSolution (FCFS Scheduling):\n\n");
printf("Job\tArrival Time\tService Time\tFinish Time\tTurn Around Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("J%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, at[i], st[i], ft[i], tat[i], wt[i]);
}

// Averages
printf("\nAverage Turnaround Time = %.2f", totalTAT / n);
printf("\nAverage Waiting Time = %.2f\n", totalWT / n);
return 0;
}

Sample Input and Output:


Enter number of Jobs: 3
Arrival time of Job[1]: 0
Service time of Job[1]: 5

Arrival time of Job[2]: 2


Service time of Job[2]: 3

Arrival time of Job[3]: 10


Service time of Job[3]: 4

Experiment No.: 2

Experiment Name: Write a C program to simulate the SJF CPU scheduling algorithms to find
turnaround time and waiting time for a problem.

Source Code:
#include <stdio.h>
#include <limits.h>

int main() {
int at[10] = {0}, st[10] = {0}, ft[10] = {0}, tat[10] = {0}, wt[10] = {0};
int n, i, j, completed[10] = {0};
int currentTime = 0, completedCount = 0;
float totalTAT = 0, totalWT = 0;

printf("Enter number of Jobs: ");


scanf("%d", &n);

// Input Arrival time and Service Time


for (i = 0; i < n; i++) {
printf("Arrival time of Job[%d]: ", i + 1);
scanf("%d", &at[i]);

printf("Service time of Job[%d]: ", i + 1);


scanf("%d", &st[i]);
printf("\n");
}
// SJF scheduling loop
while (completedCount < n) {
int idx = -1;
int minBT = INT_MAX;

// find the process with smallest burst time that has arrived
for (i = 0; i < n; i++) {
if (!completed[i] && at[i] <= currentTime && st[i] < minBT) {
minBT = st[i];
idx = i;
}
}

if (idx == -1) {
currentTime++; // No process available, CPU idle
} else {
ft[idx] = currentTime + st[idx];
tat[idx] = ft[idx] - at[idx];
wt[idx] = tat[idx] - st[idx];

totalTAT += tat[idx];
totalWT += wt[idx];

currentTime = ft[idx];
completed[idx] = 1;
completedCount++;
}
}

// Display results
printf("\nSolution (SJF Scheduling):\n\n");
printf("Job\tArrival Time\tService Time\tFinish Time\tTurn Around Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("J%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, at[i], st[i], ft[i], tat[i], wt[i]);
}

// Averages
printf("\nAverage Turnaround Time = %.2f", totalTAT / n);
printf("\nAverage Waiting Time = %.2f\n", totalWT / n);

return 0;
}

Sample Input and Output:


Enter number of Jobs: 2
Arrival time of Job[1]: 0
Service time of Job[1]: 6

Arrival time of Job[2]: 2


Service time of Job[2]: 4
Experiment No.: 3

Experiment Name: 3Write a C program to simulate the SRTF CPU scheduling algorithms to
find turnaround time and waiting time for a problem.
Source Code:
#include <stdio.h>
#include <limits.h>

int main() {
int at[10] = {0}, st[10] = {0}, rt[10] = {0}; // rt = remaining time
int ft[10] = {0}, tat[10] = {0}, wt[10] = {0};
int n, i, currentTime = 0, completed = 0, idx = -1;
float totalTAT = 0, totalWT = 0;

printf("Enter number of Jobs: ");


scanf("%d", &n);

// Input Arrival and Service times


for (i = 0; i < n; i++) {
printf("Arrival time of Job[%d]: ", i + 1);
scanf("%d", &at[i]);

printf("Service time of Job[%d]: ", i + 1);


scanf("%d", &st[i]);
rt[i] = st[i]; // initialize remaining time
printf("\n");
}

// Scheduling loop
while (completed < n) {
int minRT = INT_MAX;
idx = -1;

// Find process with smallest remaining time among arrived processes


for (i = 0; i < n; i++) {
if (at[i] <= currentTime && rt[i] > 0 && rt[i] < minRT) {
minRT = rt[i];
idx = i;
}
}

if (idx == -1) {
currentTime++; // no process has arrived yet
} else {
rt[idx]--; // run the process for 1 unit
currentTime++;

if (rt[idx] == 0) {
ft[idx] = currentTime;
tat[idx] = ft[idx] - at[idx];
wt[idx] = tat[idx] - st[idx];
totalTAT += tat[idx];
totalWT += wt[idx];
completed++;
}
}
}

// Output
printf("\nSolution (SRTF Scheduling):\n\n");
printf("Job\tArrival Time\tService Time\tFinish Time\tTurn Around Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("J%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, at[i], st[i], ft[i], tat[i], wt[i]);
}

// Averages
printf("\nAverage Turnaround Time = %.2f", totalTAT / n);
printf("\nAverage Waiting Time = %.2f\n", totalWT / n);

return 0;
}

Sample Input and Output:

Enter number of Jobs: 5


Arrival time of Job[1]: 1
Service time of Job[1]: 8

Arrival time of Job[2]: 2


Service time of Job[2]: 2

Arrival time of Job[3]: 3


Service time of Job[3]: 1

Arrival time of Job[4]: 4


Service time of Job[4]: 2

Arrival time of Job[5]: 5


Service time of Job[5]: 5
Experiment No.: 4

Experiment Name: Write a C program to simulate the Round Robin CPU scheduling algorithms to find
turnaround time and waiting time for a problem.

Source Code:

#include<stdio.h>

typedef struct {
int id, at, bt, rt, pr, ft, tat, wt;
} Job;

int main(){
int n, tq;
printf("Enter number of Job: ");
scanf("%d", &n);

Job jobs[50];

// Input job details


for(int i=0; i<n; i++){
jobs[i].id = i+1;
printf("Arrival time of Job[%d]: ", i+1);
scanf("%d", &jobs[i].at);
printf("Service time of Job[%d]: ", i+1);
scanf("%d", &jobs[i].bt);
printf("Priority of Job[%d] (lower = higher priority): ", i+1);
scanf("%d", &jobs[i].pr);
jobs[i].rt = jobs[i].bt; // Remaining time
printf("\n");
}

printf("Enter Time Quantum: ");


scanf("%d", &tq);

int time = 0, completed = 0;


float totalTAT = 0, totalWT = 0;

// Continue until all jobs are complete


while(completed < n){
int done = 1;

// Pick jobs sorted by priority for this cycle


for(int priority=1; priority<=10; priority++){ // assuming priority range
for(int i=0; i<n; i++){
if(jobs[i].rt > 0 && jobs[i].at <= time && jobs[i].pr == priority){
done = 0;
if(jobs[i].rt > tq){
time += tq;
jobs[i].rt -= tq;
} else {
time += jobs[i].rt;
jobs[i].rt = 0;
jobs[i].ft = time;
jobs[i].tat = jobs[i].ft - jobs[i].at;
jobs[i].wt = jobs[i].tat - jobs[i].bt;
totalTAT += jobs[i].tat;
totalWT += jobs[i].wt;
completed++;
}
}
}
}

// If no process was ready, move time forward


if(done) time++;
}

// Print results
printf("\nSolution:\n\n");
printf("Job\tAT\tBT\tPR\tFT\tTAT\tWT\n\n");
for(int i=0; i<n; i++){
printf("Job%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
jobs[i].id, jobs[i].at, jobs[i].bt, jobs[i].pr,
jobs[i].ft, jobs[i].tat, jobs[i].wt);
}

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


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

return 0;
}

Sample Input and Output:

Enter number of Job: 5


Arrival time of Job[1]: 1
Service time of Job[1]: 8
Priority of Job[1] (lower = higher priority): 2

Arrival time of Job[2]: 1


Service time of Job[2]: 2
Priority of Job[2] (lower = higher priority): 4

Arrival time of Job[3]: 1


Service time of Job[3]: 1
Priority of Job[3] (lower = higher priority): 3

Arrival time of Job[4]: 1


Service time of Job[4]: 2
Priority of Job[4] (lower = higher priority): 4
Arrival time of Job[5]: 1
Service time of Job[5]: 5
Priority of Job[5] (lower = higher priority): 1

Enter Time Quantum: 2

Experiment No.: 5

Experiment Name: Write a C program to simulate the Priority CPU scheduling algorithms to find
turnaround time and waiting time for a problem.

Source Code:
#include <stdio.h>
#include <limits.h>

int main() {
int n;
printf("Enter number of Jobs: ");
scanf("%d", &n);

int at[n], bt[n], pr[n], ct[n], tat[n], wt[n], done[n];


for (int i = 0; i < n; i++) {
printf("Arrival time of Job[%d]: ", i + 1);
scanf("%d", &at[i]);
printf("Service time of Job[%d]: ", i + 1);
scanf("%d", &bt[i]);
printf("Priority of Job[%d] (lower=high): ", i + 1);
scanf("%d", &pr[i]);
done[i] = 0; // Mark job as not completed
}

int time = 0, completed = 0;


float totalTAT = 0, totalWT = 0;

while (completed < n) {


int idx = -1, min_pr = INT_MAX;
// Find the ready job with the highest priority (lowest value)
for (int i = 0; i < n; i++) {
if (!done[i] && at[i] <= time && pr[i] < min_pr) {
min_pr = pr[i];
idx = i;
}
}

if (idx == -1) { // No job ready, move time forward


time++;
continue;
}

// Execute the chosen job


time += bt[idx];
ct[idx] = time;
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];

totalTAT += tat[idx];
totalWT += wt[idx];

done[idx] = 1; // Mark job completed


completed++;
}

// Print job details


printf("\nJob\tAT\tBT\tPR\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
i + 1, at[i], bt[i], pr[i], ct[i], tat[i], wt[i]);
}

// Print averages
printf("\nAverage Turnaround Time = %.2f\n", totalTAT / n);
printf("Average Waiting Time = %.2f\n", totalWT / n);

return 0;
}

Sample Input and Output:

Enter number of Jobs: 5


Arrival time of Job[1]: 0
Service time of Job[1]: 10
Priority of Job[1] (lower=high): 3
Arrival time of Job[2]: 1
Service time of Job[2]: 1
Priority of Job[2] (lower=high): 1
Arrival time of Job[3]: 2
Service time of Job[3]: 2
Priority of Job[3] (lower=high): 3
Arrival time of Job[4]: 3
Service time of Job[4]: 1
Priority of Job[4] (lower=high): 4
Arrival time of Job[5]: 4
Service time of Job[5]: 5
Priority of Job[5] (lower=high): 2

Experiment No.: 6

Experiment Name: Write a C program to simulate the Multilevel Queue scheduling algorithms to find
turnaround time and waiting time for a problem.

Source Code:

#include<stdio.h>

typedef struct {
int id, at, st, pr, ft, tat, wt;
} Job;

void sortByArrival(Job arr[], int n){


for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(arr[j].at < arr[i].at){
Job temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
}
}

void sortByServiceTime(Job arr[], int n){


for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(arr[j].st < arr[i].st){
Job temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
}
}
int main(){
int n;
printf("Enter number of Jobs: ");
scanf("%d",&n);

Job q1[50], q2[50];


int c1=0,c2=0;

// Input jobs
for(int i=0;i<n;i++){
Job j; [Link] = i+1;
printf("Arrival time of Job[%d]: ", i+1); scanf("%d",&[Link]);
printf("Service time of Job[%d]: ", i+1); scanf("%d",&[Link]);
printf("Priority of Job[%d]: ", i+1); scanf("%d",&[Link]);
if([Link] <= 2) q1[c1++] = j; // High priority → FCFS
else q2[c2++] = j; // Low priority → SJF
printf("\n");
}

// Sort queues
sortByArrival(q1, c1); // FCFS by arrival
sortByServiceTime(q2, c2); // SJF by burst time

int time = 0;
float totalTAT = 0, totalWT = 0;

// FCFS for Queue 1


for(int i=0;i<c1;i++){
if(time < q1[i].at) time = q1[i].at;
time += q1[i].st;
q1[i].ft = time;
q1[i].tat = q1[i].ft - q1[i].at;
q1[i].wt = q1[i].tat - q1[i].st;
totalTAT += q1[i].tat;
totalWT += q1[i].wt;
}

// SJF for Queue 2


for(int i=0;i<c2;i++){
if(time < q2[i].at) time = q2[i].at;
time += q2[i].st;
q2[i].ft = time;
q2[i].tat = q2[i].ft - q2[i].at;
q2[i].wt = q2[i].tat - q2[i].st;
totalTAT += q2[i].tat;
totalWT += q2[i].wt;
}

// Merge all jobs into one array


Job all[50];
int idx = 0;
for(int i=0;i<c1;i++) all[idx++] = q1[i];
for(int i=0;i<c2;i++) all[idx++] = q2[i];

// Sort merged array by Job ID for display


for(int i=0;i<idx-1;i++){
for(int j=i+1;j<idx;j++){
if(all[i].id > all[j].id){
Job temp = all[i]; all[i] = all[j]; all[j] = temp;
}
}
}

// Print final results table


printf("\nMULTILEVEL QUEUE (FCFS + SJF):\n");
printf("-------------------------------------------\n\n");
printf("Job\tAT\tST\tPR\tFT\tTAT\tWT\n");
for(int i=0;i<idx;i++){
printf("J%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
all[i].id, all[i].at, all[i].st, all[i].pr,
all[i].ft, all[i].tat, all[i].wt);
}

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


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

return 0;
}

Sample Input and Output:

You might also like