0% found this document useful (0 votes)
7 views12 pages

2 Os

The document contains a programming assignment on scheduling algorithms in operating systems, including implementations for Round Robin, Shortest Remaining Time First (SRTF), Priority Scheduling, and Multiple-Level Queues Scheduling. Each section includes C code that calculates waiting time, turnaround time, and displays Gantt charts for the respective scheduling algorithms. The assignment is structured with questions and code examples, aimed at understanding process scheduling in operating systems.

Uploaded by

70131330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

2 Os

The document contains a programming assignment on scheduling algorithms in operating systems, including implementations for Round Robin, Shortest Remaining Time First (SRTF), Priority Scheduling, and Multiple-Level Queues Scheduling. Each section includes C code that calculates waiting time, turnaround time, and displays Gantt charts for the respective scheduling algorithms. The assignment is structured with questions and code examples, aimed at understanding process scheduling in operating systems.

Uploaded by

70131330
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

NAME: Mohammad Ali Ishfaq

SAP ID: 70131330


DEPATMENT: CS & IT
SECTION: BSCS-6
TEACHER’s NAME: Mam Fatima
SUBJECT: Operating Systems
ASSISGNMENT # 02
TOPIC: Scheduling Algorithms

Q1.
Round Robin:
#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum, int gantt[], int
*gantt_len) {
int rem_bt[n];
for (int i = 0; i < n; i++) rem_bt[i] = bt[i];
int t = 0;
*gantt_len = 0;

while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (rem_bt[i] > 0) {
done = 0;
gantt[*gantt_len] = processes[i];
(*gantt_len)++;

if (rem_bt[i] > quantum) {


t += quantum;
rem_bt[i] -= quantum;
} else {
t += rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0;
}
}
}
if (done == 1) break;
}
}

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[]) {
for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i];
}

void printGanttChart(int gantt[], int gantt_len) {


printf("Gantt Chart:\n|");
for (int i = 0; i < gantt_len; i++) {
printf(" P%d |", gantt[i]);
}
printf("\n");
}

void findAvgTime(int processes[], int n, int bt[], int quantum) {


int wt[n], tat[n];
int gantt[100], gantt_len;

findWaitingTime(processes, n, bt, wt, quantum, gantt, &gantt_len);


findTurnAroundTime(processes, n, bt, wt, tat);

printGanttChart(gantt, gantt_len);

printf("Process ID\tBurst Time\tWaiting Time\tTurnaround Time\n");


int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
total_wt += wt[i];
total_tat += tat[i];
printf("%d\t\t%d\t\t%d\t\t%d\n", processes[i], bt[i], wt[i], tat[i]);
}
printf("Average waiting time = %.2f\n", (float)total_wt / n);
printf("Average turnaround time = %.2f\n", (float)total_tat / n);
}
int main() {
int n, quantum;
printf("Enter number of processes: ");
scanf("%d", &n);
int processes[n], burst_time[n];
for (int i = 0; i < n; i++) {
processes[i] = i + 1;
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
findAvgTime(processes, n, burst_time, quantum);
return 0;
}

Q2.
SRTF:
#include <stdio.h>

#define MAX 10

typedef struct {
int pid;
int arrivalTime;
int burstTime;
int remainingTime;
int completionTime;
int turnaroundTime;
int waitTime;
} Process;

float averageTurnaroundTime(Process processes[], int n) {


int totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
totalTurnaroundTime += processes[i].turnaroundTime;
}
return (float)totalTurnaroundTime / n;
}

float averageWaitingTime(Process processes[], int n) {


int totalWaitTime = 0;
for (int i = 0; i < n; i++) {
totalWaitTime += processes[i].waitTime;
}
return (float)totalWaitTime / n;
}

void displayGanttChart(Process processes[], int n) {


printf("\nGantt Chart:\n");
for (int i = 0; i < n; i++) {
printf("| P%d ", processes[i].pid);
}
printf("|\n");

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


printf(" %d ", processes[i].completionTime);
}
printf("\n");
}

void SRT(Process processes[], int n) {


int time = 0;
int completed = 0;
int i, j;
int shortestRemainingTime = MAX;
int shortestProcessIndex = -1;
int isCompleted[n];

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


isCompleted[i] = 0;
processes[i].remainingTime = processes[i].burstTime;
}

while (completed < n) {


shortestRemainingTime = MAX;
shortestProcessIndex = -1;

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


if (processes[i].arrivalTime <= time && !isCompleted[i] && processes[i].remainingTime
< shortestRemainingTime) {
shortestRemainingTime = processes[i].remainingTime;
shortestProcessIndex = i;
}
}

if (shortestProcessIndex == -1) {
time++;
} else {
processes[shortestProcessIndex].remainingTime--;
time++;

if (processes[shortestProcessIndex].remainingTime == 0) {
isCompleted[shortestProcessIndex] = 1;
completed++;
processes[shortestProcessIndex].completionTime = time;
processes[shortestProcessIndex].turnaroundTime =
processes[shortestProcessIndex].completionTime - processes[shortestProcessIndex].arrivalTime;
processes[shortestProcessIndex].waitTime =
processes[shortestProcessIndex].turnaroundTime - processes[shortestProcessIndex].burstTime;
}
}
}
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

