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

Operating Systems Lab Digital Assignment 21BME0595

docuemnt

Uploaded by

nair kaka
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)
8 views

Operating Systems Lab Digital Assignment 21BME0595

docuemnt

Uploaded by

nair kaka
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/ 11

21BME0595

Operating Systems Lab Digital Assignment – 2


Name – Arjun Nair
Reg. No. – 21BME0595

1. Implement the following scheduling algorithms: FCFS, SJF, Round Robin and
Priority
a. FCFS
Code –
// Online C compiler to run C program online
#include <stdio.h>

void fcfs_scheduling(int n, int arrival_time[], int burst_time[]) {


int wait_time[n], turnaround_time[n], total_wait = 0, total_turnaround = 0;

wait_time[0] = 0;

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


wait_time[i] = burst_time[i - 1] + wait_time[i - 1];
}

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


turnaround_time[i] = burst_time[i] + wait_time[i];
total_wait += wait_time[i];
total_turnaround += turnaround_time[i];
}

printf("Process\tArrival Time\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", i + 1, arrival_time[i], burst_time[i], wait_time[i],


turnaround_time[i]);
}

1
21BME0595

printf("Average Waiting Time: %.2f\n", (float)total_wait / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
}

int main() {
int n = 4;
int arrival_time[] = {0, 1, 2, 3};
int burst_time[] = {8, 4, 9, 5};

fcfs_scheduling(n, arrival_time, burst_time);


return 0;
}

Output-

b. SJF
Code –
#include <stdio.h>

void sjf_scheduling(int n, int arrival_time[], int burst_time[]) {


int wait_time[n], turnaround_time[n], total_wait = 0, total_turnaround = 0;
int completed = 0, current_time = 0, min_index;
int is_completed[n];

2
21BME0595

for (int i = 0; i < n; i++) is_completed[i] = 0;

while (completed != n) {
min_index = -1;
int min_burst = 1e9;

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


if (arrival_time[i] <= current_time && !is_completed[i] && burst_time[i] <
min_burst) {
min_burst = burst_time[i];
min_index = i;
}
}

if (min_index == -1) {
current_time++;
} else {
wait_time[min_index] = current_time - arrival_time[min_index];
current_time += burst_time[min_index];

turnaround_time[min_index] = wait_time[min_index] + burst_time[min_index];


is_completed[min_index] = 1;
completed++;
total_wait += wait_time[min_index];
total_turnaround += turnaround_time[min_index];
}
}

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


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

3
21BME0595

printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, arrival_time[i], burst_time[i], wait_time[i],


turnaround_time[i]);
}

printf("Average Waiting Time: %.2f\n", (float)total_wait / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
}

int main() {
int n = 4;
int arrival_time[] = {0, 1, 2, 3};
int burst_time[] = {8, 4, 9, 5};

sjf_scheduling(n, arrival_time, burst_time);


return 0;
}

Output –

c. Round Robin
Code –
#include <stdio.h>

4
21BME0595

void round_robin_scheduling(int n, int arrival_time[], int burst_time[], int quantum) {


int wait_time[n], turnaround_time[n], remaining_time[n], total_wait = 0, total_turnaround
= 0;
int current_time = 0;

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


remaining_time[i] = burst_time[i];
}

while (1) {
int done = 1;
for (int i = 0; i < n; i++) {
if (remaining_time[i] > 0) {
done = 0;
if (remaining_time[i] > quantum) {
current_time += quantum;
remaining_time[i] -= quantum;
} else {
current_time += remaining_time[i];
wait_time[i] = current_time - burst_time[i];
remaining_time[i] = 0;
}
}
}
if (done == 1)
break;
}

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


turnaround_time[i] = burst_time[i] + wait_time[i];
total_wait += wait_time[i];
total_turnaround += turnaround_time[i];

5
21BME0595

}
printf("Process\tArrival Time\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", i + 1, arrival_time[i], burst_time[i], wait_time[i],
turnaround_time[i]);
}
printf("Average Waiting Time: %.2f\n", (float)total_wait / n);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
}

int main() {
int n = 4;
int arrival_time[] = {0, 1, 2, 3};
int burst_time[] = {8, 4, 9, 5};
int quantum = 3;
round_robin_scheduling(n, arrival_time, burst_time, quantum);
return 0;
}
Output –

6
21BME0595

d. Priority Scheduling
Code –
#include <stdio.h>
void priority_scheduling(int n, int arrival_time[], int burst_time[], int priority[]) {
int wait_time[n], turnaround_time[n], total_wait = 0, total_turnaround = 0;
int completed = 0, current_time = 0, min_index;
int is_completed[n];

for (int i = 0; i < n; i++) is_completed[i] = 0;

while (completed != n) {
min_index = -1;
int highest_priority = 1e9;

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


if (arrival_time[i] <= current_time && !is_completed[i] && priority[i] <
highest_priority) {
highest_priority = priority[i];
min_index = i;
}
}

if (min_index == -1) {
current_time++;
} else {
wait_time[min_index] = current_time - arrival_time[min_index];
current_time += burst_time[min_index];
turnaround_time[min_index] = wait_time[min_index] + burst_time[min_index];
is_completed[min_index] = 1;
completed++;
total_wait += wait_time[min_index];
total_turnaround += turnaround_time[min_index];

7
21BME0595

}
}

printf("Process\tArrival Time\tBurst Time\tPriority\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\t\t%d\n", i + 1, arrival_time[i], burst_time[i],
priority[i], wait_time[i], turnaround_time[i]);
}

printf("Average Waiting Time: %.2f\n", (float)total_wait / n);


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround / n);
}

int main() {
int n = 4;
int arrival_time[] = {0, 1, 2, 3};
int burst_time[] = {8, 4, 9, 5};
int priority[] = {2, 1, 4, 3};
priority_scheduling(n, arrival_time, burst_time, priority);
return 0;
}

Output –

8
21BME0595

2. Simulation of Banker s algorithm to check whether the given system is in safe


state or not.
Code –
#include <stdio.h>
#include <stdbool.h>

#define P 5
#define R 3

void calculate_need(int need[P][R], int max[P][R], int allot[P][R]) {


for (int i = 0; i < P; i++) {
for (int j = 0; j < R; j++) {
need[i][j] = max[i][j] - allot[i][j];
}

}
}

bool is_safe(int processes[], int avail[], int max[][R], int allot[][R]) {


int need[P][R];
calculate_need(need, max, allot);

bool finish[P] = {0};


int safe_seq[P];
int work[R];

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


work[i] = avail[i];
}

int count = 0;
while (count < P) {

9
21BME0595

bool found = false;


for (int p = 0; p < P; p++) {
if (finish[p] == 0) {
int j;
for (j = 0; j < R; j++) {
if (need[p][j] > work[j]) break;
}
if (j == R) {
for (int k = 0; k < R; k++) work[k] += allot[p][k];
safe_seq[count++] = p;
finish[p] = 1;

found = true;
}
}
}
if (!found) {
printf("System is not in a safe state.\n");
return false;
}
}

printf("System is in a safe state.\nSafe sequence is: ");


for (int i = 0; i < P; i++) {
printf("%d ", safe_seq[i]);
}
printf("\n");
return true;
}

int main() {
int processes[] = {0, 1, 2, 3, 4};

10
21BME0595

int avail[] = {3, 3, 2};


int max[][R] = { {7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3} };
int allot[][R] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2} };

is_safe(processes, avail, max, allot);


return 0;
}
Output-

11

You might also like