0% found this document useful (0 votes)
23 views47 pages

Priyankaos Lab Manual

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)
23 views47 pages

Priyankaos Lab Manual

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/ 47

COMPUTER SCIENCE AND ENGINEERING

FACULTY OF ENGINEERING AND


TECHNOLOGY OPERATING SYSTEM (203105214) B.
TECH 2nd YEAR

PRACTICAL-1
Aim: To study of basic commands of Linux/Unix.
BASIC LINUX COMMANDS:

1. Pwd: -
 Pwd-Present working directory
 Syntax: - pwd
 Description: - The pwd Linux command prints the current
working directory path starting from the root. Use the pwd
command to find your way in the Linux file system
Structure maze or the pass the working directory in the
bash script. In this tutorial, you will learn to use the pwd
command.
 Output: -

2. Cd: -
 Cd: - Change directory
 Syntax: -cd
 Description: - cd command in Linux known as change
directory command. It is used to change current
working directory.

 Output: -

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214) B.
TECH 2nd YEAR

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

3. Cd .. :-
 Cd .. : - To change the parent directory to current directory.
 Description: -This command is used to move to the parent
directory of current directory, or the directory one level up
from the current directory. “..” represents parent directory
 Syntax: cd ..
 Output: -

LS: -
Ls: - This command is used to list the files present in that directory.
Description: - we use ls command to list files and directories. This command will print a
Syntax: ls
Output: -

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

5. CAT: -
 Cat: - A utility command in Linux.
 Description: - The cat command is a utility command in Linux.
One of its most common usages is to print the content of a file
onto the standard output stream. Other than that, the cat
command also allows us to write some texts into a file.
 Syntax: - cat [file-name]
 Output:

MKDIR: -
Mkdir:- Mkdir command in Linux/Unix allows users to create.
Description: - The mkdir command in Linux/Unix allows users to create or make new d
Syntax: - mkdir [directory name]
Output: -

7. MV: -

 Mv: -Move.
 Description: - The mv command termed as “Move”, which is a
command-line utility to move files or directories from source

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
to target. It supports the moving of a single file, multiple files,
and directories.
 Syntax: - mv [option] source destination

 Output:

-
echo: -
Description: - Display text on the screen.
Syntax: - echo your text
Output:

9 .CP: -
 cp: -Copies the files.
 Description: - cp command copies files (or, optionally,
directories). The copy is completely independent of the
original. You can either copy one file to another, or copy
arbitrarily many files to a destination directory. In the first
format, when two file names are given, cp command copies
SOURCE file to DEST file.

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
 Syntax: -
cp [option] source destination
 Output:

10 .RMDIR: -
 rmdir: -to remove or empty the diroctaries.
 Description: - mdir command is used remove empty
directories from the filesystem in Linux. The rmdir
command removes each and every directory specified in the
command line only if these directories are empty. So, if the
specified directory has some directories or files in it then this
cannot be removed by rmdir command.
 Syntax: - rmdir [directory name]
 Output: -

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:2

Aim: Study the basics of shell programming.

What is a Shell?

An Operating is made of many components, but its two prime components are -

 Kernel
 Shell

A Kernel is at the nucleus of a computer. It makes the communication between the


hardware and software possible. While the Kernel is the innermost part of an
operating system, a shell is the outermost one.

A shell in a Linux operating system takes input from you in the form of commands,
processes it, and then gives an output. It is the interface through which a user works
on the programs, commands, and scripts. A shell is accessed by a terminal which
runs it.

When you run the terminal, the Shell issues a command prompt (usually $),
where you can type your input, which is then executed when you hit the Enter key.
The output or the result is thereafter displayed on the terminal.

The Shell wraps around the delicate interior of an Operating system protecting it
from accidental damage. Hence the name Shell.

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Types of Shell
There are two main shells in Linux:

1. The Bourne Shell: The prompt for this shell is $ and its derivatives are
listed below:

 POSIX shell also is known as sh


 Korn Shell also knew as sh
 Bourne Again SHell also knew as bash (most popular)

2. The C shell: The prompt for this shell is %, and its subcategories are:

