0% found this document useful (0 votes)
24 views48 pages

Avichal Temp OS final

Project
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)
24 views48 pages

Avichal Temp OS final

Project
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/ 48

PRANVEER SINGH INSTITUTE OF TECHNOLOGY,

KANPUR

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Even Semester 2023-24

B. Tech.- Second Year

Semester- IV

Lab File
Operating
System (BCS401)

Submitted To: Submitted By:


Faculty Name : Mrs. Sonam Dubey Name : Avichal Agarwal
Designation : Assistant Professor Roll No. : 2201641640015
Section : AIML II A
Table of Contents
 Vision and Mission Statements of the Institute

 Vision and Mission Statements of the Department

 PEOs, POs, PSOs of the Department

 Course Objective and Outcomes

 List of Experiments

 Index
Department Vision Statement
To be a recognized Department of Computer Science & Engineering that produces versatile computer
engineers, capable of adapting to the changing needs of computer and related industry.

Department Mission Statements


The mission of the Department of Computer Science and Engineering is:

i. To provide broad based quality education with knowledge and attitude to succeed in Computer
Science & Engineering careers.

ii. To prepare students for emerging trends in computer and related industry.

iii. To develop competence in students by providing them skills and aptitude to foster culture of
continuous and lifelong learning.

iv. To develop practicing engineers who investigate research, design, and find workable solutions to
complex engineering problems with awareness & concern for society as well as environment.

Program Educational Objectives (PEOs)


i. The graduates will be efficient leading professionals with knowledge of computer science &
engineering discipline that enables them to pursue higher education and/or successful careers in
various domains.

ii. Graduates will possess capability of designing successful innovative solutions to real life problems
that are technically sound, economically viable and socially acceptable.

iii. Graduates will be competent team leaders, effective communicators and capable of working in
multidisciplinary teams following ethical values.

iv. The graduates will be capable of adapting to new technologies/tools and constantly upgrading
their knowledge and skills with an attitude for lifelong learning
Department Program Outcomes (POs)
The students of Computer Science and Engineering Department will be able:

1. Engineering knowledge: Apply the knowledge of mathematics, science, Computer Science &
Engineering fundamentals, and an engineering specialization to the solution of complex
engineering problems.

2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and Computer Science & Engineering sciences.

3. Design/development of solutions: Design solutions for complex Computer Science &


Engineering problems and design system components or processes that meet the specified needs
with appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.

4. Investigation: Use research-based knowledge and research methods including design of


experiments, analysis and interpretation of data, and synthesis of the information to provide valid
conclusions.

5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modelling to complex Computer Science &
Engineering activities with an understanding of the limitations.

6. The Engineering and Society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant
to the professional engineering practice in the field of Computer Science and Engineering.

7. Environment and sustainability: Understand the impact of the professional Computer Science
& Engineering solutions in societal and environmental contexts, and demonstrate the knowledge
of, and need for sustainable development.

8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the Computer Science & Engineering practice.

9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.

10. Communication: Communicate effectively on complex Computer Science & Engineering


activities with the engineering community and with society at large, such as, being able to
comprehend and write effective reports and design documentation, make effective presentations,
and give and receive clear instructions.

11. Project management and finance: Demonstrate knowledge and understanding of the
Computer Science & Engineering and management principles and apply these to one’s own work,
as a member and leader in a team, to manage projects and in multidisciplinary environments.

12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
Department Program Specific Outcomes (PSOs)
The students will be able to:

1. Use algorithms, data structures/management, software design, concepts of programming


languages and computer organization and architecture.

2. Understand the processes that support the delivery and management of information systems
within a specific application environment.

Course Outcomes
*Level of Bloom’s Level to be
*Level of Bloom’s Level to be met
Taxonomy met
Taxonomy
L1: Remember 1 L2: Understand 2
L3: Apply 3 L4: Analyze 4
L5: Evaluate 5 L6: Create 6

CO Number Course Outcomes


BCS-401.1 To be able to practice [L3: Apply] basic UNIX Commands
To be able to experiment with [L3: Apply] CPU scheduling algorithms, page
BCS-401.2 replacement and memory management algorithms.
List of Experiments

S. No. Topic COs


