V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

26
PESIT-BSC SS&OS Lab (10CSL58) PES Institute of Technology- Bangalore South Campus 1Km before Electronic City, Hosur Road, Bangalore-560100. DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT CODE: 10CSL58 FACULTY: Mr. Karthik S

Transcript of V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

Page 1: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

PES Institute of Technology- Bangalore South Campus 1Km before Electronic City, Hosur Road, Bangalore-560100.

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING

V SEMESTER

LAB MANUAL

SUBJECT: SS & OS LAB

SUBJECT CODE: 10CSL58

FACULTY: Mr. Karthik S

Page 2: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY Subject Code: 10CSL58 I.A. Marks: 25 Hours/Week: 03 Exam Hours: 03 Total Hours: 42 Exam Marks: 50

PART A Lex and yacc programs:

Execution of the following programs using LEX:

1) a. Program to count the number of characters, words, spaces and lines in a given input file.

b. Program to count the numbers of comment lines in a given program. Also eliminate them

and copy that program into separate file.

2) a. Program to recognize a valid arithmetic expression and identify the identifiers and

operators present. Print them separately.

b. Program to recognize whether a given sentence is simple or compound.

3) Program to recognize and count the number of identifiers in a given input file.

Execution of the following programs using YACC.

4) a. Program to recognize a valid arithmetic expression that uses operators +,- ,* and /.

b. Program to recognize a valid variable, which starts with a letter, followed by any number of

letters or digits.

5) a. Program to evaluate an arithmetic expression involving operators +, -, * and /.

b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (a n b n,

n>=0).

Page 3: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

6) Program to recognize the grammar (a n b, n>=10).

PART B

UNIX Programming:

1) a. Non-recursive shell script that accepts any number of arguments and prints them in a

Reverse order, (For example, if the script is named rargs, then executing rargs A B C should

produce C B A on the standard output).

b. C program that creates a child process to read commands from the standard input and

execute them (a minimal implementation of a shell –like program). You can assume that no

arguments will be passed to the commands to be executed.

2) a. Shell script that accepts two file names as arguments, checks if the permissions for these

files are identical and if the permissions are identical, outputs the common permissions,

otherwise outputs each file name followed by its permissions.

b. C program to create a file with 16 bytes of arbitrary data from the beginning and another 16

bytes of arbitrary data from an offset of 48. Display the file contents to demonstrate how the

hole in file is handled.

3) a. Shell script that accepts file names specified as arguments and creates a shell script that

contains this file as well as the code to recreate these files. Thus if the script generated by

your script is executed, it would recreate the original files (This is same as the “bundle”

script described by Brain W. Kernighan and Rob Pike in “The Unix Programming

Environment”, Prentice Hall India).

b. C program to do the following using fork () create a child process. The child process prints

its own process id and id of its parent and then exits. The parent process waits for its child to

finish (by executing the wait ()) and prints its own process id and the id of its child process

Page 4: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

and then exits.

Working environment: UNIX(Red Hat Linux)

Programming language: lex and yacc and unix including C.

Basic programming knowledge needed: C.

Open a file by using vi editor. Commands to open a file and save a file; To open : vi <file anme>.l � for lex programs. Vi <file name>.y � for yacc programs. Vi <file name>.c � for C programs. Once opened a vi editor it is required to write a program with insert mode.

Press I key for insert. Then we will be in insert mode where we can write a program. After

writing to save a file, use the following command.

Escape : wq

Where escpe is use to come to the command mode from insert mode

W for save, q for quit and to get the command promt.

Command for execution of lex , yacc and C programs. Lex: $lex <filename>.l $cc lex.yy.c –ll $./a.out Yacc: $lex <filename>.l $yacc –d <filename>.y $cc y.tab.c lex.yy.c –ll –ly $./a.out For C $cc <filename>.c

Page 5: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

$./a.out

Programs

PART A

A Execution of the following programs using LEX:

(char *) yytext This is a copy of the text which matched the current pattern, as a null-terminated string.

