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

CSE23467

The document discusses various Operating System scheduling algorithms including FCFS, SJF, SRTF, RR, and Priority. Each algorithm is implemented in C, demonstrating how to calculate completion time, turnaround time, and waiting time for processes based on their arrival and burst times. The document includes code examples and execution orders for each scheduling method.

Uploaded by

avkbhavansurya
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 views9 pages

CSE23467

The document discusses various Operating System scheduling algorithms including FCFS, SJF, SRTF, RR, and Priority. Each algorithm is implemented in C, demonstrating how to calculate completion time, turnaround time, and waiting time for processes based on their arrival and burst times. The document includes code examples and execution orders for each scheduling method.

Uploaded by

avkbhavansurya
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
You are on page 1/ 9

OS Scheduling Algorithms

Name : Venkata Kanna Bhavan Surya Adapa

Roll no: CB.SC.U4CSE23467

FCFS
#include <stdio.h>
// Sort processes by Arrival Time
void ATsort(int proc[], int n, int burst[], int arrival[]) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arrival[j] > arrival[j + 1]) {
int temp = arrival[j];
arrival[j] = arrival[j + 1];
arrival[j + 1] = temp;

temp = burst[j];
burst[j] = burst[j + 1];
burst[j + 1] = temp;

temp = proc[j];
proc[j] = proc[j + 1];
proc[j + 1] = temp;
}
}
}
}

// Calculate Completion Time


void completionTime(int n, int burst[], int arrival[], int completion[]) {
completion[0] = arrival[0] + burst[0];
for (int i = 1; i < n; i++) {
if (completion[i - 1] < arrival[i]) {
completion[i] = arrival[i] + burst[i];
} else {
completion[i] = completion[i - 1] + burst[i];
}
}
}

// Calculate Turnaround Time (TAT = Completion Time - Arrival Time)


void TurnaroundTime(int n, int arrival[], int completion[], int turnaround[]) {
for (int i = 0; i < n; i++) {
turnaround[i] = completion[i] - arrival[i];
}
}

// Calculate Waiting Time (WT = TAT - Burst Time)


void waitingTime(int n, int burst[], int turnaround[], int wait[]) {
for (int i = 0; i < n; i++) {
wait[i] = turnaround[i] - burst[i];
}
}

void FCFS(int proc[], int n, int burst[], int arrival[]) {


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

ATsort(proc, n, burst, arrival);


completionTime(n, burst, arrival, completion);
TurnaroundTime(n, arrival, completion, turnaround);
waitingTime(n, burst, turnaround, wait);

printf("Process Arrival Burst Completion Turnaround Waiting\n");


for (int i = 0; i < n; i++)
printf("P%d\t %d\t %d\t %d\t\t %d\t\t %d\n", proc[i], arrival[i], burst[i],
completion[i], turnaround[i], wait[i]);

printf("\nProcess execution order: ");


for (int i = 0; i < n; i++)
printf("P%d ", proc[i]);
printf("\n");
}

int main() {
int proc[] = {1,2,3,4,5};
int n = sizeof(proc) / sizeof(proc[0]);
int burst[] = {5,8,3,6,9};
int arrival[] = {0,2,6,10,8};

FCFS(proc, n, burst, arrival);


return 0;
}
SJF
#include <stdio.h>

void ATsort(int count, int processId[], int burst[], int arrival[]) {


for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (arrival[j] > arrival[j + 1]) {
int temp = arrival[j];
arrival[j] = arrival[j + 1];
arrival[j + 1] = temp;

temp = burst[j];
burst[j] = burst[j + 1];
burst[j + 1] = temp;

temp = processId[j];
processId[j] = processId[j + 1];
processId[j + 1] = temp;
}
}
}
}

void SJF(int count,int processId[], int burst[],int arrival[]){


ATsort(count, processId, burst, arrival);
int completion[count],turnaround[count],waiting[count];
int isCompleted[count], currentTime = 0, completed = 0;
for (int i = 0; i < count; i++) isCompleted[i] = 0;
printf("\nProcess Execution Order: ");
while (completed < count){
int shortest = -1, minBurst = 9999;
for(int i = 0; i < count; i++){
if(arrival[i] <= currentTime && !isCompleted[i]){
if(burst[i] < minBurst){
minBurst = burst[i];
shortest = i;
}
}
}
if(shortest == -1){
currentTime++;
continue;
}
currentTime += burst[shortest];
completion[shortest] = currentTime;
turnaround[shortest] = completion[shortest] - arrival[shortest];
waiting[shortest] = turnaround[shortest] - burst[shortest];
isCompleted[shortest] = 1;
completed++;
printf("P%d ", processId[shortest]);
}
printf("\n\nPID\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < count; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processId[i], arrival[i], burst[i], completion[i],
turnaround[i], waiting[i]);
}
}

int main() {
int processId[] = {1,2,3,5,6};
int arrival[] = {0,2,4,10,8};
int burst[] = {5,8,3,6,9};
int count = sizeof(processId) / sizeof(processId[0]);
SJF(count, processId, burst, arrival);

return 0;
}