1 Unix commands practice. CO1
2 Implementation of CPU Scheduling Algorithms CO2
a) FCFS b) SJF
3 Implementation of CPU Scheduling Algorithms CO2
a) Priority (Non-Pre-emptive)
b) Multi-level Queue (Level-1 Time Quantum = 2 ms, Level-2 Time
Quantum = 4 ms, Level-3 FCFS)
4 Implementation of resource allocation graph (RAG). CO2
5 Implementation of Banker’s algorithm for Deadlock Avoidance. CO2
6 Conversion of resource allocation graph (RAG) to wait for graph (WFG) CO2
for each type of method used for storing graph.
7 Implementation of contiguous allocation techniques: CO2
a) Worst-Fit b) Best- Fit c) First- Fit
8 Implement the solution for Bounded Buffer (producer- consumer) CO2
problem using inter process communication
techniques- Semaphores
9 Implement the solutions for Readers-Writers problem using inter CO2
process communication technique –Semaphore
10 Implementation of System calls. CO2
INDEX
Experiment Lab Experiment Date of Date of Marks Faculty
No Experiment Submission Signature
Lecture zero
(about Cos and study of
hardware and software
requirement on OS)
Unix commands Practice.
Experiment 1
Unix commands Practice

Implementation of CPU
Experiment 2
Scheduling Algorithms
a) FCFS b) SJF
Implementation of CPU Scheduling
Experiment 3 Algorithms
c) Priority (Non-Pre-emptive)
d) Multi-level Queue (Level-1 Time
Quantum = 2 ms, Level-2 Time
Quantum = 4 ms, Level-3 FCFS)

Experiment 4
Implementation of resource
allocation graph (RAG).

Implementation of Banker’s
Experiment 5
algorithm for Deadlock
Avoidance.
Conversion of resource allocation
Experiment 6 graph (RAG) to wait for graph (WFG)
for each type of method used for
storing graph.

Implementation of contiguous
Experiment 7 allocation techniques:
a) Worst-Fit b) Best- Fit c) First- Fit

Implement the solution for Bounded


Experiment 8 Buffer (producer- consumer)
problem using inter process
communication techniques- Semaphores

Implement the solutions for Readers-


Experiment Writers problem using inter process
9 communication technique –Semaphore

Experiment Implementation of System calls.


10
Experiment 1
Objective: Study of hardware and basic commands of different operating systems (UNIX, LINUX,
WINDOWS)

Theory:
COMMAND:
1. Date Command:
This command is used to display the current data and time.

Syntax: $date

Output:

2. Calender Command:
This command is used to display the calendar of the year or the particular month of calendar year.

Syntax:
a. $cal<year>
b. $cal<month><year>
Here the first syntax gives the entire calendar for given year & the second Syntax gives the calendar
of reserved month of that year.

Output:
a.

b.
3. Echo Command:
This command is used to print the arguments on the screen.

Syntax: $echo <text>

Output:

Multi line echo command:


To have the output in the same line, the following commands can be used.

Syntax: $echo <text\>texts

Output:

To have the output in different line, the following command can be used.

Syntax: $echo “text


>line2
>line3”

Output:

4. ’who’ Command:
It is used to display who are the users connected to our computer currently.

Syntax: $who – option‟s

Options:
H–Display the output with headers.

Output:

Options:
b–Display the last booting date or time or when the system was lastly rebooted.
Output:

5. ’who am i’ Command:
Display the details of the current working directory.
Syntax: $who am i

Output:

6. ’tty’ Command:
It will display the terminal name.

Syntax: $tty

Output:

7. ’CLEAR’ Command:
It is used to clear the screen.

Syntax: $clear

Output:

8. ’MAN’ Command:
It helps us to know about the particular command and its options & working. It is like
„help‟ command in windows.

Syntax: $man <command name>

Output:

9. MANIPULATION Command:
It is used to manipulate the screen.

Syntax: $tput<argument>
Arguments:

1. Clear – to clear the screen.


Output:

2. Longname – Display the complete name of the terminal.

Output:

3. SMSO – background become white and foreground become black color.

Output:

4. rmso – background become black and foreground becomes white color.

Output:

5. Cols – Display the number of columns in our terminals.


Output:

10.LIST Command:
It is used to list all the contents in the current working directory.

Syntax: $ ls–options <arguments>

If the command does not contain any argument means it is working in the Current directory.

Options:

a– used to list all the files including the hidden files. c– list all the files column wise.

Output:

Options:
c– list all the files column wise.

Output:

Options:
d- list all the directories.

Output:

Options:
m- list the files separated by commas.

Output:

Options:
p- list files include „/‟ to all the directories.

Output:

Options:
r- list the files in reverse alphabetical order.

Output:

Options:
f- list the files based on the list modification date.

Output:

Options:
x-list in column wise sorted order.

Output:

DIRECTORY RELATED COMMANDS:


1. Present Working Directory Command:
To print the complete path of the current working directory.

Syntax: $pwd

Output:

2. MKDIR Command:
To create or make a new directory in a current directory.

Syntax: $mkdir<directory name>

Output:
3. CD Command:
To change or move the directory to mentioned directory.

Syntax: $cd <directory name>

Output:

4. RMDIR Command:
To remove a directory in the current directory & not the current directory itself.

Syntax: $rmdir<directory name>

Output:

FILE RELATED COMMANDS:

1. CREATE A FILE:
To create a new file in the current directory we use CAT command.

Syntax:

$cat > filename.


The > symbol is redirectory we use cat command.

Output:

2. DISPLAY A FILE:
To display the content of file mentioned we use CAT command without „>‟ operator.

Syntax:

$cat <filename.
Options –s = to neglect the warning /error message.
Output:

3. COPYING CONTENTS:

To copy the content of one file with another. If file doesnot exist, a new file is
created and if the file exists with some data then it is overwritten.

Syntax:

$ cat <filename source>>><destination filename>

Options:
-n content of file with numbers included with blank lines.

Output:

Syntax:
$cat –n <filename>

Output:

4. SORTING A FILE:

To sort the contents in alphabetical order or in reverse order.

Syntax:

$sort <filename >


Output:

Option:

$ sort –r <filename>
Output:

5. COPYING CONTENTS FROM ONE FILE TO ANOTHER:


To copy the contents from source to destination file. so that both contents are same.

Syntax:
$cp<source filename><destination filename>

Output:

Syntax:
$cp<source filename path ><destination filename path>

Output:

6. MOVE Command:

To completely move the contents from source file to destination file and to remove the source file.

Syntax:
$ mv <source filename><destination filename>

Output:
7. REMOVE Command:

To permanently remove the file we use this command.

Syntax:
$rm<filename>

Output:

8. WORD Command:

To list the content count of no of lines, words, characters.

Syntax:

$wc<filename>

Output:

Options:

-c – to display no of characters.

Output:

Options:

-l – to display only the lines.


Output:

Options:
-w – to display the no. of words.

Output:

9. LINE PRINTER:

To print the line through the printer, we use lp command.

Syntax:
$lp<filename>

Output:
10. PAGE Command:
This command is used to display the contents of the file page wise & next page can
be viewed by pressing the enter key.
Syntax:
$ more<filename> –displays the content of example.txt page-wise. Press Enter to move to
the next line, Space to move to the next page, and `q` to quit.

Output:

Syntax:

$ less<filename> –displays the content of example.txt page-wise. Use arrow keys to


navigate, and `q` to quit.

Output:

11. FILTERS AND PIPES

HEAD: It is used to display the top ten lines of file.

Syntax: $head<filename>

Output:

TAIL: This command is used to display the last ten lines of file.

Syntax: $tail<filename>

Output:

GREP: This command is used to search and print the specified patterns from the file.

Syntax: $grep [option] pattern <filename>

Options: -i: Ignore case distinctions in both the pattern and the input files.

Output:

Options: -v: Invert the sense of matching, to select non-matching lines.

Output:

Options: -c: Count the number of matching lines and print the count instead of the lines.

Output:
Options: -l: Print only the names of files with matching lines (once per file).

Output:

Options: -n: Prefix each line of output with the line number within its input file.

Output:

Options: -r or -R: Read all files under each directory, recursively.

Output:

Options: -w: Match only whole words.

Output:

Options: -x: Match only whole lines.

Output:

SORT: This command is used to sort the data in some order.

Syntax: $sort<filename>

Output:

PIPE: It is a mechanism by which the output of one command can be channeled into the input of another command.

Syntax: $who | wc -1

Output:

TR: The tr filter is used to translate one set of characters from the standard inputs to another.

Syntax: $tr “[a-z]” “[A-Z]”

Output:
Experiment 2
Implementation of CPU Scheduling Algorithms
a) FCFS b) SJF

a.) FCFS

#include <stdio.h>

struct Process {
int id;
int arrival_time;
int burst_time;
int
waiting_time;
int turnaround_time;
};

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


{ int total_waiting_time = 0;
int total_turnaround_time = 0;

processes[0].waiting_time =
0; for (int i = 1; i < n; i++) {
processes[i].waiting_time = processes[i-1].waiting_time + processes[i-1].burst_time;
}

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


processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
}

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


total_waiting_time += processes[i].waiting_time;
total_turnaround_time +=
processes[i].turnaround_time;
}

printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\


n"); for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time,
processes[i].turnaround_time);
printf("\n");
}
printf("\nAverage Waiting Time: %.2f\n", (float)total_waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}

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].id = i +
1;
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
}

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


for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j+1].arrival_time)
{ struct Process temp = processes[j];
processes[j] =
processes[j+1];
processes[j+1] = temp;
}
}
}

calculateTimes(processes, n);

return 0;
}
OUTPUT:

Enter the number of processes: 3


Enter arrival time and burst time for process 1: 0 4
arrival time and burst time for process 2: 1 3
arrival time and burst time for process 3: 2 1
Process Arrival Time Burst Time Waiting Time Turnaround Time
P1 0 4 0 4
P2 1 3 4 7
P3 2 1 7 8

Average Waiting Time: 3.67


Average Turnaround Time: 6.33
b.) SJF

#include <stdio.h>

struct Process
{ int id;
int arrival_time;
int burst_time;
int
waiting_time;
int turnaround_time;
int completion_time;
};

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


{ int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed = 0, current_time = 0;

while (completed != n)
{ int idx = -1;
int min_burst = 99999;

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


if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0)
{ if (processes[i].burst_time < min_burst) {
min_burst =
processes[i].burst_time; idx = i;
}
if (processes[i].burst_time == min_burst) {
if (processes[i].arrival_time < processes[idx].arrival_time)
{ idx = i;
}
}
}
}

if (idx != -1) {
processes[idx].completion_time = current_time + processes[idx].burst_time;
processes[idx].turnaround_time = processes[idx].completion_time -
processes[idx].arrival_time; processes[idx].waiting_time = processes[idx].turnaround_time -
processes[idx].burst_time;
total_waiting_time += processes[idx].waiting_time;
total_turnaround_time += processes[idx].turnaround_time;

completed++;
current_time = processes[idx].completion_time;
} else {
current_time++;
}
}

printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\


n"); for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\n\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time,
processes[i].turnaround_time);
}

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


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

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].id = i +
1;
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time,
&processes[i].burst_time); processes[i].completion_time = 0;
}

calculateTimes(processes, n);

return 0;
}
OUTPUT:

Enter the number of processes: 3


Enter arrival time and burst time for process 1: 0 4
Enter arrival time and burst time for process 2: 1 3
Enter arrival time and burst time for process 3: 2 1

Process Arrival Time Burst Time Waiting Time Turnaround Time


P1 0 4 3 7
P2 1 3 2 5
P3 2 1 0 1

Average Waiting Time: 1.67


Average Turnaround Time: 4.33
Experiment 3
Implementation of CPU Scheduling Algorithms
a) Priority (Non-Pre-emptive)

b) Multi-level Queue (Level-1 Time Quantum = 2 ms, Level-2 Time Quantum = 4 ms, Level-
3 FCFS)

a) Priority (Non-Pre-emptive)

#include <stdio.h>

struct Process
{ int id;
int
arrival_time;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
int completion_time;
};

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


{ int total_waiting_time = 0;
int total_turnaround_time = 0;
int completed = 0, current_time = 0;

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

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


if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0)
{ if (processes[i].priority > highest_priority) {
highest_priority =
processes[i].priority; idx = i;
}
if (processes[i].priority == highest_priority) {
if (processes[i].arrival_time < processes[idx].arrival_time)
{ idx = i;
}
}
}
}

if (idx != -1) {
processes[idx].completion_time = current_time +
processes[idx].burst_time; processes[idx].turnaround_time =
processes[idx].completion_time -
processes[idx].arrival_time;
processes[idx].waiting_time = processes[idx].turnaround_time - processes[idx].burst_time;
total_waiting_time += processes[idx].waiting_time;
total_turnaround_time +=
processes[idx].turnaround_time;
completed++;
current_time = processes[idx].completion_time;
} else {
current_time++;
}
}

