Lab Manual
Lab Manual
T.E.COMP
System Programming & Operating System Lab
Laboratory Manual
Computer Engineering
Laboratory Objectives:
1. Understandand develop programs in LEX & YACC for illustrating Lexical and
syntax analysis phase of compiler.
2. To implement different scheduling algorithms.
3. To implement language translators(system software) like assembler and macro-
processor.
GROUP B
Assignment no.:-B2 and B3
TITLE:Lexical analyzer for sample language using LEX
OBJECTIVES:
Understand the importance and usage of LEX automated tool.
PROBLEM STATEMENT:
Implement a lexical analyzer for a sample languageusing LEX Implementation
THEORY:
As the first phase of a compiler, the main task of the lexical analyzer is to read the input
characters of the source program, group them into lexemes, and produce as output a sequence of
tokens for each lexeme in the source program.
Additionally, it willfillter out whatever separates the tokens (the so-called white-space), i.e., lay-
out characters (spaces, newlines etc.) and comments.
REGULAR EXPRESSIONS
Regular expressions in lex are composed of metacharacters (Table 1). Pattern-matching
examples are shown in Table 2. Within a character class, normal operators lose their meaning.
Two operators allowed in a character class are the hyphen (“-”) and circumflex (“^”). When
used between two characters, the hyphen represents a range of characters. The circumflex, when
used as the first character, negates the expression. If two patterns match the same string, the
longest match wins. In case both matches are the same length, then the first pattern listed is used.
Table 1: Pattern Matching Primitives
3) yywrap() :- This function is used for taking i/p from more than one file.
3) yyin :- This is input file pointer used to change value of input file pointer. Default file
pointer is pointing to stdin i.e. keyboard.
4) yyout :- This is output file pointer used to change value of output file pointer. Default
output file pointer is pointing to stdout i.e. Monitor.
How to execute LEX program :- For executing LEX program follow the following
steps
OBJECTIVES:
1. To understand Second phase of compiler: Syntax Analysis.
2. Understand the importance and usage of YACC automated tool.
PROBLEM STATEMENT:
Write a program using YACC specification to implement syntax analysis phase of compiler.
THEORY:
1) YACC Specifications: -
Parser generator facilitates the construction of the front end of a compiler. YACC is
LALR parser generator. It is used to implement hundreds of compilers. YACC is
command (utility) of the UNIX system. YACC stands for “Yet Another Compiler
Complier”.
File in which parser generated is with .y extension. e.g. parser.y, which is containing
YACC specification of the translator. After complete specification UNIX command.
YACC transforms parser.y into a C program called y.tab.c using LR parser. The program
y.tab.c is automatically generated. We can use command with –d option as yacc –d
parser.y. By using –d option two files will get generated namely y.tab.c andy.tab.h.
The header file y.tab.h will store all the token information and so you need not have to
create y.tab.h explicitly. The program y.tab.c is a representation of an LALR parser
written in C, along with other C routines that the user may have prepared. By compiling
y.tab.c with the ly library that contains the LR parsing program using the command cc
y.tab.c -ly. We obtain the desired object program a.out that perform the translation
specified by the original program. If procedure is needed, they can be compiled or loaded
with y.tab.c, just as with any C program.
LEX recognizes regular expressions, whereas YACC recognizes entire grammar. LEX
divides the input stream into tokens, while YACC uses these tokens and groups them
together logically.LEX and YACC work together to analyze the program syntactically.
The YACC can report conflicts or ambiguities (if at all) in the form of error messages.
In declaration section, % { and %} symbol used for C declaration. This section is used for
definition of token, associativity and precedence of operator. The statement between % { and
% } is passed as it is to C program, normally used for comments.
Built-in Types :
%token
Used to declare the tokens used in the grammar. The tokens that are declared in the
declaration section will be identified by the parser.
Eg.:- %token NAME NUMBER
%start
Used to declare the start symbol of the grammar.
Eg.:- %start STMT
%left
Used to assign the associatively to operators.
Eg.:- %left ‘+’ ‘-‘ - Assign left associatively to + & – with lowest precedence.
%left ‘*’ ‘/‘ - Assign left associatively to * & / with highest precedence.
%right
Used to assign the associatively to operators.
Eg.:- 1) %right ‘+’ ‘-‘
- Assign right associatively to + & – with lowest precedence
2) %right ‘*’ ‘/‘
- Assign right left associatively to * & / with highest precedence.
%nonassoc
Used to un associate.
Eg.:- %nonassoc UMINUS
%prec
Used to tell parser use the precedence of given code.
Eg.:- %prec UMINUS
%type
Used to define the type of a token or a non-terminal of the production written in the rules
section of the .Y file.
Eg.:- %type <name of any variable>exp
CONCLUSION:
Group:- c
Assignment no.:-C1
TITLE: Process Scheduling Algorithms.
OBJECTIVES:
1. To understand cpu scheduling algorithms.
PROBLEM STATEMENT:
Write a java program to implement scheduling algorithm like FCFS, SJF, Priority, Round Robin.
OUTPUT: It will generate the total turn around time and waiting time.
THEORY:
CPU scheduling deals with the problem of deciding which of the processes in the ready queue is
to be allocated the CPU.
First come, first served:
With this scheme, the process that requests the CPU first is allocated the CPU first. Consider the
following set of processes that arrive at time 0, burst time is given.
Process Burst Time
P1 24
P2 3
P3 3
If the processes arrive in the order p1,p2,p3 and served in FCFS order, then Gantt Chart is :
P1 p2 p3
0 24 27 30
Thus, the average waiting time is ( 0+24+27)/3 = 17 ms.
Shortest Job First:
This algorithm associates with each process the length of the latter’s next CPU burst. When the
CPU is available, it is assigned to the process that has the smallest next CPU burst. If the two
process have the same length next CPU burst, FCFS scheduling is used to break the tie.
Consider the following set of processes.
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Using SJF scheduling, we would schedule these processes according to the following Gantt
Chart :
P4 p1 p3 p2
0 3 9 16 24
Thus, the average waiting time is ( 3 + 16 + 9 + 0)/4 = 7 ms.
Preemptive SJF
When new process arrives at the ready queue while previous process is executing, the new
process may have a shorter next CPU burst than what is left of the currently executing process,
preemptive algo. will preempt the currently executing process.
E.g.
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Corresponding Gantt Chart :
P1 p2 p4 p1 p3
0 1 5 10 17 26
The avg. waiting time for this example is ((10 – 1)+(1 – 1) + (17 – 2) + (5 – 3))/4 = 6.5 ms.
Priority Scheduling :
A prority is associated with each process, and the CPU is allocated to the process with the
highest priority. Equal priority processes are scheduled in FCFS order.
Using priority scheduling, we would schedule processes according to the following Gantt chart
P2 p5 p1 p3 p4
01 6 16 18
01 6 16 18 19
P1 p2 p3 p1 p1 p1 p1 p1
0 4 7 10 14 18 22 26 30
Assignment no.:-C2
TITLE:Banker’s Algorithm
OBJECTIVES:
1. To understand dead lock and dead lock avoidance concepts.
PROBLEM STATEMENT:
Write a java program to implement Banker’s Algorithm.
THEORY:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types
Available :
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type
Rj
Need :
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Banker’s algorithm consist of Safety algorithm and Resource request algorithm
Safety Algorithm
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti[j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following
actions are taken:
Example:
Considering a system with five processes P0 through P4 and three resources types A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:
Need Matrix :
process P1 requests one additional instance of resource type A and two instances of resource
type C
We must determine whether this new system state is safe. To do so, we again execute Safety
algorithm on the above data structures.
After applying Safety algorithm, the new system state is safe, (safe sequence P1, P3, P4, P0, P2)
so, we can immediately grant the request for process P1 .
Assignment no.:C3
TITLE:Process Management in UNIX Sytsem
OBJECTIVES:
1. To understand the basic UNIX system calls for process.
PROBLEM STATEMENT:
Implement Unix System Calls like fork, ps, join, exec family and wait for process management
SOFTWARE REQUIRED:Linux Operating Systems, GCC or java editor and jdk s/w
THEORY:
A process is an executing program. A process is created whenever the user gives a command to
computer and the process is given a unique number i.e. known as process ID or simply PID.
System Calls for Process Control
fork():
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
The exec family of functions replaces the current process image with a new process
image.
Commonly a process generates a child process because it would like to transform the
child process by changing the program code the child process is executing.
The text, data and stack segment of the process are replaced and only the u (user) area of
the process remains the same.
If successful, the exec system calls do not return to the invoking program as the calling
image is lost.
It is possible for a user at the command line to issue an exec system call, but it takes over
the current shell and terminates the shell.
OBJECTIVES:
1. To understand and compare page replacement methods.
PROBLEM STATEMENT:
Write a java program to implement page replacement policies like LRU (Least Recently Used),
Optimal and FIFO.
OUTPUT: It will generate the page faults , page hits , fault ratio and hit ratio.
THEORY:
Paging is a memory management scheme that permits the physical address space of a process to
be non-contiguous.
In paged memory management each job’s address space is divided into equal no of pieces
called as pages and physical memory is divided into pieces of same size called as blocks or
frames.
Whenever there is a page reference for which the page needed is not in memory that
event is called as page fault.
Suppose all page frames are occupied and new page has to be brought in due to page
fault, in such case we have to make space in memory for this new page by replacing any existing
page. There are several algo. or policies for page replacement.
FIFO page replacement:
When a page must be replaced, the oldest page is chosen.
Consider the reference string 4, 3, 2, 1, 4, 3, 5, 4, 3, 2, 1, 5 with memory of three references
Ref. String 4 3 2 1 4 3 5 4 3 2 1 5
4 4 4 1 1 1 5 5 5 5 5 5
3 3 3 4 4 4 4 4 2 2 2
2 2 2 3 3 3 3 3 1 1
Fault + + + + + + + + +
Page Fault = 9 hit ratio = 3/12 , fault ratio=9/12
Algo.is affected by the Belady’s anomaly ( the page fault rate may increase as the number of
allocated frames increases.)
Optimal page replacement:
The basic idea of this algo is to replace the page that will not be used for the longest period of
time. It has the lowest page fault of all algo. and will never suffer from the Belady’sanamoly.
Consider the string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 with three memory frames.
Ref. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
string
7 7 7 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 7 7
0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1
Fault + + + + + + + + +
Page fault = 9 Hit ratio = 11/20 , fault ratio=9/20
This algo is difficult to implement because it requires further knowledge of reference string
LRU page replacement:
We will replace the page that has not been used for the longest period of time. This algo.also
never suffers from Belady’sanamoly.
Consider the string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 with three memory frames
Ref. 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
string
7 7 7 2 2 2 2 4 4 4 0 0 0 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 3 3 3 3 3 3 0 0 0 0 0
1 1 3 3 3 2 2 2 2 2 2 2 2 2 2 7 7 7
Fault + + + + + + + + +
Page fault = 9 Hit ratio = 11/20, fault ratio=9/20