Round Robin Scheduling algorithm in C

Round Robin Algorithm in C

TheLazyBusyCoder's Blog

Welcome to my coding adventures! In this post, we'll explore a C program.

Round Robin - scheduling algorithm in C

Check out my YouTube video: Watch now

        
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct process {
	int id  , at , bt , ct , tat, wt , rt;
} process;

typedef struct queue {
	int pro;
} queue;

queue root[50];

int front = 0;
int rare = 0;

void push(int id) {
	root[rare].pro = id;
	rare++;
}

int pop() {
	if(front == rare) return -1;
	int temp = root[front].pro;
	front++;
	return temp;
}

int isEmpty() {
	if(front == rare) return 1;
	return 0;
}

void sortAT(process pro[], int n)
{
	process temp;
	for(int i = 0; i < n; i++)
		for(int j = 0; j < n - i - 1; j++)
			if(pro[j].at > pro[j + 1].at)
			{
				temp = pro[j];
				pro[j] = pro[j + 1];
				pro[j + 1] = temp;
			}
}

void display(process pro[], int n)
{
	printf("\n\nTABLE:\n");
	for (int i = 0; i < n; i++)
	{
		printf("P%d AT: %d BT: %d CT: %d TAT: %d WT: %d\n",
				pro[i].id,
				pro[i].at,
				pro[i].bt,
				pro[i].ct,
				pro[i].tat,
				pro[i].wt);
	}
}

void roundrobin(process proc[], int n, int time_quantom) {
	sortAT(proc , n);
	int i , currTime = 0;
	for(i = 0; i < n;) {
		if(proc[i].at <= currTime && proc[i].rt > 0) {
			if(proc[i].rt < time_quantom) {
				currTime += proc[i].rt;
				proc[i].rt = 0;
				proc[i].ct = currTime;
			} else {
				proc[i].rt -= time_quantom;
				currTime += time_quantom;
				if(proc[i].rt > 0)
					push(proc[i].id);
				else {
					proc[i].ct = currTime;
				}
			}
			i++;
		} else currTime++;
	}

	while(!isEmpty()) {
		int curr = pop();
		int find = 0;
		while(find < n) {
			if(proc[find].id == curr) {
				break;
			}
			find++;
		}
		curr = find;
		if(proc[curr].rt < time_quantom) {  
			currTime += proc[curr].rt;
			proc[curr].rt = 0;
			proc[curr].ct = currTime;
		} else {
			proc[curr].rt -= time_quantom;
			currTime += time_quantom;
			proc[curr].ct = currTime;
			if(proc[curr].rt > 0)
				push(proc[curr].id);
		}
	}

	double twt = 0;
	double ttat = 0;

	for(i = 0; i < n; i++) {
		proc[i].tat = proc[i].ct - proc[i].at;
		proc[i].wt = proc[i].tat - proc[i].bt;
		twt += proc[i].wt;
		ttat = proc[i].tat;
	}

	printf("\n");
	display(proc , n);
	printf("\nAvg waiting time: %.2f\nAvg turnaround time: %.2f\n\n", twt / n, ttat / n);
}


int main() {
	int n = 4;
	printf("Enter number of processes: ");
	scanf(" %d" , &n);
	process pro[n];
	int i = 0;
	for(i = 0; i < n; i++) {
		pro[i].id = i+1;
		printf("Enter AT: ");
		scanf(" %d" , &pro[i].at);
		printf("Enter BT: ");
		scanf(" %d" , &pro[i].bt);
		pro[i].rt = pro[i].bt;
		pro[i].ct = pro[i].wt = pro[i].tat = 0;
	}
	int tq;
	printf("Enter time quantom: ");
	scanf(" %d" , &tq);
	roundrobin(pro , n , tq);
}
            
        
    

Comments

Post a Comment

Popular posts from this blog

Least Recently Used (LRU) page algorithm in c

Optimal Page Replacement (OPT) in C

OS Assignment 2 MyShell to do file operations