printf("Process\tArrival Time\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\


n"); for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time, processes[i].priority,
processes[i].waiting_time, processes[i].turnaround_time);
}

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


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

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].id = i + 1;
printf("Enter arrival time, burst time and priority for process %d: ", i + 1);
scanf("%d %d %d", &processes[i].arrival_time, &processes[i].burst_time,
&processes[i].priority);
processes[i].completion_time = 0; // Initially, no process is completed
}

calculateTimes(processes, n);

return 0;
}
OUTPUT:

Enter the number of processes: 3


Enter arrival time, burst time and priority for process 1: 0 4 2
Enter arrival time, burst time and priority for process 2: 1 3 1
Enter arrival time, burst time and priority for process 3: 2 1 3

Process Arrival Time Burst Time Priority Waiting Time Turnaround Time
P1 0 4 2 3 7
P2 1 3 1 6 9
P3 2 1 3 0 1

Average Waiting Time: 3.00


Average Turnaround Time: 5.67
b) Multi-level Queue (Level-1 Time Quantum = 2 ms, Level-2 Time Quantum =
4ms, Level-3 FCFS)

#include <stdio.h>
#include <stdbool.h>
#define MAX_PROCESSES
100 struct Process {
int id;
int
arrival_time;
int burst_time;
int
remaining_time;
int waiting_time;
int turnaround_time;
int completion_time;
int queue_level;
};

void roundRobin(struct Process processes[], int n, int time_quantum, int start_time, int end_time)
{ int time = start_time;
bool done;

do {
done = true;
for (int i = 0; i < n; i++) {
if (processes[i].remaining_time > 0 && processes[i].queue_level == 1)
{ done = false;
if (processes[i].remaining_time > time_quantum)
{ time += time_quantum;
processes[i].remaining_time -= time_quantum;
} else {
time +=
processes[i].remaining_time;
processes[i].completion_time = time;
processes[i].remaining_time = 0;
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
}
}
}
} while (!done && time <= end_time);
}
void fcfs(struct Process processes[], int n, int start_time)
{ int time = start_time;

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


if (processes[i].queue_level == 3) {
if (time < processes[i].arrival_time)
{ time = processes[i].arrival_time;
}
time += processes[i].burst_time;
processes[i].completion_time = time;
processes[i].turnaround_time = processes[i].completion_time -
processes[i].arrival_time; processes[i].waiting_time = processes[i].turnaround_time -
processes[i].burst_time;
}
}
}
void calculateTimes(struct Process processes[], int n)
{ int total_waiting_time = 0;
int total_turnaround_time = 0;

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


total_waiting_time += processes[i].waiting_time;
total_turnaround_time +=
processes[i].turnaround_time;
}
printf("Process\tArrival Time\tBurst Time\tQueue Level\tWaiting Time\tTurnaround Time\
n"); for (int i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id,
processes[i].arrival_time, processes[i].burst_time, processes[i].queue_level,
processes[i].waiting_time, processes[i].turnaround_time);
}

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


