SS & OS Lab Manual

22
SS AND OS LAB PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 1 PESIT BANGALORE SOUTH CAMPUS 1Km before Electronic City, Hosur Road, Bangalore-560100. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING V SEMESTER LAB MANUAL SUBJECT: SYSTEM SOFTWARE AND OPERATING SYSTEM LAB SUBJECT CODE: 10CSL58 SESSION: JULY 2016 – DECEMBER 2016 FACULTY : Mrs. Shanthala P T/Dr. Sarasvathi

Transcript of SS & OS Lab Manual

Page 1: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 1

PESIT BANGALORE SOUTH CAMPUS

1Km before Electronic City, Hosur Road, Bangalore-560100.

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

V SEMESTER

LAB MANUAL

SUBJECT: SYSTEM SOFTWARE AND OPERATING SYSTEM LAB

SUBJECT CODE: 10CSL58

SESSION: JULY 2016 – DECEMBER 2016

FACULTY : Mrs. Shanthala P T/Dr. Sarasvathi

Page 2: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 2

Lab sessions

Programs to be execute

1

PART A

Practice lab Unix commands

2 LEX: program 1a and 1b

3 Program 2a and 2b

4 Program 3 and YACC: Program 4a

5 Yacc Program 4b and 5a

6 Yacc 5b and 6

7

PART B

UNIX: program 7a,7b

8 Program 8a and 8b

9 Program 9a and 9b

10 Program 10

11 Program 11

12 Program 12

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 and 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: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 3

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

Part B

Unix Programming:

7) 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. 8) 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. 9) 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 and then exits.

OPERATING SYSTEMS: 10. 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.

11. 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.

12. Design, develop and run a program to implement the Banker’s Algorithm. Demonstrate its working with different data values.

Page 4: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 4

INSTRUCTIONS:

Working environment: ubuntu( unix operating system) Programming language: lex and yacc and including C. Basic programming knowledge needed: C. Open a file by using vi editor or geditor. Commands to open a file and save a file; in vi editor. 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 escape 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 $./a.out

1. Program Definition:

Page 5: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 5

1) 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 encounters increment the spacecounter(sc). - In the input whenever a line character is encountered increment the linecounter(lc).

3) In the subroutine section - 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.

Input: some sentences with words, spaces and new lines

------------------------------------------------------------------------------------------------------------------

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 coment lines if 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.

Page 6: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 6

- 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. -----------------------------------------------------------------------------------------------------------------------

2a) 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 intialise 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.

-----------------------------------------------------------------------------------------------------------------------

2b) Program to recognize weather the given sentence is simple or compound

Pseudo code:

Input: a valid sentence

Outout: display whether the input sentence is simple or compound

Logic:

o Enter a sentence. o Checkwhether the sentence contain ;and’,;or’,’because’,’but’ etc.. in between .

If so..

Print”compound”

Else

“simple”

Page 7: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 7

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

Aim: here from the given C file we need to count the number od different identifiers present .

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 starta with long | char or int or float or double or unsigned or short and followed a character string and followed by ‘,’ 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.

-----------------------------------------------------------------------------------------------------------------------

YACC PROGRAMS

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

Aim: The main aim of this program is to recognize weather a given arithmetic expression

is valid or not.

• Arithmetic expression: number operator number

• Simple expression: (number or id) operator (number or id)

Pseudo code:

1).Definition section: Declare the tokens used in the program.

2). Rule section: proper grammar rule to recognize the expression is valid or not.

3). User section: call the function yyparse().

-----------------------------------------------------------------------------------------------------------------------

4b). Program to recognize a valid Variable, which starts with a letter, followed by any

number of letters or digits.

Aim: the main aim of this program to recognize the valid variable and variable should be start

with at least one letter then followed by any number of letters or digits

Pseudo code:

1).Definition section: Declare the tokens used in the program.

Page 8: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 8

2). Rule section: proper grammar rule to recognize the valid variable is valid or not.

3). User section: call the function yyparse().

-----------------------------------------------------------------------------------------------------------------------

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

Aim: here we are going to evaluate an arithmetic expression.

Some points:

Every symbol in yacc parser has a value. value gives an additional information about a particular instance of a symbol.

If symbol�num value is particuler number.

| literal, the value be a pointer to a copy of the string.

| variable in a program , the value would be a pointer to symbol table entry describing the variable.

Nonterminals can have any values, but action code builds a parse tree corresponding to the input.

Statement�NAME=expression

Expression�NUMBER’+’expression

| NUMBER’-’expression

In this grammar, value of NUMBER and expression is the numerical value but value of name will be a symbol pointer table.

Different symbols uses different data types, if we have multiple value types, list all the value types used in a parser and yacc creates a C union typedef function YYSTYPE to contain them.

• Whenever yacc reduces a rule it executes user C code associated with the rule before ; or |.the action code can refer to the values of the RHS symbols as $1,$2 and etc… and can set LHS by setting $$.

• $1� first symbol

• $2� second symbol

• $3� third symbol etc..

• expr : expr '+' term { $$ = $1 + $3; }

• | term { $$ = $1; }

• ;

• term : term '*' factor { $$ = $1 * $3; }

• | factor { $$ = $1; }

• ;

• factor : '(' expr ')' { $$ = $2; }

• | ID

• | NUM

Page 9: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 9

• ;

Example:

%%

statement : expression { printf (“ = %g\n”, $1); }

expression : expression ‘+’ expression { $$ = $1 + $3; }

| expression ‘-’ expression { $$ = $1 - $3; }

| NUMBER { $$ = $1; }

: /* ; is optional but suggestion*/

%%

Pseudo code:

1).Definition section: Declare the tokens used in the program.