(int) yyleng This is the length of yytext

Please read the man page on flex for

01.

a. Program to count the number of characters, words, spaces and lines in a given input file.

Aim: A lex program to count how many characters (with space or without space), words,

spaces and lines from a given C file.

Here the input should be in different file. It is required to open the file in the main program by

using command line arguments.

Pseudo code:

1) In the definition section, declare and initialize the variables wc,cc,sc and lc to 0.

2) In the rule section;

- in the input, whenever there in no space, tab and new line character increment the

word counter (wc) and character counter(cc). cc= length(word).

- In the input, whenever a space or tab is encounters increment the space

counter(sc).

- In the input whenever a line character is encountered increment the line

counter(lc).

3) In the a subroutine section

Page 6: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

- since the input is through a file and output is on screen only, so there are only 2

arguments need to passed to main program(one for source and one for input file) which

also include yylex() function.

Compilation steps $ lex les1a.l $ cc lex.yy.c –ll $ ./a.out input.c

b. Program to count the numbers of comment lines in a given program. Also eliminate them

and copy that program into separate file.

Aim: the main aim of this program is to count the number of comment lines in a given C

program.

Next to eliminate all the comment lines and store the same program in one more file.

Here the input should be in different file and output is in different file. It is required to open 2

files in the main program by using command line arguments.

Pseudo code:

1) In the definition section, initialize a variable c with 0.

2) In the rule section :

We use following logical implementation:

� Any line produced by “//” is encountered increment c by 1.

� Any number of lines preceded by ‘/*’ and ended by ‘*/’ increments the c by

1.(multi comment line)

3) In the subroutine section:

- use the input c program from a file and store the output without

comment lines in an output file.

