0% found this document useful (0 votes)
6 views

schedule

The document is a C program that implements various CPU scheduling algorithms including First-Come, First-Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. It defines a structure for processes and includes functions to calculate turnaround time, waiting time, and display results for each scheduling algorithm. The program prompts the user for process details and executes the scheduling algorithms based on the provided input.
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)
6 views

schedule

The document is a C program that implements various CPU scheduling algorithms including First-Come, First-Served (FCFS), Shortest Job First (SJF), Priority Scheduling, and Round Robin. It defines a structure for processes and includes functions to calculate turnaround time, waiting time, and display results for each scheduling algorithm. The program prompts the user for process details and executes the scheduling algorithms based on the provided input.
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
You are on page 1/ 6

#include <stdio.

h>

// Muhammed Noshin M

typedef struct {

int pid, at, bt, priority, wt, tat, ct, remaining_bt;

} Process;

void calculateTimes(Process p[], int n) {

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

p[i].tat = p[i].ct - p[i].at;

p[i].wt = p[i].tat - p[i].bt;

void displayResults(Process p[], int n, char *algo) {

printf("\n%s Scheduling Results:\n", algo);

printf("PID\tAT\tBT\tPriority\tCT\tTAT\tWT\n");

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

printf("%d\t%d\t%d\t%d\t\t%d\t%d\t%d\n",

p[i].pid, p[i].at, p[i].bt, p[i].priority, p[i].ct,

p[i].tat, p[i].wt);

void sortByArrival(Process p[], int n) {

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

for (int j = 0; j < n - i - 1; j++) {

if (p[j].at > p[j + 1].at) {


Process temp = p[j];

p[j] = p[j + 1];

p[j + 1] = temp;

void fcfs(Process p[], int n) {

sortByArrival(p, n);

int time = 0;

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

if (time < p[i].at) {

time = p[i].at;

time += p[i].bt;

p[i].ct = time;

calculateTimes(p, n);

displayResults(p, n, "FCFS");

void sjf(Process p[], int n) {

sortByArrival(p, n);

int completed = 0, time = 0, min_idx;

int done[100] = {0};

while (completed < n) {


min_idx = -1;

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

if (!done[i] && p[i].at <= time) {

if (min_idx == -1 || p[i].bt < p[min_idx].bt) {

min_idx = i;

if (min_idx == -1) {

time++;

} else {

time += p[min_idx].bt;

p[min_idx].ct = time;

done[min_idx] = 1;

completed++;

calculateTimes(p, n);

displayResults(p, n, "SJF");

void priorityScheduling(Process p[], int n) {

sortByArrival(p, n);

int completed = 0, time = 0, min_idx;

int done[100] = {0};

while (completed < n) {

min_idx = -1;
for (int i = 0; i < n; i++) {

if (!done[i] && p[i].at <= time) {

if (min_idx == -1 || p[i].priority <

p[min_idx].priority) {

min_idx = i;

if (min_idx == -1) {

time++;

} else {

time += p[min_idx].bt;

p[min_idx].ct = time;

done[min_idx] = 1;

completed++;

calculateTimes(p, n);

displayResults(p, n, "Priority Scheduling");

void roundRobin(Process p[], int n, int quantum) {

int time = 0, remaining = n;

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

p[i].remaining_bt = p[i].bt;

}
while (remaining > 0) {

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

if (p[i].remaining_bt > 0) {

int exec_time;

if (p[i].remaining_bt > quantum) {

exec_time = quantum;

} else {

exec_time = p[i].remaining_bt;

p[i].remaining_bt -= exec_time;

time += exec_time;

if (p[i].remaining_bt == 0) {

p[i].ct = time;

remaining--;

calculateTimes(p, n);

displayResults(p, n, "Round Robin");

int main() {

int n, quantum;

printf("Enter number of processes: ");

scanf("%d", &n);
Process p[n], p_fcfs[n], p_sjf[n], p_prio[n], p_rr[n];

printf("Enter process details (PID AT BT Priority):\n");

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

printf("Enter process ID: ");

scanf("%d", &p[i].pid);

printf("Enter arrival time: ");

scanf("%d", &p[i].at);

printf("Enter burst time: ");

scanf("%d", &p[i].bt);

printf("Enter priority: ");

scanf("%d", &p[i].priority);

p_fcfs[i] = p_sjf[i] = p_prio[i] = p_rr[i] = p[i];

printf("Enter time quantum for Round Robin: ");

scanf("%d", &quantum);

fcfs(p_fcfs, n);

sjf(p_sjf, n);

priorityScheduling(p_prio, n);

roundRobin(p_rr, n, quantum);

return 0;

You might also like