Operating Systems Lab Manual
Operating Systems Lab Manual
OPERATING SYSTEM
(203105214)
4th SEMESTER
COMPUTER SCIENCE & ENGINEERING DEPARTMENT
Lab
Manual
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
CERTIFICATE
210303126213 1
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
7. Write a C program to
create a child process
210303126213 2
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
8. Finding out biggest
number from given
three numbers
supplied as command
line arguments
12. Implementation of
FCFS &Round Robin
Algorithm.
13. Implementation of
Banker’s Algorithm.
210303126213 3
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
List of Practicals:
1. Study of Basic commands of Linux.
2. Study the basics of shell programming.
3. Write a Shell script to print given numbers sum of all digits.
4. Write a shell script to validate the entered date. (eg. Date format is: dd-mm-yyyy).
5. Write a shell script to check entered string is palindrome or not.
6. Write a Shell script to say Good morning/Afternoon/Evening as you log in to
system.
7. Write a C program to create a child process
8. Finding out biggest number from given three numbers supplied as command line
arguments
9. Printing the patterns using for loop.
10. Shell script to determine whether given file exist or not.
11. Write a program for process creation using C. (Use of gcc compiler).
12. Implementation of FCFS & Round Robin Algorithm.
13. Implementation of Banker’s Algorithm.
210303126213 4
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 1
Shell Script: Allows a user to execute commands by typing them manually at a terminal, or
automatically in programs called shell scripts.
A shell is not an operating system. It is a way to interface with the operating system and run
Commands.
• Bash is a shell written as a free replacement to the standard Bourne Shell (/bin/sh) originally
written by Steve Bourne for UNIX systems.
• It has all of the features of the original Bourne Shell, plus additions that make it easier to
program with and use from the command line.
• Since it is Free Software, it has been adopted as the default shell on most Linux systems.
DESCRIPTION:
SYNTAX:
Pwd
EXAMPLE:
$ pwd
/home/directory_name
210303126213 5
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
DESCRIPTION:
It allows you to change your working directory. You use it to move around within
the hierarchy of your file system.
210303126213 6
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
SYNTAX:
cd directory_name
EXAMPLE:
Input: $ cd /documents/work
3. cd ..
DESCRIPTION:
SYNTAX:
cd ..
EXAMPLE:
cd ..
DESCRIPTION:
List all files and folders in the current directory in the column format.
SYNTAX:
ls [options]
• Lists the total files in the directory and subdirectories, the names of the files in the current
210303126213 7
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
5. cat
DESCRIPTION:
cat stands for "catenate". It reads data from files, and outputs their contents. It is the simplest
way to display the contents of a file at the command line.
SYNTAX:
cat filename
EXAMPLES:
cat /proc/meminfo
6. head
DESCRIPTION:
head, by default, prints the first 10 lines of each FILE to standard output. With more than one
FILE, it precedes each set of output with a header identifying the file name.
If no FILE is specified, or when FILE is specified as a dash ("-"), head reads from standard input.
SYNTAX:
210303126213 8
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
head [option]…[file/directory]
EXAMPLE:
head myfile.txt
7. tail
210303126213 9
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
DESCRIPTION:
tail is a command which prints the last few number of lines (10 lines by default) of a certain
file, then terminates.
SYNTAX:
tail [option]…[file/directory]
EXAMPLE:
DESCRIPTION:
The mv command lets you move a file from one directory location to another. It also lets you
rename a file (there is no separate rename command).
SYNTAX:
EXAMPLE:
mv myfile.txt destination_directory
mv myfile.txt ../
• In this case, if JOE1_expenses does not exist, it will be created with the exact content
ofjoe_expenses, and joe_expenses will disappear.
mv joe_expenses JOE1_expenses
210303126213 10
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
DESCRIPTION:
210303126213 11
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
If the specified directory does not already exist, mkdir creates it. More than one directory
may be specified when calling mkdir.
SYNTAX:
EXAMPLE:
mkdir work
DESCRIPTION:
SYNTAX:
EXAMPLE:
Creates a copy of the file in the currently working directory named origfile. The copy will be
named newfile, and will be located in the working directory.
cp origfile newfile
DESCRIPTION:
The rmdir command is used to remove a directory that contains other files or directories.
210303126213 12
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
SYNTAX:
rm directory_name
EXAMPLE:
Delete mydir directory along with all files and directories within that directory. Here, -r is for
recursive and –f is for forcefully.
210303126213 13
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
12. gedit
DESCRIPTION:
SYNTAX:
gedit filename.txt
EXAMPLE:
gedit light2.sh
MAN
DESCRIPTION:
SYNTAX:
man command
EXAMPLE:
man ls
210303126213 14
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
210303126213 15
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
13. clear
DESCRIPTION:
SYNTAX:
clear
EXAMPLE:
clear
14. whoami
DESCRIPTION:
whoami prints the effective user ID. This command prints the username associated with the
current effective user ID.
SYNTAX:
whoami [option]
EXAMPLE:
Whoami
15. wc
DESCRIPTION:
210303126213 16
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
wc (word count) command, can return the number of lines, words, and characters in a file.
SYNTAX:
wc [option]… [file]…
EXAMPLE:
wc -l myfile.txt
wc -l myfile.txt
DESCRIPTION:
SYNTAX:
EXAMPLE:
210303126213 17
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Search the word Hello in file named myfile.txt grep “Hello” myfile.txt
18. FREE
DESCRIPTION:
Display RAM details in Linux machine.
SYNTAX:
free
EXAMPLE:
To display the RAM details in Linux machine need to write following command.
free
1. pipe ( | )
DESCRIPTION:
Pipe command is used to send output of one program as a input to another. Pipes “|” help combine 2 or more
commands.
SYNTAX:
Command 1 | command 2
EXAMPLE:
Display lines of input files containing “Aug” and send to standard output ls -l | grep “Aug
210303126213 18
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 2
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.
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:
210303126213 19
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
• 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:
1. Create a file using a vi editor(or any other editor). Name script file with extension .sh
2. 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.
#!/bin/sh
ls
210303126213 20
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Commenting is important in any program. In Shell programming, the syntax to add a comment is
#comment
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
#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"
210303126213 21
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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
210303126213 22
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 3
Definition: 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.
Flowchart:
Sample Code:
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Input: -
210303126213 23
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
$sh num.sh Operating System (203105214) B. Tech. 2nd Year
210303126213 24
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
0
1
2
3
4
5
6
7
8
9
Flowchart:
Sample Code:
sum=0
210303126213 25
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
while [ $num -gt 0 ]
do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done
echo $sum
Note: -gt=greater than, -lt=less than
OUTPUT:
210303126213 26
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 4
Definition: 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 it is a leap year then the no. days will be 29. Bychecking
these conditions for days, month and year using various control statements of Linux can validate the
date entered by the user. (eg. Date format is: dd-mm-yyyy)
Flowchart:
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.
210303126213 27
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
210303126213 28
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
case expression
in
pattern1 )
statements ;;
pattern2 )
statements ;;
...
esac
• 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.
Sample Code:
read str
case $str in
hello)
echo "Hello yourself!"
;;
bye)
echo "See you again!"
;;
*)
echo "Sorry, I don't understand"
;;
esac
Input:
Hi
Output:
210303126213 29
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Code:
dd=0
mm=0
yy=0
days=0
echo -n "Enter day (dd) : "
read dd
210303126213 30
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
fi Operating System (203105214) B. Tech. 2nd Year
OUTPUT
210303126213 31
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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.
Flowchart:
Sample Code:
210303126213 32
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
210303126213 33
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
read str
k=`echo $str | cut -c 2`
echo “$k”
Input:
#!/bin/bash
echo "Enter a String"
read input
reverse=""
len=${#input}
for (( i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo "$input is palindrome"
else
echo "$input is not palindrome"
fi
OUTPUT
210303126213 34
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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.
Flowchart:
Sample Code:
minutes=`date + M%`
echo $minutes
210303126213 35
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Set 2: Read the hours from system using cut command of unix.
Sample Code:
clear
hours=`date | cut -c12-13`
echo "the value of hour is $hours"
Input: -
Output:
Set 3: Write a Shell script to say Good morning/Afternoon/Evening as you log in to system.
Syntax
h=$(date +"%H")
if [ $h -gt 6 -a $h -le 12 ]
then
echo good morning
elif [ $h -gt 12 -a $h -le 16 ]
then
echo good afternoon
elif [ $h -gt 16 -a $h -le 20 ]
then
echo good evening
else
echo good night
fi
210303126213 36
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
210303126213 37
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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.
Fork system call is used for creating a new process, which is called child process, which runs
concurrently with the process that makes the fork() call (parent process). After a new child
process is created, both processes will execute the next instruction following the fork() system
call. A child process uses the same pc(program counter), same CPU registers, same open files
which use in the parent process.
Flowchart:
210303126213 38
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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()
{
printf("Hello world!\n");
return 0;
}
OUTPUT:
Set 2.
INPUT:
#include <stdio.h>
#include <sys/types.h>
int main()
{
fork();
fork();
fork();
210303126213 39
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
printf("hello\n");
return 0;
}
OUTPUT:
Set 3
INPUT:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void forkexample()
{
// child process because return value zero
if (fork() == 0)
printf("Hello from Child!\n");
210303126213 40
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
OUTPUT:
210303126213 41
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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:
Show the use of command line argument for finding the biggest number among three
numbers using if else ladder.
210303126213 42
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
INPUT:
echo "Enter Num1"
read num1
echo "Enter Num2"
read num2
echo "Enter Num3"
read num3
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo $num2
else
echo $num3
fi
OUTPUT:
210303126213 43
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
210303126213 44
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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:
Code:
#Here $1 is the parameter you passed. It specifies the no. of rows i,e “ / ”
210303126213 45
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
# Static input for N
N=5
i=0
j=0
while [ $i -le `expr $N - 1` ]
do
j=0
while [
$j -le `expr $N - 1` ]
do
if [ `expr $N - 1` -le `expr $i + $j` ]
then
# Print the pattern
echo -ne "/"
else
# Print the spaces required
echo -ne " "
fi
j=`expr $j + 1`
done
# For next line
echo
i=`expr $i + 1`
done
Input:
210303126213 46
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Output:
210303126213 47
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 10
Definition: Various commands are available in Linux to check whether the given directory or file
are 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:
210303126213 48
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Code:
Shell script to determine whether given directory, exist or not.
#!/bin/bash
File=read_file.txt
if [ -f "$File" ]; then
echo "$File exist"
else
echo "$File does not exist"
fi
Output:
210303126213 49
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
PRACTICAL NO: 12
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 processes
by the order of their arrival.
Flowchart:
Set 1: How to take Arrival Time and Burst Time of the processes.
Sample Code:
#include<stdio.h>
int main()
{
int n,count;
int at[10],bt[10];
printf("Enter Total Process:\t ");
scanf("%d",&n);
for(count=0;count<n;count++)
210303126213 50
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
{
210303126213 51
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
printf("Enter Arrival Time and Burst Time for Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
}
return 0;
}
Input:
Output:
Set 2: How to sort processes based on their Arrival Time for 5 processes.
Sample Code:
#include<stdio.h>
void main()
{
int i,temp,j,f,array[10];
printf("Enter 5 Processes");
//read array
for(i=0;i<5;i++)
{
scanf("%d",&array[i]);
}
//bubble sort
for(i=0;i<10;i++)
{ f=1;
for(j=0;j<9;j++)
{
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
f=0;
}
}
if(f==1)
210303126213 52
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
break;
}
printf("after swaping");
for(i=0;i<5;i++)
printf(" %d",array[i]
}
210303126213 53
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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. Each 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.
Flowchart:
Set 1: How to take Arrival Time, Burst Time and Time Quantum of the processes.
Sample Code:
#include <stdio.h>
210303126213 54
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
int main()
{
Input:
Output:
210303126213 55
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
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
Set 1: How to take no. of processes, no. of resources, maximum resource matrix, allocated resource
210303126213 56
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
matrix and available resources for each process.
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10], completed[10];
int p, r, i, j, process, count;
count = 0;
printf("Enter the no of processes : ");
scanf("%d", &p);
for(i = 0; i< p; i++)
completed[i] = 0;
printf("\n\nEnter the no of resources : ");
scanf("%d", &r);
printf("\n\nEnter the Max Matrix for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}
printf("\n\nEnter the allocation for each process : ");
for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
printf("\n\nEnter the Available Resources : ");
for(i = 0; i < r; i++)
scanf("%d", &avail[i]);
210303126213 57
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
Operating System (203105214) B. Tech. 2nd Year
Set 3: Perform the Banker’s Algorithm and find out that whether the System is Safe or not and also
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.
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 such process exists, just go to step 4.
That means if all processes are finished, then the system is in safe state.
210303126213 58