- `Print the number of comment lines within the input c program.

- In this part, in the main program chech whether the number of command

line arguments are 3.

- If not say “file can not open “ exit.

Page 7: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Compilation steps $ lex 1b.l $ cc lex.yy.c –ll $ ./a.out input.c output.c

02.

a. Program to recognize a valid arithmetic expression and identify the identifiers and

operators present. Print them separately.

Aim: the aim of this program is to recognize whether a given expression is valid or not. And

identify how many operators and operands presents in that expression.

Pseudo code:

1) In the definition section declare an array of size four and initialize with 0. set valid=1 and

number of operands as 0.

2) Use OPER as a start condition.

3) In the rule section:

� Whenever an operand is encountered increment the operand count and set valid to

1 if it was zero.

� After if operand is encountered if any of the operator ‘+’ ,’-‘,’*’ and ‘/’ in

encountered set valid to 0.

� Finally if valid is 1

Print expression is valid

Else

Print as invalid expression and exit.

Page 8: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Compilation steps $ lex 2a.l $ cc lex.yy.c –ll $ ./a.out

b. Program to recognize whether a given sentence is simple or compound.

Pseudo code:

Input: a valid sentence

Output: display whether the input sentence is simple or compound

Logic:

o Enter a sentence.

o Check whether the sentence contain ;and’,;or’,’because’,’but’ etc.. in between .

Is so..

Print ”compound”

Else

“simple”

Compilation steps $ lex 2b.l $ cc lex.yy.c –ll $ ./a.out 03.

Program to recognize and count the number of identifiers in a given input file.

Pseudo code:

1) In definition section declare and initialize the idcount to 0.

2) In the rule section,

- any line in the input c-file start a with long | char or int or float or double

or unsigned or short and followed a character string and followed by ‘,’

Page 9: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

increment idcount, if its character string is followed by ‘;’ increment the

idcount.

4) in the subroutine section:

- input is read from the c=file.

- Output is wriiten onto a file.

- No. of identifiers in the input c-file is printed on the console.

Compilation steps $ lex 3p.l $ cc lex.yy.c –ll $ ./a.out

Page 10: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

B. Execution the following programs using YACC.:

___________________________________________

01.

a. Program to recognize a valid arithmetic expression that uses operators +,- ,* and /

Pseudo code:

Lex: 1. {Declaration and regular definition]

Define header files to include first section

2. [translation rule]

Tokens generated are used in yacc files

[a-z A-Z] alphabets are returned

0-9 one or more combinations of integers

Yacc:

1. Accept token generated in lex part as input

2. Specify the order of procedure

3. Define rules with end points

4. Parse input string from standard input by calling yyparse() main function.

5. Print the result of any rules defined matches as arithmetic expression as valid

6. If none of the rule defined matches print arithmetic expression is invalid.

Compilation steps

$ lex 4a.l $ yacc –d 4a.y $ cc lex.yy.c y.tab.c –ll -ly $ ./a.out b. Program to recognize a valid variable, which starts with a letter, followed by any number

of letters or digits.

Pseudo code:

Page 11: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

1. Include header file y.tab.h

2. Define tokens

3. Declare [a-z] as L

[0-9] as D

4. Declare variable L D P

5. Call function yyerror()

6. If error exist then print “invalid” and exit

7. In main() call yyparse(), if yyparse() print “valid variable”

Compilation steps $ lex 4b.l $ yacc –d 4b.y $ cc lex.yy.c y.tab.c –ll -ly $ ./a.out 02.

a. Program to evaluate an arithmetic expression involving operators +, -, * and /.

Pseudo code:

Lex:

[declaration section]

Define header file and global variable definition

[translation rule]

[0-9] one or more combination of integer

Yacc:

1. Accept the token generated in lex part as input

2. Specify order of procedure

3. Define rules with end point

4. Parse input string from standard input by calling yyparse() by main function

5. Print the result of any rules matches

6. If none of results defined matches print “invalid expression”

Page 12: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Compilation steps $ lex 5a.l $ yacc –d 5a.y $ cc lex.yy.c y.tab.c –ll -ly $ ./a.out b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (a

nb

n, n>=0).

Pseudo code:

Lex:

1. Define header files to include in the first section

2. Translation rules

a A is returned

b B is returned

Yacc:

1. Include global ‘c’ declaration and assign it to one

2. Accept the token generated in lex part as input

3. Translation rule:

a. Define rules with end points

b. Parse input string from standard input by calling yyparse() by main function

4. Print result “valid” if any of rules defined matches

5. If none of rules matched, print invalid string

Compilation steps $ lex 5b.l $ yacc 5b.y $ cc lex.yy.c y.tab.c –ll -ly $ ./a.out 03.

Program to recognize the grammar (anb, n>=10).

Pseudo code:

1. Input a string

Page 13: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

2. Define grammar accept strings form

3. an b n>=10

4. stub->AAAAAAAAA expr B

5. expr : A expr

|

;

6. output if string was accepted by grammar or not

Compilation steps $ lex 6p.l $ yacc 6p.y $ cc lex.yy.c y.tab.c –ll -ly $ ./a.out

Page 14: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

PART B UNIX

01.

a. Non-recursive shell script that accepts any number of arguments and prints them in a

Reverse order, (For example, if the script is named rargs, then rargs A B C should produce

C B A on the standard output).

Pseudo code:

1. assign number of arguments to variable ‘C’ 2. print arguments in reverse order 3. until C is equal to 0 4. do 5. echo the last argument 6. decrement the value of ‘C’ by 1 7. done

b. C program that creates a child process to read commands from the standard input and

execute them (a minimal implementation of a shell – like program). You can assume that

no arguments will be passed to the commands to be executed.

Pseudo code:

1. Initialize or declare a variable of type pid-t 2. Fork() a child process 3. If a child process is created 4. Do 5. Print “enter the command” 6. Read command into syscmd 7. System(syscmd) 8. Utill(1)

02.

a. Shell script that accepts two file names as arguments, checks if the permissions for these

files are identical and if the permissions are identical, outputs the common permissions,

otherwise outputs each file name followed by its permissions.

Page 15: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Pseudo code:

1. Cut the permission part from attributes list of first file using ls –l and cut commands 2. Store it in perm1 3. Repeat step1 then store it in perm2 4. If contents of perm1=contents of perm2 5. Print “files have same permission” 6. Print file permission of either file 7. Else 8. Print “files have different permission” 9. Print permission of both files

b. C program to create a file with 16 bytes of arbitrary data from the beginning and

another 16 bytes of arbitrary data from an offset of 48. Display the file contents to

demonstrate how the hole in file is handled.

Pseudo code:

1. Initialize buf-1 to 16 bytes of data 2. Initialize buf-2 to another 16 bytes of data 3. Open a file with appropriate modes and permission 4. Write the contents of buf-1 into file 5. Move pointer by 32 bytes from current position 6. Write the contents of buf-2 into file 7. Close the file 8. Display the contents

03.

a. Shell script that accepts file names specified as arguments and creates a shell script that

contains this file as well as the code to recreate these files. Thus if the script generated by

your script is executed, it would recreate the original files (This is same as the “bundle”

script described by Brain W. Kernighan and Rob Pike in “The Unix Programming

Environment”, Prentice – Hall India).

Pseudo code:

1. if number of arguments passed is 0 2. then 3. print “usage: file sh f1 f2” 4. fi

Page 16: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

5. for I in argument list 6. print “echo extracting $i” 7. send line to bundle.sh 8. print “cat> $i << EOF 9. send line to bundle.sh 10. open file $i and send contents to bundle.sh 11. print EOF and send to bundle.sh

b. C program to do the following using fork () create a child process. The child process

prints its own process id and id of its parent and then exits. The parent process waits for its

child to finish (by executing the wait ()) and prints its own process id and the id of its child

process and then exits.

Pseudo code:

1. For a child using pid=fork() 2. If pid is less than 0 3. Print error in forking child\n” 4. Exit 5. If pid is equal to 0 6. Assign parent pid to ppid 7. Print “child is executing and parent id=”ppid” 8. Assign pid of child to mpid 9. Print “child id:”, mpid 10. Wait 11. exit

Operating Systems:

10. Program definition:

Design, develop and execute a program in C / C++ to simulate the working of Shortest

Remaining Time and Round-Robin Scheduling Algorithms. Experiment with different quantum

sizes for the Round-Robin algorithm. In all cases, determine the average turn-around time. The

input can be read from key board or from a file.

Aim:

Page 17: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

The aim of this problem is to schedule some given processes with the help of shortest remaining

time (SRT) scheduling and round robin (RR) scheduling algorithm and to find out the average

turn around time. Test round robin with different quantum and compare the average turn around

time.

Implementation:

Shortest Remaining Time Scheduling algorithm:

Input:

1. Number of processes (N)

2. arrival time of each process,

3. burst time of each process,

Output: Average turn around time, say ATAT

List of other Variables :

struct process

{

int name; //process name

int AT; //Arrival Time

int burst; //Burst time

int wait; //waiting time

int TAT; //Turn around time

int remain; //remaining burst time

int flag;

};

struct process p[5];

Logic: //input arrival time, AT in ascending order

• Accept the input from the user; call function input( )

• Call SRTF( ); calculates the waiting time and turn around time for each process

• Calculate average turn around time and display

� total_TAT =0,

Page 18: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

� for i = 0 to N-1 do,

total_TAT = total_TAT + p[i].TAT

� Avg_TAT = total_TAT/N

� Print “Avg_TAT”

Pseudo code:

1. Input( ): function to accept input from the user

• Accept total number of processes, i.e. Read N

• For all N processes,

� Accept each process name, i.e. Read p[i].name

� Accept each process arrival time, i.e. Read p[i].AT

� Accept each process burst time, i.e. Read p[i].burst

� Initialize waiting time, p[i].wait = 0

2. SRTF( ): function to implement shortest remaining time first algorithm and calculates the

waiting time and turn around time for each process.

• int t_burst=0;

• for i=0 to N-1 do,

� p[i].remain = p[i].burst // copy the burst time to remaining time

� t_burst = sum of all the burst time

• for i=0 to t_burst-1 do,

� for j=0 to N-1 do,

p[j].flag = 0;

if (p[j].AT<=i && p[j].remain>0)

p[j].flag = 1;

� num = min( )

� p[num-1].remain = p[num-1].remain-1, // current process executed for 1 more

time unit

//Calculate waiting time for rest of the processes

� for k =0 to N-1 do,

Page 19: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

if(p[k].AT<=i && p[k].remain>0 && p[k].name!= num)

p[k].wait = p[k].wait + 1

• //Calculate turn around time

for i = 0 to N-1 do,

p[i].TAT = p[i].wait +p[i].burst;

3. min( ): function to find the process with minimum remaining burst time

• //This function compares the remaining burst time of all the incomplete processes and

returns the process number (process_num) with minimum burst time

• Initialize the temporary array with infinity

for i =0 to N-1 do,

a[i] =99,

• for i =0 to N-1 do,

if (p[i].flag=1 && p[i].remain>0)

a[p[i].name-p[i].remain //copy remaining to array

• min=a[0],

• for i =0 to N-1 do,

if(a[i]<min)

min = a[i], process_num=i;

Round Robin scheduling algorithm:

Input:

1. Number of processes (N) ,

2. time quantum (tq),

3. burst time of each process,

Output: Average turn around time, say ATAT

List of other Variables:

struct process

{

int name; //process name

int burst; //Burst time

int wait; //waiting time

Page 20: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

int TAT; //Turn around time

int remain; //Remaining burst time

};

struct process [5];

Logic: //same arrival time for all the processes and order of arrival is from lower to higher

process number

• Accept the input from the user; call function input( )

• Call RR( ); calculates the waiting time and turn around time for each process

• Calculate average turn around time and display

� Avg_TAT = ttat/N;

� Print “Avg_TAT”

Pseudo code:

1. Input( ): function to accept input from the user

• Accept total number of processes, i.e. Read N

• Accept time quantum, i.e. Read tq;

• For all N processes,

� Accept each process name, i.e. Read p[i].name

� Accept each process burst time, i.e. Read p[i].burst

� Copy the burst time to p[i].remain = p[i].burst

� Initialize waiting time, p[i].wait = 0

2. RR( ): function to implement round robin scheduling algorithm and calculates the waiting

time and turn around time for each process

• count = 0, time_q=0, twt=0, ttat=0;

• Repeat the following,

� For i=0 to N-1do,

� temp=tq

� if (p[i].remain=0)

count=count+1, continue

� if (p[i].remain>tq)

p[i].remain= p[i].remain-tq

� else if (p[i].remain>=0)

Page 21: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

temp=p[i].remain, p[i].remain=0;

� time_q=time_q+temp, p[i].TAT=time_q

� if (N = count) then break;

• for i=0 to N-1 do,

� p[i].wait=p[i].TAT-p[i].burst, //waiting of ith process

� twt =twt+p[i].wait, //total waiting time

� ttat=ttat+p[i].TAT //total turn around time

Note: The following example shows the way to display the output on screen-

Sample Input and Output:

/*

1. SJF

2. Round_Robin

3. EXIT

Enter your choice : 1

Enter the total number of procedure : 5

Enter the procedure name : 1

Enter the arrival time : 0

Enter the burst time : 8

Enter the procedure name : 2

Enter the arrival time : 1

Enter the burst time : 1

Enter the procedure name : 3

Enter the arrival time : 2

Enter the burst time : 3

Enter the procedure name : 4

Enter the arrival time : 3

Enter the burst time : 2

Enter the procedure name : 5

Enter the arrival time : 4

Enter the burst time : 6

Page 22: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

0-P1-1-P2-2-P3-3-P3-4-P3-5-P4-6-P4-7-P5-8-P5-9-P5-10-P5-11-P5-12-P5-13-P1-14-P1-15-P1-16-

P1-17-P1-18-P1-19-P1-20

NAME Arrival Burst Wait Turn_Arround_Time

P1 0 8 12 20

P2 1 1 0 1

P3 2 3 0 3

P4 3 2 2 4

P5 4 6 3 9

*/

11. Program definition:

Using OpenMP, Design, develop and run a multi-threaded program to generate and print

Fibonacci Series. One thread has to generate the numbers up to the specified limit and another

thread has to print them. Ensure proper synchronization.

Aim:

The aim of this problem is to generate print Fibonacci Series using a multi-threaded program

where one thread has to generate the numbers up to the specified limit and another thread has to

print them while ensuring proper synchronization.

Implementation:

Input: No. to terms which have to be generated (n)

Output: Fibonacci Series up to the specified limit

List of other variables : a[n] – holds the elements of Fibonacci series

Algorithm :

1. [input the value of n]

Read n

2. [indicate the required no. of threads]

Page 23: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Call omp_set_num_threads(2)

3. [initialize]

a[0]=0

a[1]=1

3. [generate the fibonacci numbers]

Use #pragma omp parallel directive to run the following structured block

{

Use #pragma omp single directive to run the following block using one thread

For i=2 to n-1

a[i]=a[i-2]+a[i-1];

using omp_get_thread_num( ) display the id of thread involved in the

computation of ith fib number

end for

Use #pragma omp barrier directive to synchronize the threads

Use #pragma omp single directive to run the following block using one thread

For i=2 to n-1

Display the fibonacci elements along with the thread id

End for

}

4. [finished]

Page 24: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

12. Program definition:

Design, develop and run a program to implement the Banker’s Algorithm. Demonstrate its

working with different data values.

AIM:

The aim of this problem is to design and develop a program to avoid deadlock using Banker’s

Algorithm. This algorithm is used to avoid deadlock in a system where every resources have

multiple instances. It will generate a safe sequence, if possible, using which resources should be

allocated to each process and execute them otherwise restart the system.

Implementation:

Several data structures must be maintained to implement Banker’s algorithm. Assume there are

‘p’ processes and ‘r’ resource types in the system,

1. avail[r] – no. of available resources of each type; if avail[j] = k, there are k instances of

resource type j

2. claim[p][r] – p × r matrix defines the maximum claim of each process of each resource

type

3. alloc[p][r] - p × r matrix defines the number of each resource type that are currently

allocated to each process;

4. req[p][r] - p × r matrix indicates the remaining resource need of each process;

req[p][r] = claim[p][r] – alloc[p][r];

Input:

1. number of processes, p and number of resource types, r

2. claim[p][r] and alloc[p][r] matrix

3. total number of available resources of each type, rsrc[r]

Output:

Display whether allocation of resources will be safe or not

Page 25: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

Pseudo code:

• initialize, count = 0, k = 0

• initialize, for i = 1 to p do, comp[i] = 0

• [calculate, available resources of each type]

for j = 1 to r do,

total = 0

avail[j] = 0

for i = to p do

total = total + alloc [i][j]

avail[j] = rsrc[j] – total

• [Initialize, temporary variable work[] to avail[]]

for j=1 to r do,

work[j]=avail[j];

• [calculate the need of each process i.e. req[p][r] matrix]

for i=1 to p do,

for j = 1 to r do,

req[i][j] = claim [i][j] – alloc [i][j]

• Repeat the following until (count!=p && k<2)

� Increment k

� for i =1 to p do,

for j = 1 to r do,

if(comp[i] =0) //process Pi is not completed

if(req[i][j] <= work[j]) //need of Pi is less than available resouces

� work[j] = work[j] + alloc[i][j]

� comp[i] = 1

� alloc[i][j] 0, claim[i][j] = 0

� Increment count

else break;

Page 26: V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...

PESIT-BSC SS&OS Lab (10CSL58)

• if (count != p)

Print “system is in an unsafe state”

else

Print “system is in a safe state”