CSE23467
CSE23467
CSE23467
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;
}
}
}
}
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};
temp = burst[j];
burst[j] = burst[j + 1];
burst[j + 1] = temp;
temp = processId[j];
processId[j] = processId[j + 1];
processId[j + 1] = temp;
}
}
}
}
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>
if (shortestIndex == -1) {
currentTime++;
continue;
}
if (startTime[shortestIndex] == -1) {
startTime[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]);
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;
while (1) {
allDone = 1;
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];
int currentTime = 0;
printf("\nExecution Order: ");
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]);