printf("Average Turnaround Time: %.2f\n", (float)total_turnaround_time / n);
}
int main()
{ int n;
printf("Enter the number of processes:
"); scanf("%d", &n);

struct Process processes[MAX_PROCESSES];

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


{ processes[i].id = i +
1;
printf("Enter arrival time, burst time and queue level (1-3) for process %d: ", i +
1); scanf("%d %d %d", &processes[i].arrival_time, &processes[i].burst_time,
&processes[i].queue_level);
processes[i].remaining_time = processes[i].burst_time;
}

roundRobin(processes, n, 2, 0, MAX_PROCESSES);
roundRobin(processes, n, 4, 0, MAX_PROCESSES);

fcfs(processes, n, 0);

calculateTimes(processes,
n); return 0;
}
OUTPUT:

Enter the number of processes: 3


Enter arrival time, burst time and queue level (1-3) for process 1: 0 5 1
Enter arrival time, burst time and queue level (1-3) for process 2: 2 3 2
Enter arrival time, burst time and queue level (1-3) for process 3: 4 1 3

Process Arrival Time Burst Time Queue Level Waiting Time Turnaround Time
P1 0 5 1 2 7
P2 2 3 2 4 7
P3 4 1 3 0 1

Average Waiting Time: 2.00


Average Turnaround Time: 5.00
Experiment 4
Implementation of resource allocation graph (RAG)

#include

<stdio.h> #define

MAX 100

void printRAG(int allocation[MAX][MAX], int request[MAX][MAX], int processes, int resources)


{ printf("Resource Allocation Graph (RAG):\n\n");
printf("Allocation Matrix:\n");
for (int i = 0; i < processes; i++)
{
for (int j = 0; j < resources; j++)
{ printf("%d ", allocation[i]
[j]);
}
printf("\n");
}

printf("\nRequest Matrix:\n");
for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++)
{
printf("%d ", request[i][j]);
}
printf("\n");
}
}
int main() {
int processes, resources;
int allocation[MAX]
[MAX]; int request[MAX]
[MAX];

printf("Enter the number of processes:


"); scanf("%d", &processes);

printf("Enter the number of resources:


"); scanf("%d", &resources);

printf("Enter the allocation matrix:\


n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++)
{ scanf("%d", &allocation[i]
[j]);
}
}
printf("Enter the request matrix:\
n"); for (int i = 0; i < processes; i++)
{
for (int j = 0; j < resources; j++)
{ scanf("%d", &request[i][j]);
}
}
printRAG(allocation, request, processes,
resources); return 0;
}
OUTPUT:

Enter the number of processes: 3


Enter the number of resources: 3
Enter the allocation matrix:
100
010
101
Enter the request matrix:
010
001
110

Resource Allocation Graph

(RAG): Allocation Matrix:


100
010
101

Request
Matrix:
010
001
110
Experiment 5
Implementation of Banker’s algorithm for Deadlock Avoidance.

#include <stdio.h>
#include <stdbool.h>

#define MAX_PROCESSES 10
#define MAX_RESOURCES 10

void calculateNeed(int need[MAX_PROCESSES][MAX_RESOURCES],


int max[MAX_PROCESSES][MAX_RESOURCES], int
allocation[MAX_PROCESSES][MAX_RESOURCES], int processes, int resources)
{ for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++) { need[i]
[j] = max[i][j] - allocation[i][j];
}
}
}

bool isSafe(int processes, int resources, int available[MAX_RESOURCES],


int max[MAX_PROCESSES][MAX_RESOURCES], int
allocation[MAX_PROCESSES][MAX_RESOURCES]) {
int need[MAX_PROCESSES][MAX_RESOURCES];
calculateNeed(need, max, allocation, processes, resources);

bool finished[MAX_PROCESSES] =
{0}; int work[MAX_RESOURCES];
for (int i = 0; i < resources; i++)
{ work[i] = available[i];
}

while (true) {
bool found = false;
for (int i = 0; i < processes; i++)
{ if (!finished[i]) {
bool canAllocate = true;
for (int j = 0; j < resources; j++)
{ if (need[i][j] > work[j]) {
canAllocate =
false; break;
}
}

if (canAllocate) {
for (int j = 0; j < resources; j++)
{ work[j] += allocation[i][j];
}
finished[i] = true;
found = true;
}
}
}

if (!found) {
for (int i = 0; i < processes; i++)
{ if (!finished[i]) {
return false;
}
}
return true;
}
}
}

int main() {
int processes, resources;

printf("Enter the number of processes:


"); scanf("%d", &processes);

printf("Enter the number of resources:


"); scanf("%d", &resources);

int allocation[MAX_PROCESSES]
[MAX_RESOURCES]; int max[MAX_PROCESSES]
[MAX_RESOURCES];
int available[MAX_RESOURCES];

printf("Enter the Allocation Matrix:\


n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++)
{ scanf("%d", &allocation[i]
[j]);
}
}

printf("Enter the Maximum Matrix:\


n"); for (int i = 0; i < processes; i++) {
for (int j = 0; j < resources; j++)
{ scanf("%d", &max[i][j]);
}
}
printf("Enter the Available Resources:\
n"); for (int i = 0; i < resources; i++) {
scanf("%d", &available[i]);
}
if (isSafe(processes, resources, available, max, allocation))
{ printf("The system is in a safe state.\n");
} else {
printf("The system is NOT in a safe state.\n");
}
return 0;
}
OUTPUT:

Enter the number of processes: 5


Enter the number of resources: 3
Enter the Allocation Matrix:
010
200
302
211
002
Enter the Maximum Matrix:
753
322
902
222
433
Enter the Available
Resources:3 3 2

The system is in a safe state.


Experiment 6
Conversion of resource allocation graph (RAG) to wait for graph (WFG) for each type of method
used for storing graph

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

#define MAX_PROCESSES 10
#define MAX_RESOURCES 10

void convertToWFGMatrix(int rag[MAX_PROCESSES + MAX_RESOURCES]


[MAX_PROCESSES + MAX_RESOURCES], int
wfg[MAX_PROCESSES][MAX_PROCESSES], int processes, int resources) {

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


for (int j = 0; j < processes; j++)
{ wfg[i][j] = 0;
}
}

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


for (int j = processes; j < processes + resources; j++)
{ if (rag[i][j] == 1) {
for (int k = 0; k < processes; k++)
{ if (rag[j][k] == 1) {
wfg[i][k] = 1;
}
}
}
}
}
}

