0% found this document useful (0 votes)
13 views23 pages

Final Os

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

Final Os

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

PRANVEER SINGH INSTITUTE OF TECHNOLOGY, KANPUR

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Odd Semester 2023-24

B. Tech.- Second Year

Semester- IV

Lab File
Operating System
(BCS451)

Submitted To : Submitted By :
Faculty Name : Name :
Designation :
Roll No. : Section :
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 Taxonomy Level to be met
Taxonomy met
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- CO2
consumer)problem using inter process communication techniques-
Semaphores
9 Implement the solutions for Readers-Writers problem using inter process CO2
communication technique –Semaphore
10 Implementation of System calls. CO2
INDEX
S No Lab Experiment Date of Date of Marks Faculty
Experiment Submission Signature
0

10
Experiment 1:

Unix commands practice

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

void executeCommand(char *command) {


int result = system(command);
if (result == -1) {
perror("Error executing command");
}
}

int main() {
char command[256];
int choice;

while (1) {
printf("Unix Commands Practice:\n");
printf("1. List files (ls)\n");
printf("2. Print working directory (pwd)\n");
printf("3. Display current date and time (date)\n");
printf("4. Display the contents of a file (cat)\n");
printf("5. Create a directory (mkdir)\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Clear newline character from buffer

switch (choice) {
case 1:
executeCommand("ls");
break;
case 2:
executeCommand("pwd");
break;
case 3:
executeCommand("date");
break;
case 4:
printf("Enter filename: ");
fgets(command, sizeof(command), stdin);
command[strcspn(command, "\n")] = 0; // Remove newline
character
char catCommand[256];
snprintf(catCommand, sizeof(catCommand), "cat %s", command);
executeCommand(catCommand);
break;
case 5:
printf("Enter directory name: ");
fgets(command, sizeof(command), stdin);
command[strcspn(command, "\n")] = 0; // Remove newline
character
char mkdirCommand[256];
snprintf(mkdirCommand, sizeof(mkdirCommand), "mkdir %s",
command);
executeCommand(mkdirCommand);
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}

printf("\n");
}

return 0;
}

Input:

1 (choice for listing files)

Output:

Unix Commands Practice:


1. List files (ls)
2. Print working directory (pwd)
3. Display current date and time (date)
4. Display the contents of a file (cat)
5. Create a directory (mkdir)
6. Exit
Enter your choice: 1
(file1 file2 file3 ...)
Experiment 2:

Implementation of CPU Scheduling Algorithms


a) FCFS (First-Come, First-Served)
#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {


wt[0] = 0;

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


wt[i] = bt[i-1] + wt[i-1];
}

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 findAverageTime(int processes[], int n, int bt[]) {


int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);


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

printf("Processes Burst time Waiting time Turn around time\n");

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


total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ", (i+1));
printf(" %d ", bt[i]);
printf(" %d", wt[i]);
printf(" %d\n", tat[i]);}

printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);


printf("Average turn around time = %.2f\n", (float)total_tat / (float)n);
}
int main() {
int processes[] = { 1, 2, 3 };
int n = sizeof(processes) / sizeof(processes[0]);

int burst_time[] = { 10, 5, 8 };

findAverageTime(processes, n, burst_time);
return 0;
}
Input:

Process IDs: [1, 2, 3]


Burst times: [10, 5, 8]
Output:

Processes Burst time Waiting time Turn around time


1 10 0 10
2 5 10 15
3 8 15 23
Average waiting time = 8.33
Average turn around time = 16.00
b) SJF (Shortest Job First)

#include <stdio.h>

void findWaitingTime(int processes[], int n, int bt[], int wt[]) {


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

int complete = 0, t = 0, minm = 10000000;


int shortest = 0, finish_time;
int check = 0;

while (complete != n) {
for (int j = 0; j < n; j++) {
if ((rt[j] < minm) && (rt[j] > 0)) {
minm = rt[j];
shortest = j;
check = 1;
}
}

if (check == 0) {
t++;
continue;
}

rt[shortest]--;
minm = rt[shortest];
if (minm == 0)
minm = 10000000;

if (rt[shortest] == 0) {
complete++;
check = 0;
finish_time = t + 1;
wt[shortest] = finish_time - bt[shortest];
if (wt[shortest] < 0)
wt[shortest] = 0;
} t+
+;
}
}

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 findAverageTime(int processes[], int n, int bt[]) {


int wt[n], tat[n], total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);


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

printf("Processes Burst time Waiting time Turn around time\n");

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


total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
printf(" %d ", (i+1));
printf(" %d ", bt[i]);
printf(" %d", wt[i]);
printf(" %d\n", tat[i]);
}

printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);


printf("Average turn around time = %.2f\n", (float)total_tat / (float)n);
}

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

