0% found this document useful (0 votes)
15 views9 pages

Shweta

The document presents an end-term project on operating system design principles by Shweta Pragyan G, focusing on CPU scheduling algorithms such as First Come First Served (FCFS) and Round Robin (RR), along with a Banker's algorithm for deadlock avoidance. It includes code implementations for both scheduling policies and the Banker's algorithm, detailing how to calculate average times and check system safety. The project is part of the Computer Science and Information Technology curriculum at Siksha 'O' Anusandhan University.

Uploaded by

ftwaryan
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)
15 views9 pages

Shweta

The document presents an end-term project on operating system design principles by Shweta Pragyan G, focusing on CPU scheduling algorithms such as First Come First Served (FCFS) and Round Robin (RR), along with a Banker's algorithm for deadlock avoidance. It includes code implementations for both scheduling policies and the Banker's algorithm, detailing how to calculate average times and check system safety. The project is part of the Computer Science and Information Technology curriculum at Siksha 'O' Anusandhan University.

Uploaded by

ftwaryan
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/ 9

End Term Project

On
Design Principles of Operating System (CSE 3249)
Submitted by

Name : SHWETA PRAGYAN G

Reg. No. : 2241007001

Branch : CSIT

Semester : 5th

Section : 05
Session : 2024-2025
Admission Batch : 2022

DEPARTMENT OF COMPUTER SCIENCE & INFORMATION

TECHNOLOGY FACULTY OF ENGINEERING &


TECHNOLOGY (ITER) SIKSHA ‘O’ ANUSANDHAN DEEMED

TO BE UNIVERSITY BHUBANESWAR, ODISHA - 751030

Name:SHWETA PRAGYAN G Regd No:2241007001


Project 1:
The C program provides an interface to the user to implement the following scheduling policies
as per the choice provided:

1. First Come First Served (FCFS)

2. Round Robin (RR)


Appropriate option needs to be chosen from a switch case-based menu driven program with an
option of “Exit from program” in case 5 and accordingly a scheduling policy will print the Gantt
chart and the average waiting time, average turnaround time and average response time. The
program will take Process ids, its arrival time, and its CPU burst time as input. For implementing
RR scheduling, user also needs to specify the time quantum. Assume that the process ids should
be unique for all processes. Each process consists of a single CPU burst (no I/O bursts), and
processes are listed in order of their arrival time. Further assume that an interrupted process
gets placed at the back of the Ready queue, and a newly arrived process gets placed at the back
of the Ready queue as well. The output should be displayed in a formatted way for clarity of
understanding and visual.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> struct Process
{
char id[10];
int arrival_time, burst_time, completion_time, turnaround_time, waiting_time,
response_time;
};
void printGanttChart(struct Process p[], int n)
{
printf("\nGantt Chart:\n|");
for (int i = 0; i < n; i++)
{
printf(" %s |", p[i].id);
}
printf("\n0");
for (int i = 0; i < n; i++)
{
printf(" %d", p[i].completion_time);
}
printf("\n");
}

Name:SHWETA PRAGYAN G Regd No:2241007001