void printMatrix(int matrix[MAX_PROCESSES][MAX_PROCESSES], int size)


{ for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

typedef struct Node


{ int process;
struct Node* next;
} Node;

void addEdge(Node* adjList[], int src, int dest) {


Node* newNode =
(Node*)malloc(sizeof(Node));
newNode->process = dest;
newNode->next =
adjList[src]; adjList[src] =
newNode;
}

void convertToWFGList(Node* rag[], Node* wfg[], int processes, int resources) {


for (int i = 0; i < processes; i++) {
wfg[i] = NULL;
}

for (int i = 0; i < processes; i++)


{ Node* temp = rag[i];
while (temp != NULL) {
int resource = temp->process;
Node* temp2 =
rag[resource]; while (temp2 !
= NULL) {
int process = temp2-
>process; addEdge(wfg, i,
process); temp2 = temp2-
>next;
}
temp = temp->next;
}
}
}

void printList(Node* adjList[], int size)


{ for (int i = 0; i < size; i++) {
printf("P%d: ", i);
Node* temp =
adjList[i]; while (temp !
= NULL) {
printf("P%d -> ", temp-
>process); temp = temp->next;
}
printf("NULL\n");
}
}

int main() {
int processes, resources;

printf("Enter the number of processes:


"); scanf("%d", &processes);

printf("Enter the number of resources:


"); scanf("%d", &resources);

int ragMatrix[MAX_PROCESSES + MAX_RESOURCES][MAX_PROCESSES


+ MAX_RESOURCES];
int wfgMatrix[MAX_PROCESSES][MAX_PROCESSES];

printf("Enter the RAG adjacency matrix (%d x %d):\n", processes + resources, processes
+ resources);
for (int i = 0; i < processes + resources; i++) {
for (int j = 0; j < processes + resources; j++)
{
scanf("%d", &ragMatrix[i][j]);
}
}

convertToWFGMatrix(ragMatrix, wfgMatrix, processes, resources);

printf("\nWFG Adjacency Matrix:\


n"); printMatrix(wfgMatrix,
processes);

Node* ragList[MAX_PROCESSES + MAX_RESOURCES] = {NULL};


Node* wfgList[MAX_PROCESSES] = {NULL};

printf("\nEnter the RAG adjacency list:\n");


for (int i = 0; i < processes + resources; i++) {
int numEdges;
printf("Enter number of edges from node %d: ",
i); scanf("%d", &numEdges);
for (int j = 0; j < numEdges; j++)
{ int dest;
scanf("%d", &dest);
addEdge(ragList, i, dest);
}
}

convertToWFGList(ragList, wfgList, processes, resources);

printf("\nWFG Adjacency List:\


n"); printList(wfgList, processes);

return 0;
}
OUTPUT:

Enter the number of processes:


3 Enter the number of resources:
2
Enter the RAG adjacency matrix (5 x
5): 0 1 0 0 0
00000
00010
00000
00000

WFG Adjacency Matrix:


000
000
010

Enter the RAG adjacency list:


Enter number of edges from node 0:
11 Enter number of edges from node
1: 0 Enter number of edges from node
2: 13 Enter number of edges from
node 3: 0 Enter number of edges from
node 4: 0

WFG Adjacency
List: P0: NULL
P1: NULL
P2: P1 -> NULL
Experiment 7
Implementation of contiguous allocation techniques:
a) Worst-Fit b) Best- Fit c) First- Fit

a) Worst-Fit

