FCFS
#include <stdio.h> // Standard input-output library
void main() {
int n, i, j; // Variables for number of processes and loop counters
// Ask the user for the number of processes
printf("Enter the number of processes: ");
scanf("%d", &n);
// Declare arrays to store process details
int process[n], arrival[n], burst[n], completion[n], turnaround[n], waiting[n];
// Input: Getting arrival time and burst time for each process
printf("Enter Arrival Time and Burst Time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d %d", &arrival[i], &burst[i]); // Read Arrival Time and Burst Time
process[i] = i + 1; // Assign process ID (1, 2, 3, ...)
}
// Step 1: Sort processes by Arrival Time (if needed)
// FCFS executes processes in the order they arrive, so sorting ensures correctness
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (arrival[i] > arrival[j]) { // Compare Arrival Time
// Swap Arrival Time
int temp = arrival[i];
arrival[i] = arrival[j];
arrival[j] = temp;
// Swap Burst Time
temp = burst[i];
burst[i] = burst[j];
burst[j] = temp;
// Swap Process ID
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}
// Step 2: Compute Completion Time (CT)
// The first process starts at its arrival time and finishes after its burst time
completion[0] = arrival[0] + burst[0];
// Calculate Completion Time for remaining processes
for (i = 1; i < n; i++) {
if (completion[i - 1] < arrival[i]) {
// If CPU was idle before this process arrived
completion[i] = arrival[i] + burst[i];
} else {
// Process starts execution immediately after the previous one completes
completion[i] = completion[i - 1] + burst[i];
}
}
// Step 3: Compute Turnaround Time (TAT) and Waiting Time (WT)
for (i = 0; i < n; i++) {
turnaround[i] = completion[i] - arrival[i]; // TAT = CT - AT
waiting[i] = turnaround[i] - burst[i]; // WT = TAT - BT
}
// Step 4: Display the results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t%d\t%d\t%d\t%d\n",
process[i], arrival[i], burst[i], completion[i], turnaround[i], waiting[i]);
}
}
LRU
#include <stdio.h>
#define FRAME_SIZE 30
int findLRU(int time[], int n) {
int i, minimum = time[0], pos = 0;
for(i = 1; i < n; ++i) {
if(time[i] < minimum) {
minimum = time[i];
pos = i;
}
}
return pos;
}
int main() {
int frames[FRAME_SIZE], pages[100], time[FRAME_SIZE];
int total_pages, page_faults = 0, counter = 0;
int i, j, flag1, flag2, pos;
printf("Enter total number of pages: ");
scanf("%d", &total_pages);
printf("Enter the page reference string: ");
for(i = 0; i < total_pages; ++i) {
scanf("%d", &pages[i]);
}
for(i = 0; i < FRAME_SIZE; ++i) frames[i] = -1;
for(i = 0; i < total_pages; ++i) {
flag1 = flag2 = 0;
for(j = 0; j < FRAME_SIZE; ++j) {
if(frames[j] == pages[i]) {
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0) {
for(j = 0; j < FRAME_SIZE; ++j) {
if(frames[j] == -1) {
counter++;
page_faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if(flag2 == 0) {
pos = findLRU(time, FRAME_SIZE);
counter++;
page_faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for(j = 0; j < FRAME_SIZE; ++j) {
if(frames[j] != -1)
printf("%d ", frames[j]);
else
printf("- ");
}
}
printf("\n\nTotal Page Faults = %d\n", page_faults);
return 0;
}
RR
#include <stdio.h>
int main() {
int n, i, time = 0, tq, completed = 0;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], rt[n], wt[n], tat[n], ct[n];
// Input arrival and burst times
for (i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter arrival time and burst time for process P%d: ", pid[i]);
scanf("%d %d", &at[i], &bt[i]);
rt[i] = bt[i]; // Remaining time initialized to burst time
wt[i] = 0;
tat[i] = 0;
ct[i] = 0;
}
printf("Enter time quantum: ");
scanf("%d", &tq);
int allDone = 0;
while (completed < n) {
allDone = 1;
for (i = 0; i < n; i++) {
if (rt[i] > 0 && at[i] <= time) {
allDone = 0;
if (rt[i] > tq) {
time += tq;
rt[i] -= tq;
} else {
time += rt[i];
rt[i] = 0;
ct[i] = time;
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
completed++;
}
}
}
if (allDone) {
time++;
}
}
// Output the results
float totalWT = 0, totalTAT = 0;
printf("\nProcess\tAT\tBT\tCT\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], ct[i], wt[i], tat[i]);
totalWT += wt[i];
totalTAT += tat[i];
}
printf("\nAverage Waiting Time = %.2f\n", totalWT / n);
printf("Average Turnaround Time = %.2f\n", totalTAT / n);
return 0;
}
Priority non preemtive
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], pr[n], ct[n], wt[n], tat[n], done[n];
int i, time = 0, completed = 0;
// Input arrival time, burst time, and priority
for (i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter Arrival Time, Burst Time, and Priority for Process P%d: ", pid[i]);
scanf("%d %d %d", &at[i], &bt[i], &pr[i]);
done[i] = 0;
}
while (completed < n) {
int idx = -1;
int highestPriority = 9999;
for (i = 0; i < n; i++) {
if (at[i] <= time && done[i] == 0) {
if (pr[i] < highestPriority || (pr[i] == highestPriority && at[i] < at[idx])) {
highestPriority = pr[i];
idx = i;
}
}
}
if (idx != -1) {
time += bt[idx];
ct[idx] = time;
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];
done[idx] = 1;
completed++;
} else {
time++; // CPU is idle
}
}
// Display results
float totalWT = 0, totalTAT = 0;
printf("\nProcess\tAT\tBT\tPR\tCT\tWT\tTAT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], pr[i], ct[i], wt[i], tat[i]);
totalWT += wt[i];
totalTAT += tat[i];
}
printf("\nAverage Waiting Time = %.2f\n", totalWT / n);
printf("Average Turnaround Time = %.2f\n", totalTAT / n);
return 0;
}
Priority preemtive
#include <stdio.h>
int main() {
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
int pid[n], at[n], bt[n], pr[n], rt[n];
int ct[n], tat[n], wt[n], done[n];
// Input process data
for (int i = 0; i < n; i++) {
pid[i] = i + 1;
printf("Enter Arrival Time, Burst Time, and Priority for Process P%d: ", pid[i]);
scanf("%d %d %d", &at[i], &bt[i], &pr[i]);
rt[i] = bt[i]; // Remaining time
done[i] = 0;
}
int time = 0, completed = 0;
while (completed < n) {
int idx = -1;
int highestPriority = 9999;
// Find the process with highest priority at current time
for (int i = 0; i < n; i++) {
if (at[i] <= time && rt[i] > 0) {
if (pr[i] < highestPriority || (pr[i] == highestPriority && at[i] < at[idx])) {
highestPriority = pr[i];
idx = i;
}
}
}
if (idx != -1) {
rt[idx]--;
time++;
if (rt[idx] == 0) {
ct[idx] = time;
tat[idx] = ct[idx] - at[idx];
wt[idx] = tat[idx] - bt[idx];
done[idx] = 1;
completed++;
}
} else {
time++; // CPU is idle
}
}
// Output results
float totalWT = 0, totalTAT = 0;
printf("\nProcess\tAT\tBT\tPR\tCT\tWT\tTAT\n");
for (int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", pid[i], at[i], bt[i], pr[i], ct[i], wt[i], tat[i]);
totalWT += wt[i];
totalTAT += tat[i];
}
printf("\nAverage Waiting Time = %.2f", totalWT / n);
printf("\nAverage Turnaround Time = %.2f\n", totalTAT / n);
return 0;
}
SJF non preemtive
#include <stdio.h>
// Define maximum number of processes
#define MAX 10
int main() {
int n, i, j;
int at[MAX], bt[MAX], ct[MAX], tat[MAX], wt[MAX], completed[MAX];
int time = 0, total_tat = 0, total_wt = 0;
// Input the number of processes
printf("Enter number of processes: ");
scanf("%d", &n);
// Input arrival time and burst time for each process
for (i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for Process P%d: ", i + 1);
scanf("%d%d", &at[i], &bt[i]);
completed[i] = 0; // Initially, no process is completed
}
// Process scheduling loop
int completed_count = 0;
while (completed_count < n) {
int idx = -1;
int min_bt = 9999;
// Find the shortest job that has arrived and is not completed
for (i = 0; i < n; i++) {
if (at[i] <= time && completed[i] == 0 && bt[i] < min_bt) {
min_bt = bt[i];
idx = i;
}
}
if (idx != -1) {
// Process found
time += bt[idx]; // Update current time
ct[idx] = time; // Completion time
tat[idx] = ct[idx] - at[idx]; // Turnaround time
wt[idx] = tat[idx] - bt[idx]; // Waiting time
completed[idx] = 1; // Mark process as completed
completed_count++;
total_tat += tat[idx];
total_wt += wt[idx];
} else {
// If no process is ready to execute, increment time
time++;
}
}
// Output the results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);
}
// Print average turnaround and waiting time
printf("\nAverage Turnaround Time = %.2f\n", (float)total_tat / n);
printf("Average Waiting Time = %.2f\n", (float)total_wt / n);
return 0;
}
SJF preemtive
#include <stdio.h>
#include <limits.h>
#include <stdbool.h> //boolean type bool and the values true and false
// Maximum number of processes
#define MAX 10
int main() {
int n; // Number of processes
int arrival_time[MAX], burst_time[MAX], remaining_time[MAX];
int completion_time[MAX], turnaround_time[MAX], waiting_time[MAX];
bool is_done[MAX] = {false};
printf("Enter number of processes: ");
scanf("%d", &n);
// Input arrival time and burst time for each process
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process P%d: ", i + 1);
scanf("%d%d", &arrival_time[i], &burst_time[i]);
remaining_time[i] = burst_time[i]; // Initially, remaining time is equal to burst time
}
int complete = 0, current_time = 0;
// Loop until all processes are completed
while (complete != n) {
int shortest = -1;
int min_remaining = INT_MAX;
// Find the process with the shortest remaining time among the arrived processes
for (int i = 0; i < n; i++) {
if (arrival_time[i] <= current_time && !is_done[i] && remaining_time[i] <
min_remaining && remaining_time[i] > 0) {
min_remaining = remaining_time[i];
shortest = i;
}
}
// If no process is available at the current time
if (shortest == -1) {
current_time++;
continue;
}
// Execute the selected process for 1 unit of time
remaining_time[shortest]--;
current_time++;
// If the process is finished
if (remaining_time[shortest] == 0) {
completion_time[shortest] = current_time;
is_done[shortest] = true;
complete++;
}
}
// Calculate turnaround time and waiting time for each process
float total_tat = 0, total_wt = 0;
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (int i = 0; i < n; i++) {
turnaround_time[i] = completion_time[i] - arrival_time[i];
waiting_time[i] = turnaround_time[i] - burst_time[i];
total_tat += turnaround_time[i];
total_wt += waiting_time[i];
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, arrival_time[i], burst_time[i],
completion_time[i], turnaround_time[i], waiting_time[i]);
}
// Print average turnaround time and waiting time
printf("\nAverage Turnaround Time = %.2f", total_tat / n);
printf("\nAverage Waiting Time = %.2f\n", total_wt / n);
return 0;
}