C shell also is known as csh


Tops C shell also is known as tcsh

What is Shell Scripting?


Shell scripting is writing a series of command for the shell to execute. It can combine lengthy and repe

Let us understand the steps in creating a Shell Script

Create a file using a vi editor(or any other editor). Name script file with
extension .sh
Start the script with #! /bin/sh
3. Write some code.
4. Save the script file as filename.sh
5. For executing the script type bash filename.sh

"#!" is an operator called shebang which directs the script to the interpreter location.
So, if we use"#!
/bin/sh" the script gets directed to

the bourne-shell. Let's create a

small script –

Let's see the steps to create it -

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

vi

:wq

vi

Command 'ls' is executed when we execute the scrip sample.sh file


Adding shell comments

Commenting is important in any program. In Shell programming, the syntax to add a comment is

#comment

Let understand this with an example.

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

What are Shell Variables?

As discussed earlier, Variables store data in the form of characters and


numbers. Similarly, Shell variables are used to store information and they can
by the shell only.

For example, the following creates a shell variable and then prints it:

Variable = “hello”
echo $variable

Below is a small script which will use a variable. echo “what is your name?”
read $name
echo “how do you do $name?” read remark
echo “I am $remark too! ”

Let's understand, the steps to create and execute the script

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

As you see, the program picked the value of the variable 'name' as Joy and 'remark'
as excellent.

This is a simple script. You can develop advanced scripts which contain conditional
statements, loops, and functions. Shell scripting will make your life easy and Linux
administration a breeze.

Summary:
Kernel is the nucleus of the operating systems, and it communicates between hardware and software
Shell is a program which interprets user commands through CLI like Terminal
The Bourne shell and the C shell are the most used shells in Linux
Shell scripting is writing a series of command for the shell to execute
Shell variables store the value of a string or a number for the shell to read
Shell scripting can help you create complex programs containing conditional statements, loops, and functions

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:3

AIM: Take any number from the user. Get each and every digit one by one and make
addition of that to find the sum of digits of a given number. E.g if user has entered 342
then the sum of digits is 3+4+2 = 9.

Set 1: Printing numbers from 0 to 9 using while loop.

Flowchart:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Code:

Output:

Set 2: To find whether a number is even or odd.

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Code:

Output:

Set 3 : write a shell script for Sum of all digits of a number

Code:

Output:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:4

Description: -

In a year there are 12 months and in each month the no. of days
are 30 or 31.For February month the no. of days is 28 and if the leap year
then the no. of days is 29. By checking these conditions for days , months
and year using various control statements of Linux can validate the data
entered by the user (eg. Date format is : dd-mm-yyyy )

Flow-chart: -

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
SET 1: Read a string from the user and if it is hello then print “Hello
yourself” , if it is bye then print “See you again” otherwise print “Sorry , I don’t
understand” using case statement .

Syntax
Bash case statement.

Case expression in
Pattern 1 )

Statements ;;

Pattern 2 )

Statements ;; … esac
Following are the key points of bash case statements
Case statement first expands the expression and tries to match it against each pattern
When a match is found all of the associated statements until the double semicolon (;; ) are executed
After the first match, case terminates with the exit status of the last command that was executed
If there is no match, exit status of case is zero.

Code:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
Input :

Hi

Output:

SET 2: Check given year is leap or not.

Code:

Input:
Enter the year 2004 Enter the year 2001 Output :

SET 3: Write a shell script to validate the entered date. (eg. Date format is : dd-mm-yyyy )

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
Code :

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
Output :

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:5
Definition: A string is said to be palindrome if reverse of the string is same as string. For example,
“abba” is palindrome, but “abbc” is not palindrome. We can use Linux command to find the length
of the string and then by comparing first character and last character of the string, second
character and second last character of the string and so on., we can identify that whether the string
is palindrome or not.

Flow chart:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

SET 2:Write a shell script to check entered string is palindrome or not.

SAMPLE CODE

OUTPUT

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:6

Definition: The date command is used to print out the system's time and date
information. By extracting hours from the date using Linux ‘cut’ command and by using if
else ladder can wish user an appropriate message like Good morning/Afternoon/Evening.

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