int burst_time[] = { 6, 8, 7, 3 };

findAverageTime(processes, n, burst_time);
return 0;
}

Input:

Process IDs: [1, 2, 3, 4, 5]


Burst times: [10, 1, 2, 1, 5]
Priorities: [3, 1, 4, 5, 2]

Output:

Processes Burst time Priority Waiting time Turn around time


2 1 1 0 1
5 5 2 1 6
1 10 3 6 16
3 2 4 16 18
4 1 5 18 19
Average waiting time = 8.20
Average turn around time = 12.00
Experiment 3:

Implementation of CPU Scheduling Algorithms

a) Priority (Non-Preemptive)
#include <stdio.h>

typedef struct {
int processID;
int burstTime;
int priority;
int waitingTime;
int turnAroundTime;
} Process;

void findWaitingTime(Process proc[], int n) {


proc[0].waitingTime = 0;

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


proc[i].waitingTime = proc[i - 1].waitingTime + proc[i - 1].burstTime;
}
}

void findTurnAroundTime(Process proc[], int n) {


for (int i = 0; i < n; i++) {
proc[i].turnAroundTime = proc[i].burstTime + proc[i].waitingTime;
}
}

void sortProcessesByPriority(Process proc[], int n) {


Process temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].priority > proc[j].priority) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}

void findAverageTime(Process proc[], int n) {


int total_wt = 0, total_tat = 0;

findWaitingTime(proc, n);
findTurnAroundTime(proc, n);

printf("Processes Burst time Priority Waiting time Turn around time\n");


for (int i = 0; i < n; i++) {
total_wt += proc[i].waitingTime;
total_tat += proc[i].turnAroundTime;
printf(" %d ", proc[i].processID);
printf(" %d ", proc[i].burstTime);
printf(" %d ", proc[i].priority);
printf(" %d ", proc[i].waitingTime);
printf(" %d\n", proc[i].turnAroundTime);
}

printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);


printf("Average turn around time = %.2f\n", (float)total_tat / (float)n);
}

int main() {
Process proc[] = { {1, 10, 3}, {2, 1, 1}, {3, 2, 4}, {4, 1, 5}, {5, 5, 2} };
int n = sizeof(proc) / sizeof(proc[0]);

sortProcessesByPriority(proc, n);
findAverageTime(proc, n);
return 0;
}

Input:

Allocation Matrix: [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
Request Matrix: [[0, 0, 0], [2, 0, 2], [0, 0, 0], [1, 0, 0], [0, 0, 2]]
Available Resources: [3, 3, 2]

Output:

Allocation Matrix:
010
200
302
211
002
Request Matrix:
000
202
000
100
002
Available Resources:
332
b) Multi-level Queue Scheduling
#include <stdio.h>

typedef struct {
int processID;
int burstTime;
int queueLevel;
int waitingTime;
int turnAroundTime;
} Process;

void findWaitingTime(Process proc[], int n) {


proc[0].waitingTime = 0;

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


proc[i].waitingTime = proc[i - 1].waitingTime + proc[i - 1].burstTime;
}
}

void findTurnAroundTime(Process proc[], int n) {


for (int i = 0; i < n; i++) {
proc[i].turnAroundTime = proc[i].burstTime + proc[i].waitingTime;
}
}

void sortProcessesByQueueLevel(Process proc[], int n) {


Process temp;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (proc[i].queueLevel > proc[j].queueLevel) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}

void findAverageTime(Process proc[], int n) {


int total_wt = 0, total_tat = 0;

findWaitingTime(proc, n);
findTurnAroundTime(proc, n);

printf("Processes Burst time Queue level Waiting time Turn around


time\n");
for (int i = 0; i < n; i++) {
total_wt += proc[i].waitingTime;
total_tat +=
proc[i].turnAroundTime; printf("%d
", proc[i].processID);
printf(" %d ", proc[i].burstTime);
printf(" %d ", proc[i].queueLevel);
printf(" %d ", proc[i].waitingTime);
printf(" %d\n", proc[i].turnAroundTime);
}

printf("Average waiting time = %.2f\n", (float)total_wt / (float)n);


printf("Average turn around time = %.2f\n", (float)total_tat / (float)n);
}

int main() {
Process proc[] = { {1, 10, 1}, {2, 5, 1}, {3, 8, 2}, {4, 6, 3} };
int n = sizeof(proc) / sizeof(proc[0]);

sortProcessesByQueueLevel(proc, n);
findAverageTime(proc, n);
return 0;
}

Input:

Processes: [0, 1, 2, 3, 4]
Available Resources: [3, 3, 2]
Maximum Resources: [[7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3]]
Allocated Resources: [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]

Output:

System is in safe state.


Safe sequence is: 1 3 4 0 2
Experiment 4:

Implementation of resource allocation graph (RAG)

#include <stdio.h>

#define N 5 // Number of processes


#define M 3 // Number of resources

void displayRAG(int allocation[N][M], int request[N][M], int available[M]) {


printf("Allocation Matrix:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%d ", allocation[i][j]);
}
printf("\n");
}

printf("Request Matrix:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("%d ", request[i][j]);
}
printf("\n");
}