void calculateAverages(struct Process p[], int n)
{
float total_tat = 0, total_wt = 0, total_rt = 0;
for (int i = 0; i < n; i++)
{
total_tat += p[i].turnaround_time;
total_wt += p[i].waiting_time;
total_rt += p[i].response_time;
}
printf("\nAverage Turnaround Time: %.2f\n", total_tat / n);
printf("Average Waiting Time: %.2f\n", total_wt / n);
printf("Average Response Time: %.2f\n", total_rt / n);
}
void FCFS(struct Process p[], int n)
{
int time = 0;
for (int i = 0; i < n; i++)
{
if (time < p[i].arrival_time)
{
time = p[i].arrival_time;
}
time += p[i].burst_time;
p[i].completion_time = time;
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
p[i].response_time = p[i].waiting_time;
}
printf("\nFCFS Scheduling:\n");
printf("PID\tAT\tBT\tCT\tTAT\tWT\tRT\n");
for (int i = 0; i < n; i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id,
p[i].arrival_time, p[i].burst_time,
p[i].completion_time, p[i].turnaround_time, p[i].waiting_time, p[i].response_time);
}
printGanttChart(p, n);
calculateAverages(p, n);
}
void RoundRobin(struct Process p[], int n, int quantum)
{

Name:SHWETA PRAGYAN G Regd No:2241007001


int time = 0, remaining[n], finished = 0;
for (int i = 0; i < n; i++)
{
remaining[i] = p[i].burst_time;
p[i].response_time = -1;
}
printf("\nGantt Chart:\n|");
while (finished < n)
{
for (int i = 0; i < n; i++)
{
if (remaining[i] > 0 && p[i].arrival_time <= time)
{
if (p[i].response_time == -1)
{
p[i].response_time = time - p[i].arrival_time;
}
printf(" %s |", p[i].id);

int exec_time = remaining[i] > quantum ? quantum :


remaining[i];
time += exec_time;
remaining[i] -= exec_time;
if (remaining[i] == 0)
{
finished++;
p[i].completion_time = time;
p[i].turnaround_time
= p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
}
}
}
if (finished < n && remaining[finished] > 0) time++;
}
printf("\n0");
for (int i = 0; i < n; i++)
{
printf(" %d", p[i].completion_time);
}
printf("\n");

Name:SHWETA PRAGYAN G Regd No:2241007001


printf("\nRound Robin Scheduling:\n");
printf("PID\tAT\tBT\tCT\tTAT\tWT\tRT\n");
for (int i = 0; i < n; i++)
{
printf("%s\t%d\t%d\t%d\t%d\t%d\t%d\n", p[i].id,
p[i].arrival_time, p[i].burst_time,
p[i].completion_time, p[i].turnaround_time, p[i].waiting_time, p[i].response_time); }
calculateAverages(p, n);
}
int main()
{
int n, choice, quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);

struct Process p[n];


printf("Enter Process ID, Arrival Time, and Burst Time:\n");
for (int i = 0; i < n; i++)
{
scanf("%s %d %d", p[i].id, &p[i].arrival_time, &p[i].burst_time);
}
while (1)
{
printf("\nCPU Scheduling Menu:\n1. FCFS\n2. Round Robin\n3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{ case 1:
FCFS(p, n); break;
case2:
printf("Enter Time Quantum: ");
scanf("%d", &quantum); RoundRobin(p,
n, quantum);
break;
case 3:
exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

Name:SHWETA PRAGYAN G Regd No:2241007001


Name:SHWETA PRAGYAN G Regd No:2241007001
Project 2:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes a state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.
CODE:
#include <stdio.h>
#include <stdbool.h>
#define P 5 #define R 4 void calculateNeed(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 isSafe(int processes[], int avail[], int max[][R], int allot[][R])
{
int need[P][R];
calculateNeed(need, max,
allot); bool finished[P] = {false}; int
safeSequence[P]; int
work[R]; for (int i = 0; i < R; i++) work[i]
= avail[i]; int count = 0;
while (count < P)
{
bool found = false;
for (int i = 0; i < P; i++)
{
if (!finished[i])
{
bool canAllocate = true;
for (int j = 0; j < R; j++)
if (need[i][j] > work[j])
{
canAllocate = false;
break;
}
if (canAllocate)
{
for (int j = 0; j < R; j++)

Name:SHWETA PRAGYAN G Regd No:2241007001


work[j] += allot[i][j];
safeSequence[count++] = processes[i];
finished[i] = true;
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 ", safeSequence[i]);
printf("\n");
return true;
}
int main()
{
int processes[] = {0, 1, 2, 3, 4}; int avail[] = {6, 7, 12, 12}; int
max[P][R] = {{0, 0, 1, 2},{2, 7, 5, 0},{6, 6, 5, 6},{4, 3, 5, 6}, {0, 6, 5,
2}}; int allot[P][R] = {{0, 0, 1, 2},{2, 0, 0, 0},{0, 0, 3, 4},{2, 3, 5, 4},{0, 3,
3, 2}}; isSafe(processes, avail, max, allot); return 0;
}

Name:SHWETA PRAGYAN G Regd No:2241007001


Name:SHWETA PRAGYAN G Regd No:2241007001

You might also like