SET 2:Write a Shell script to say Good morning/Afternoon/Evening as you log in to system.

CODE :

Output :

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:7
Definition: Create child process using fork( ), which is a system call to create a child process.
Use getpid( ) for getting the id of the process, getppid( ) for getting the parent process id.

Flowchart:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Set 1: Create a child process using one fork( ), getpid( ), getppid( ).

Note: fork() is threading based function, to get the correct output run

the program on a local system. Please note that the above program do

not compile in Windows Environment.

INPUT:

#include <stdio.h>

#include <sys/types.h>
#include <unistd.h> int main()
{
// make two process which run same
// program after this instruction fork();
printf("Hello world!\n");

return 0;

}
OUTPUT:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Set 2.

INPUT:

#include <stdio.h>

#include <sys/types.h>

int main()

fork();

fork();

fork(); printf(“hello/n”); return 0;


}

Output:

**The number of times ‘name is printed is equal to number of process

created. Total Number of Processes = 2n, where n is number of fork

system calls. So here n = 3, 23 = 8.

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Set 3

INPUT:
#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

void forkexample()

printf("Hello from Child!\n");

// child process because return value

zero if (fork() == 0)

printf("Hello from Child!\n");

// parent process because return value non-zero.

else

printf("Hello from Parent!\n");

int main()

forkexample();

return 0;

Output:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:8
Definition: Finding out biggest number from given three numbers supplied as command line

argument. Command argument means at run time, the user will enter the input. First input will be

considered as number 1, second input will be consider as number 2 and so on. It depends on the no

of command line argument.

Flowchart:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Show the use of command line argument for finding the biggest number among three

numbers using if else ladder.

INPUT:

OUTPUT:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:9
Definition: Print the pattern using for loop which is used to do the same thing again and
again until

some condition is satisfied.

For loop is used to do the same thing until some condition is there.

Flowchart:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Input:

Output:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:10
Definition: Various commands are available in Linux to check whether the given
directory or fileare exist or not in the system. Several options are also available to check
special conditions like file.is empty or not. We can use these commands in shell script as
well.

Flowchart:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Code:

Shell script to determine whether

given directory, exist or not.

OUT PUT:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:11
Definition: First come, first served (FCFS) is an operating system process scheduling
algorithm and a network routing management mechanism that automatically executes
queued requests and processesby the order of their arrival.

Flowchart:

Set 1: How to take Arrival Time and Burst Time of the processes.

Sample Code:

#include<stdio.h>

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

wt[0] = 0;

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

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
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 findavgTime( 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] );

int s=(float)total_wt / (float)n;

PAGE NO: Enrolment No :


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
int t=(float)total_tat / (float)n;

printf("Average waiting time = %d",s);

printf("\n");

printf("Average turn around time = %d ",t);

int main()

int processes[] = { 1, 2, 3};

int n = sizeof processes / sizeof processes[0];

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

findavgTime(processes, n, burst_time); return 0;


}
Output:

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:12
Definition: Round robin (RR) scheduling is a job-scheduling algorithm that is considered
to be very fair, as it uses time slices that are assigned to each process in the queue or line.

Flowchart:

Eac h process is
then allowed to use the CPU for a given amount of time, and if it does not finish within the
allotted time,it is preempted and then moved at the back of the line so that the next
process in line is able to use the CPU for the same amount of time

Set 1: How to take Arrival Time, Burst Time and Time Quantum of the processes.

Sample Code:

#include<stdio.h>

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

void main()

// initlialize the variable name

int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];

float avg_wt, avg_tat;

printf(" Total number of process in the system: ");

scanf("%d", &NOP);

y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time for(i=0; i<NOP; i++)
{

printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1); printf(" Arrival time is: \t"); // Acc
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time scanf("%d", &bt[i]);

temp[i] = bt[i]; // store the burst time in temp array

// Accept the Time qunat

printf("Enter the Time Quantum for the process: \t");

scanf("%d", &quant);

// Display the process No, burst time, Turn Around Time and the waiting

time printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");

for(sum=0, i = 0; y!=0; )

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions

sum = sum +

temp[i]; temp[i] =

0; count=1;

else if(temp[i] > 0)

temp[i] = temp[i] - quant; sum = sum + quant;


}

if(temp[i]==0 && count==1)

y--; //decrement the process no.

printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]- bt[i]);
wt = wt+sum-at[i]-bt[i];

tat = tat+sum-at[i];

count =0;

if(i==NOP-1)

i=0;

else if(at[i+1]<=sum)

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
{
i++;

else

i=0;

// represents the average waiting time and Turn Around time

avg_wt = wt * 1.0/NOP; avg_tat = tat * 1.0/NOP;


printf("\n Average Turn Around Time: \t%f", avg_wt); printf("\n Average Waiting Time: \t%f", avg_tat);
}

Output:

PAGE NO: Enrolment


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PRACTICAL NO:13
Definition: The Banker's algorithm, sometimes referred to as the detection
algorithm, is a resource allocation and deadlock avoidance algorithm. It tests for safety by
simulating the allocation of predetermined maximum possible amounts of all resources.
When a new process enters a system, it must declare the maximum number of instances of
each resource type that it may ever claim; clearly, that number may not exceed the total
number of resources in the system. Also, when a process gets all its requested resources it
must return them in a finite amount of time.

Flow Chart:
Resource-Request Algorithm

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

Set 1: How to take no. of processes, no. of resources, maximum resource matrix, allocated
resource matrix and available resources for each process.

#include <stdio.h>

int main()

int n, m, i, j, k;

n = 5;

m = 3;

int alloc[5][3] = { { 0, 1, 0 },

{ 2, 0, 0 },

{ 3, 0, 2 },

{ 2, 1, 1 },

{ 0, 0, 2 } };

int max[5][3] = { { 7, 5, 3 },

{ 3, 2, 2 },

{ 9, 0, 2 },
{ 2, 2, 2 },

{ 4, 3, 3 } };

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

int f[n], ans[n], ind = 0;

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

f[k] = 0;

int need[n][m];

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
for (i = 0; i < n; i++) {

for (j = 0; j < m; j++)

need[i][j] = max[i][j] - alloc[i][j];

int y = 0;

for (k = 0; k < 5; k++) {

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

if (f[i] == 0) {

int flag = 0;

for (j = 0; j < m; j++) {

if (need[i][j] > avail[j]){ flag = 1;


break;

if (flag == 0) {

ans[ind++] = i;

for (y = 0; y < m; y++)

avail[y] += alloc[i][y];

f[i] = 1;

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
int flag = 1;

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

if(f[i]==0)

flag=0;

printf("The following system is not

safe"); break;

if(flag==1)

printf("Following is the SAFE Sequence\n"); for (i = 0; i < n - 1; i++)


printf(" P%d ->", ans[i]);

printf(" P%d", ans[n - 1]);

return (0);

Output:

Set 3: Perform the Banker’s Algorithm and find out that whether the System is Safe or not
and also

PAGE NO: Enrolment No:


COMPUTER SCIENCE AND ENGINEERING
FACULTY OF ENGINEERING AND
TECHNOLOGY OPERATING SYSTEM (203105214)
print the Safe Sequence.

Safety Algorithm:

1. Let Work and Finish be vectors of length m and n, respectively. Initially,

Work = Available

Finish[i] =false for i = 0, 1, ... , n - 1.

This means, initially, no process has finished and the number of available resources is

represented by the Available array.

2. Find an index i such that both


Finish[i] ==false Needi <= Work
If there is no such i present, then proceed to step 4.

It means, we need to find an unfinished process whose need can be satisfied by the available resources. If no
3. Perform the following: Work = Work + Allocation; Finish[i] = true;
Go to step 2.

When an unfinished process is found, then the resources are allocated and the process is

marked finished. And then, the loop is repeated to check the same for all other processes.

4. If Finish[i] == true for all i, then the system is in a safe state.

That means if all processes are finished, then the system is in safe state.

PAGE NO: Enrolment No:

You might also like