printf("Available Resources:\n");
for (int j = 0; j < M; j++) {
printf("%d ", available[j]);
}
printf("\n");
}

int main() {
int allocation[N][M] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2} };
int request[N][M] = { {0, 0, 0}, {2, 0, 2}, {0, 0, 0}, {1, 0, 0}, {0, 0, 2} };
int available[M] = {3, 3, 2};

displayRAG(allocation, request, available);


return 0;
}
Input:

Allocation Matrix: [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
Request Matrix: [[0, 0, 0], [2, 0, 2], [0, 0, 0], [1, 0, 0], [0, 0, 2]]
Available Resources: [3, 3, 2]

Output:

Wait-For Graph:
00000
00110
00000
00000
00000
Experiment 5:

Implementation of Banker’s algorithm for Deadlock


Avoidance

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

#define N 5 // Number of processes


#define M 3 // Number of resources

bool isSafe(int processes[], int avail[], int max[][M], int allot[][M]) {


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

bool finish[N] = {0};


int safeSeq[N];
int work[M];
for (int i = 0; i < M; i++)
work[i] = avail[i];

int count = 0;
while (count < N) {
bool found =
false;
for (int p = 0; p < N; p++) {
if (!finish[p]) {
int j;
for (j = 0; j < M; j++)
if (need[p][j] > work[j])
break;

if (j == M) {
for (int k = 0; k < M; k++)
work[k] += allot[p][k];

safeSeq[count++] = p;
finish[p] = 1;
found = true;
}
}
}

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

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


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

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

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

int max[N][M] = { {7, 5, 3}, {3, 2, 2}, {9, 0, 2}, {2, 2, 2}, {4, 3, 3} };

int allot[N][M] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2} };

isSafe(processes, avail, max, allot);

return 0;
}

Input

Maximum Resource Requirement Matrix:max[][] = {


{7, 5, 3}, // Maximum resources required by P0
{3, 2, 2}, // Maximum resources required by P1
{9, 0, 2}, // Maximum resources required by P2
{2, 2, 2}, // Maximum resources required by P3
{4, 3, 3} // Maximum resources required by P4
}
Allocation Matrix:allot[][] = {
{0, 1, 0}, // Resources allocated to P0
{2, 0, 0}, // Resources allocated to P1
{3, 0, 2}, // Resources allocated to P2
{2, 1, 1}, // Resources allocated to P3
{0, 0, 2} // Resources allocated to P4
}

Output

System is in safe state.


Safe sequence is: 1 3 4 0 2
Experiment 6:
Conversion of resource allocation graph (RAG) to
wait-for graph (WFG)

#include <stdio.h>

#define N 5 // Number of processes


#define M 3 // Number of resources

void displayWFG(int allocation[N][M], int request[N][M], int available[M]) {


printf("Wait-For Graph:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int wait = 0;
for (int k = 0; k < M; k++) {
if (request[i][k] > available[k]) {
wait = 1;
break;
}
}
printf("%d ", wait);
}
printf("\n");
}
}

int main() {
int allocation[N][M] = { {0, 1, 0}, {2, 0, 0}, {3, 0, 2}, {2, 1, 1}, {0, 0, 2} };
int request[N][M] = { {0, 0, 0}, {2, 0, 2}, {0, 0, 0}, {1, 0, 0}, {0, 0, 2} };
int available[M] = {3, 3, 2};

displayWFG(allocation, request, available);


return 0;
}
Input

Number of processes (N): 5Number of resource types (M): 3


Allocation Matrix:allocation[][] = {
{0, 1, 0}, // Resources allocated to P0
{2, 0, 0}, // Resources allocated to P1
{3, 0, 2}, // Resources allocated to P2
{2, 1, 1}, // Resources allocated to P3
{0, 0, 2} // Resources allocated to P4
}
Request Matrix:request[][] = {
{0, 0, 0}, // Resources requested by P0
{2, 0, 2}, // Resources requested by P1
{0, 0, 0}, // Resources requested by P2
{1, 0, 0}, // Resources requested by P3
{0, 0, 2} // Resources requested by P4
}
Available Resources:available[] = {3, 3, 2}

Output

Wait-For Graph:
00000
00000
00000
00000
00000

You might also like