if (n <= 0 || n > MAX) {


printf("Please enter a number between 1 and %d.\n", MAX);
return 1; // Exit if the number of processes is invalid
}
Process processes[MAX];

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


processes[i].pid = i + 1;
printf("Enter Arrival Time and Burst Time for Process P%d: ", processes[i].pid);
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
}

SRT(processes, n);

printf("\nProcess ID | Arrival Time | Burst Time | Completion Time | Turnaround Time | Wait
Time\n");
for (int i = 0; i < n; i++) {
printf("P%d | %d | %d | %d | %d | %d\n",
processes[i].pid, processes[i].arrivalTime, processes[i].burstTime,
processes[i].completionTime, processes[i].turnaroundTime, processes[i].waitTime);
}

printf("\nAverage Turnaround Time: %.2f", averageTurnaroundTime(processes, n));


printf("\nAverage Waiting Time: %.2f", averageWaitingTime(processes, n));

displayGanttChart(processes, n);

return 0;
}
Q3.
Priority:
#include <stdio.h>

typedef struct {
int processID;
int burstTime;
int priority;
int arrivalTime;
int waitingTime;
int turnaroundTime;
} Process;

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


int totalWaitTime = 0, totalTurnaroundTime = 0;
int completionTime[n];

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


for (int j = i+1; j < n; j++) {
if (processes[i].priority < processes[j].priority ||
(processes[i].priority == processes[j].priority && processes[i].arrivalTime >
processes[j].arrivalTime)) {

Process temp = processes[i];


processes[i] = processes[j];
processes[j] = temp;
}
}
}

completionTime[0] = processes[0].arrivalTime + processes[0].burstTime;


for (int i = 1; i < n; i++) {
processes[i].waitingTime = completionTime[i-1] - processes[i].arrivalTime;
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
completionTime[i] = completionTime[i-1] + processes[i].burstTime;

totalWaitTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}

float avgWaitTime = (float)totalWaitTime / n;


float avgTurnaroundTime = (float)totalTurnaroundTime / n;
printf("\nPROCESS\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].processID, processes[i].priority,
processes[i].burstTime, processes[i].waitingTime, processes[i].turnaroundTime);
}

printf("\nAverage Waiting Time: %.6f", avgWaitTime);


printf("\nAverage Turnaround Time: %.6f", avgTurnaroundTime);

printf("\n\nGantt Chart:\n");
printf("|\t");
for (int i = 0; i < n; i++) {
printf("P%d\t", processes[i].processID);
}
printf("|\n0\t");
for (int i = 0; i < n; i++) {
printf("%d\t", completionTime[i]);
}
printf("\n");
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

Process processes[n];

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


processes[i].processID = i;
printf("Enter Burst Time and Priority of Process %d: ", i);
scanf("%d %d", &processes[i].burstTime, &processes[i].priority);
printf("Enter Arrival Time of Process %d: ", i);
scanf("%d", &processes[i].arrivalTime);
}

calculateTimes(processes, n);

return 0;
}
Q4.
Multiple-Level Queues Scheduling:
#include <stdio.h>

struct Process {
int processID;
int burstTime;
int waitingTime;
int turnaroundTime;
int systemUser;
};

void calculateTimes(struct Process processes[], int n) {


int totalWaitTime = 0, totalTurnaroundTime = 0;
int systemProcessesCount = 0;
for (int i = 0; i < n; i++) {
if (processes[i].systemUser == 1) {
systemProcessesCount++;
}
}

int systemCompletionTime = 0;
for (int i = 0; i < systemProcessesCount; i++) {
processes[i].waitingTime = systemCompletionTime;
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
systemCompletionTime = processes[i].turnaroundTime;
totalWaitTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}

int userCompletionTime = systemCompletionTime;


for (int i = systemProcessesCount; i < n; i++) {
processes[i].waitingTime = userCompletionTime;
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
userCompletionTime = processes[i].turnaroundTime;
totalWaitTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}

float avgWaitTime = (float)totalWaitTime / n;


float avgTurnaroundTime = (float)totalTurnaroundTime / n;
printf("\nPROCESS\tSYSTEM/USER PROCESS\tBURST TIME\tWAITING
TIME\tTURNAROUND TIME\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t\t%d\t\t%d\t\t%d\n", processes[i].processID, processes[i].systemUser,
processes[i].burstTime, processes[i].waitingTime, processes[i].turnaroundTime);
}

printf("\nAverage Waiting Time: %.6f", avgWaitTime);


printf("\nAverage Turnaround Time: %.6f", avgTurnaroundTime);
}

int main() {
int n;

printf("Enter the number of processes: ");


scanf("%d", &n);

struct Process processes[n];

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


processes[i].processID = i;
printf("Enter the Burst Time of Process %d: ", i);
scanf("%d", &processes[i].burstTime);
printf("System/User Process (0/1) ? ");
scanf("%d", &processes[i].systemUser);
}

calculateTimes(processes, n);
return 0;
}

You might also like