cpu_scheduling.
c
#include <stdio.h>
#include <stdlib.h>
struct ProcessControlBlock {
int procId;
int cpuBurst;
int waitingTime;
struct ProcessControlBlock
*next;
struct ProcessControlBlock
*prev;
};
struct ProcessControlBlock *createProcessControlBlock(int procId, int
cpuBurst) {
struct ProcessControlBlock
*newPCB = (struct ProcessControlBlock
*)malloc(sizeof(struct ProcessControlBlock
));
newPCB->procId = procId;
newPCB->cpuBurst = cpuBurst;
newPCB->waitingTime = 0;
newPCB->next = NULL;
newPCB->prev = NULL;
return newPCB;
}
void insertFCFS(struct ProcessControlBlock **head, struct
ProcessControlBlock **tail, struct ProcessControlBlock *newPCB) {
if (*head == NULL) {
*head = newPCB;
*tail = newPCB;
} else {
(*tail)->next = newPCB;
newPCB->prev = *tail;
*tail = newPCB;
}
}
void insertRR(struct ProcessControlBlock **head, struct ProcessControlBlock
**tail, struct ProcessControlBlock *newPCB) {
if (*head == NULL) {
*head = newPCB;
*tail = newPCB;
newPCB->next = newPCB;
newPCB->prev = newPCB;
} else {
(*tail)->next = newPCB;
newPCB->prev = *tail;
newPCB->next = *head;
(*head)->prev = newPCB;
*tail = newPCB;
}
}
void executeFCFS(struct ProcessControlBlock **head, struct
ProcessControlBlock **tail) {
int time = 0;
int waitingTime = 0;
int count = 0;
struct ProcessControlBlock
*completedProcesses[10];
struct ProcessControlBlock
*current = *head;
while (current != NULL) {
printf("Executing process %d with CPU burst %d\n", current->procId,
current->cpuBurst);
time += current->cpuBurst;
current->waitingTime = time - current->cpuBurst;
waitingTime += current->waitingTime;
completedProcesses[count] = current;
count++;
current = current->next;
}
printf("\nAverage waiting time: %.2f\n", (float)waitingTime / count);
printf("Order of completion: \n");
for (int i = 0; i < count; i++) {
printf("%d ", completedProcesses[i]->procId);
printf(" - waiting time: %d\n", completedProcesses[i]->waitingTime);
}
}
void executeRR(struct ProcessControlBlock **head, struct
ProcessControlBlock **tail, int timeQuantum) {
int originalBurst[10] = {0};
struct ProcessControlBlock
*temp = *head;
for (int i = 0; i < 10; i++) {
if (temp == NULL) {
break;
}
originalBurst[temp->procId] = temp->cpuBurst;
temp = temp->next;
}
int time = 0;
int waitingTime = 0;
int count = 0;
struct ProcessControlBlock
*completedProcesses[10];
struct ProcessControlBlock
*current = *head;
while (current != NULL) {
printf("Executing process %d with CPU burst %d\n", current->procId,
current->cpuBurst);
if (current->cpuBurst > timeQuantum) {
current->cpuBurst -= timeQuantum;
time += timeQuantum;
current = current->next;
} else {
time += current->cpuBurst;
current->waitingTime = time - originalBurst[current->procId];
waitingTime += current->waitingTime;
completedProcesses[count] = current;
count++;
if (current->prev == current) {
*head = NULL;
*tail = NULL;
break;
}
current->prev->next = current->next;
current->next->prev = current->prev;
current = current->next;
}
}
printf("\nAverage waiting time: %.2f\n", (float)waitingTime / count);
printf("Order of completion: \n");
for (int i = 0; i < count; i++) {
printf("%d ", completedProcesses[i]->procId);
printf(" - waiting time: %d\n", completedProcesses[i]->waitingTime);
}
}
int main() {
struct ProcessControlBlock
*headFCFS = NULL;
struct ProcessControlBlock
*tailFCFS = NULL;
struct ProcessControlBlock
*headRR = NULL;
struct ProcessControlBlock
*tailRR = NULL;
insertFCFS(&headFCFS, &tailFCFS, createProcessControlBlock(1, 10));
insertFCFS(&headFCFS, &tailFCFS, createProcessControlBlock(2, 5));
insertFCFS(&headFCFS, &tailFCFS, createProcessControlBlock(3, 8));
insertRR(&headRR, &tailRR, createProcessControlBlock(1, 10));
insertRR(&headRR, &tailRR, createProcessControlBlock(2, 5));
insertRR(&headRR, &tailRR, createProcessControlBlock(3, 8));
printf("Processes added to the queue:\n");
struct ProcessControlBlock
*temp = headFCFS;
while (temp != NULL) {
printf("Process %d with CPU burst %d\n", temp->procId,
temp->cpuBurst);
temp = temp->next;
}
printf("---\n");
printf("First Come First Serve\n");
executeFCFS(&headFCFS, &tailFCFS);
int timeQuantum = 3;
printf("\nRound Robin with time quantum = %d\n", timeQuantum);
executeRR(&headRR, &tailRR, timeQuantum);
return 0;
}
Output
Processes added to the queue:
Process 1 with CPU burst 10
Process 2 with CPU burst 5
Process 3 with CPU burst 8
---
First Come First Serve
Executing process 1 with CPU burst 10
Executing process 2 with CPU burst 5
Executing process 3 with CPU burst 8
Average waiting time: 8.33
Order of completion:
1 - waiting time: 0
2 - waiting time: 10
3 - waiting time: 15
Round Robin with time quantum = 3
Executing process 1 with CPU burst 10
Executing process 2 with CPU burst 5
Executing process 3 with CPU burst 8
Executing process 1 with CPU burst 7
Executing process 2 with CPU burst 2
Executing process 3 with CPU burst 5
Executing process 1 with CPU burst 4
Executing process 3 with CPU burst 2
Executing process 1 with CPU burst 1
Average waiting time: 12.00
Order of completion:
2 - waiting time: 9
3 - waiting time: 14
1 - waiting time: 13
(base) santhoshtk@intense ~/Desktop/PSG-TECH/4th-sem/OS/Lab