#include <stdio.h>

void worstFit(int blockSize[], int m, int processSize[], int n)


{ int allocation[n];
for (int i = 0; i < n; i++)
{ allocation[i] = -1;
}

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


{ int wstIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (wstIdx == -1 || blockSize[j] > blockSize[wstIdx])
{ wstIdx = j;
}
}
}
if (wstIdx != -1) {
allocation[i] =
wstIdx;
blockSize[wstIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\


n"); for (int i = 0; i < n; i++) {
printf(" %d \t\t %d \t\t", i + 1,
processSize[i]); if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}

int main() { int m, n;

printf("Enter the number of memory blocks:


"); scanf("%d", &m);

int blockSize[m];
printf("Enter sizes of memory blocks:\
n"); for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}

printf("Enter the number of processes:


"); scanf("%d", &n);

int processSize[n];
printf("Enter sizes of processes:\
n"); for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}

worstFit(blockSize, m, processSize, n);

return 0;
}
OUTPUT:

Enter the number of memory blocks:


5 Enter sizes of memory blocks:
100
500
200
300
600
Enter the number of processes:
4 Enter sizes of processes:
212
417
112
426

Process No. Process Size Block no.


1 212 5
2 417 2
3 112 5
4 426 Not Allocated
b) Best-Fit

#include <stdio.h>

void bestFit(int blockSize[], int m, int processSize[], int n)


{ int allocation[n];
for (int i = 0; i < n; i++)
{ allocation[i] = -1;
}

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


{ int bestIdx = -1;
for (int j = 0; j < m; j++) {
if (blockSize[j] >= processSize[i]) {
if (bestIdx == -1 || blockSize[j] < blockSize[bestIdx])
{ bestIdx = j;
}
}
}
if (bestIdx != -1) {
allocation[i] =
bestIdx;
blockSize[bestIdx] -= processSize[i];
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\


n"); for (int i = 0; i < n; i++) {
printf(" %d \t\t %d \t\t", i + 1,
processSize[i]); if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}int main() { int m, n;

printf("Enter the number of memory blocks:


"); scanf("%d", &m);

int blockSize[m];
printf("Enter sizes of memory blocks:\
n"); for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}
printf("Enter the number of processes:
"); scanf("%d", &n);

int processSize[n];
printf("Enter sizes of processes:\
n"); for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}
bestFit(blockSize, m, processSize, n);
return 0;
}
OUTPUT:

Enter the number of memory blocks: 5 Enter sizes of memory blocks:


100
500
200
300
600
Enter the number of processes:
4 Enter sizes of processes:
212
417
112
426

Process No. Process Size Block no.


1 212 4
2 417 2
3 112 3
4 426 5
c) First-Fit

#include <stdio.h>
void firstFit(int blockSize[], int m, int processSize[], int n) { int allocation[n];
for (int i = 0; i < n; i++)
{ allocation[i] = -1;
}

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


for (int j = 0; j < m; j++)
{
if (blockSize[j] >= processSize[i])
{ allocation[i] = j;
blockSize[j] -=
processSize[i]; break;
}
}
}

printf("\nProcess No.\tProcess Size\tBlock no.\


n"); for (int i = 0; i < n; i++) {
printf(" %d \t\t %d \t\t", i + 1,
processSize[i]); if (allocation[i] != -1) {
printf("%d\n", allocation[i] + 1);
} else {
printf("Not Allocated\n");
}
}
}

int main()
{ int m,
n;

printf("Enter the number of memory blocks:


"); scanf("%d", &m);

int blockSize[m];
printf("Enter sizes of memory blocks:\
n"); for (int i = 0; i < m; i++) {
scanf("%d", &blockSize[i]);
}

printf("Enter the number of processes:


"); scanf("%d", &n);

int processSize[n];
printf("Enter sizes of processes:\
n"); for (int i = 0; i < n; i++) {
scanf("%d", &processSize[i]);
}

firstFit(blockSize, m, processSize, n);

return 0;
}
OUTPUT:

Enter the number of memory blocks: 5 Enter sizes of memory blocks:


100
500
200
300
600
Enter the number of processes:
4 Enter sizes of processes:
212
417
112
426

Process No. Process Size Block no.


1 212 2
2 417 5
3 112 2
4 426 Not Allocated

You might also like