2). Rule section: proper grammar rule to recognize the valid variable is valid or not.

3). User section: call the function yyparse().

5 b. Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (a n b n, n>=0). Aim: to recognize the given grammar is valid or not.

Pseudo code:

1).Definition section: Declare the tokens used in the program.

2). Rule section: proper grammar rule to recognize the valid variable is valid or not.

3). User section: call the function yyparse().

6). Program to recognize the grammar (a n b, n>=10). Aim: to recognize the given grammar is valid or not.

Pseudo code:

1).Definition section: Declare the tokens used in the program.

2). Rule section: proper grammar rule to recognize the valid variable is valid or not.

3). User section: call the function yyparse().

PART B: Unix Programming:

� Shell Programming: Sumitabha Das, Unix Concepts and Applications, Yashwant Kanetkar, Unix Shell programming

� System Programming:

Page 10: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 10

W.Richard Stevens : Advanced Programming in the Unix Environment APIS: system, fork, wait Some pointers

� #!/bin/bash THis indicates that the script should be run in the bash shell regardless of which interactive shell the user has chosen. This is very important, since the syntax of different shells can vary greatly.

� Anything following a pound sign # is treated as a comment. � bash gets unhappy if you leave a space on either side of the = sign. Eg: X =

hello Error � The test command is typically abbreviated in this form:

[ operand1 operator operand2 ] Eg: if [ $X -lt $Y ] � Backtick expansion expands `commands` to the output of commands (Command

Substitution) � ‘ is strong quotes � “ is weak quotes � $# : Number of command line args � $* : The command line argument list

7) 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). eval [arg ...] The args are read and concatenated together into a single command. This command is then read and executed by the shell, and its exit status is returned as the value of eval.

� expr - evaluate expressions 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.

#include<stdio.h> main() { int status_p; char cmd[20]; if(fork()==0) { do { printf("\nEnter the command:"); scanf("%s",cmd); system(cmd); }while(strcmp(cmd,"exit")); }

Page 11: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 11

else wait(&status_p); }

----------------------------------------------------------------------------------------------------------------------- 8) 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.

� set -- Any arguments remaining after the options are processed are treated as values for the positional parameters and are assigned, in order, to $1, $2, ... $n.

[user@localhost ~]$ ls -l 2a.l -rw-r--r-- 1 user user 0 Mar 16 10:32 2a.l 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. #include<sys/stat.h> #include<sys/types.h> #include<stdlib.h> #include<stdio.h> int main() { char *cmd="od -bc test.dat"; char *fname="test.dat"; char *str1="ABCDEFGHIJKLMNOP"; char *str2="abcdefghijklmnop"; int fd; fd=creat(fname,S_IREAD|S_IWRITE); if(fd==-1) { printf("\nERROR IN CREATING FILE\n"); exit(1); } if(write(fd,str1,16)!=16) { printf("\nERROR IN WRITING\n"); exit(1); } lseek(fd,48,SEEK_SET); if(write(fd,str2,16)!=16) {

Page 12: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 12

printf("\nERROR IN WRITING 16 BYTES OF DATA AFTER OFFSET OF 48\n"); exit(1); } printf("\nTHE CONTENTS OF THE FILE ARE\n"); system(cmd); } ----------------------------------------------------------------------------------------------------------------------- 9) 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). #! /bin/bash echo "#to unbundle,bash this file" for i do echo "echo $i 1>&2" echo "cat >$i << 'End of $i'" cat $i echo "End of $i" done [user@localhost swetha]$ vi temp1.dat [user@localhost swetha]$ vi temp2.dat [user@localhost swetha]$ cat temp1.dat BCDEFG [user@localhost swetha]$ cat temp2.dat 12345678 [user@localhost swetha]$ ./7a.sh temp1.dat temp2.dat > temp.dat [user@localhost swetha]$ cat temp.dat #to unbundle,bash this file echo temp1.dat 1>&2 cat>temp1.dat <<'end of temp1.dat' BCDEFG end of temp1.dat echo temp2.dat 1>&2 cat>temp2.dat <<'end of temp2.dat' 12345678 end of temp2.dat [user@localhost swetha]$ the redirection operator [n]>&digit- Moves the file descriptor digit to file descriptor n, or the standard output (file descriptor 1) if n is

Page 13: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 13

not specified. <<word Read until word, substitute variables 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. #include <stdio.h>

#include <stdlib.h>

#include <sys/stat.h>

#include <sys/types.h>

int main()

{

pid_t ppid, mpid, pid, status = 0;

pid = fork();

If (pid < 0) {

printf(“error”);

exit(o);

}

If (pid == 0)

{

mpid = getpid();

printf(“\n I am child, my id is %d”, mjpid);

ppid = getppid();

printf(“\n I am parent, my id is %d”, ppid);

exit(1);

}

pid = waitpid(pid, &status, 0);

Mpid = getpid();

printf(“\I am parent with id %d and my child is %d”, mpid, pid);

}

-----------------------------------------------------------------------------------------------------------------------

Page 14: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 14

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:

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;

};

Page 15: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 15

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,

� 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;

Page 16: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 16

� 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,

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

Page 17: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 17

{

int name; //process name

int burst; //Burst time

int wait; //waiting time

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

Page 18: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 18

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

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

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

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

Page 19: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 19

Enter the procedure name : 5

Enter the arrival time : 4

Enter the burst time : 6

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 20: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 20

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]

--------------------------------------------------------------------------------------------------------------------

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:

Page 21: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 21

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

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,

Page 22: SS & OS Lab Manual

SS AND OS LAB

PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 22

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;

• if (count != p)

Print “system is in an unsafe state”

else

Print “system is in a safe state”

*******************************************************************************