SRTF
#include <stdio.h>
#include <limits.h>

void calculateAvgTime(int count, int processId[], int arrival[], int burst[]) {


int remaining[count], completion[count], waiting[count], turnaround[count];
int completed = 0, currentTime = 0, shortestIndex = -1, shortestBurst = INT_MAX;
int startTime[count];
for (int i = 0; i < count; i++) {
remaining[i] = burst[i];
startTime[i] = -1;
}

printf("\nExecution Order: ");


while (completed < count) {
shortestIndex = -1;
shortestBurst = INT_MAX;

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


if (arrival[i] <= currentTime && remaining[i] > 0 && remaining[i] < shortestBurst) {
shortestBurst = remaining[i];
shortestIndex = i;
}
}

if (shortestIndex == -1) {
currentTime++;
continue;
}

if (startTime[shortestIndex] == -1) {
startTime[shortestIndex] = currentTime;
}

printf("P%d ", processId[shortestIndex]);


remaining[shortestIndex]--;
currentTime++;

if (remaining[shortestIndex] == 0) {
completed++;
completion[shortestIndex] = currentTime;
turnaround[shortestIndex] = completion[shortestIndex] - arrival[shortestIndex];
waiting[shortestIndex] = turnaround[shortestIndex] - burst[shortestIndex];
}
}

printf("\n\nPID\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < count; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processId[i], arrival[i], burst[i], completion[i],
turnaround[i], waiting[i]);
}
}

int main() {
int processId[] = {1,2,4,5,6};
int arrival[] = {0,2,4,5,8};
int burst[] = {5,8,11,6,9};
int count = sizeof(processId) / sizeof(processId[0]);

calculateAvgTime(count, processId, arrival, burst);


return 0;
}

RR
#include <stdio.h>

void calculateTimes(int processCount, int processId[], int arrival[], int burst[], int
timeQuantum) {
int remainingBurst[processCount], completion[processCount], waiting[processCount],
turnaround[processCount];
int currentTime = 0, allDone;

for (int i = 0; i < processCount; i++)


remainingBurst[i] = burst[i];

printf("\nExecution Order: ");

while (1) {
allDone = 1;

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


if (remainingBurst[i] > 0 && arrival[i] <= currentTime) {
allDone = 0;
printf("P%d ", processId[i]);

if (remainingBurst[i] > timeQuantum) {


currentTime += timeQuantum;
remainingBurst[i] -= timeQuantum;
} else {
currentTime += remainingBurst[i];
completion[i] = currentTime;
remainingBurst[i] = 0;
}
}
}
if (allDone)
break;
}
// Calculate Turnaround Time (TAT) and Waiting Time (WT)
for (int i = 0; i < processCount; i++) {
turnaround[i] = completion[i] - arrival[i];
waiting[i] = turnaround[i] - burst[i];
}

printf("\n\nPID\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < processCount; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n", processId[i], arrival[i], burst[i], completion[i],
turnaround[i], waiting[i]);
}
}
int main() {
int processId[] = {1,2,3,5,6};
int arrival[] = {0,2,6,10,8};
int burst[] = {5,8,11,6,9};
int processCount = sizeof(processId) / sizeof(processId[0]);
int timeQuantum = 4;
calculateTimes(processCount, processId, arrival, burst, timeQuantum);
return 0;
}

Priority
#include <stdio.h>

void calculateTimes(int count, int processId[], int arrival[], int burst[], int priority[]) {
int completion[count], waiting[count], turnaround[count], sortedIndices[count];

for (int i = 0; i < count; i++)


sortedIndices[i] = i;

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


for (int j = 0; j < count - i - 1; j++)
if (priority[sortedIndices[j]] > priority[sortedIndices[j + 1]]) {
int temp = sortedIndices[j];
sortedIndices[j] = sortedIndices[j + 1];
sortedIndices[j + 1] = temp;
}

int currentTime = 0;
printf("\nExecution Order: ");

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


int idx = sortedIndices[i];

if (currentTime < arrival[idx])


currentTime = arrival[idx];

printf("P%d ", processId[idx]);

completion[idx] = currentTime + burst[idx];


turnaround[idx] = completion[idx] - arrival[idx];
waiting[idx] = turnaround[idx] - burst[idx];

currentTime = completion[idx];
}

// Print results
printf("\n\nPID\tAT\tBT\tPriority\tCT\tTAT\tWT\n");
for (int i = 0; i < count; i++)
printf("%d\t%d\t%d\t%d\t\t%d\t%d\t%d\n", processId[i], arrival[i], burst[i], priority[i],
completion[i], turnaround[i], waiting[i]);
}

int main() {
int processId[] = {1,3,4,5,6};
int arrival[] = {0,2,4,10,8};
int burst[] = {5,8,3,6,9};
int priority[] = {2, 1, 3, 4, 0};
int count = sizeof(processId) / sizeof(processId[0]);

calculateTimes(count, processId, arrival, burst, priority);


return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy