Professional Documents
Culture Documents
V SEMESTER
LAB MANUAL
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.
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
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
Programs
PART A
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
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.
$ 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.
$ lex 2a.l
$ cc lex.yy.c –ll
$ ./a.out
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 ‘,’
Compilation steps
$ lex 3p.l
$ cc lex.yy.c –ll
$ ./a.out
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:
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”
$ 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 (anbn, 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
Compilation steps
$ lex 6p.l
$ yacc 6p.y
$ cc lex.yy.c y.tab.c –ll -ly
$ ./a.out
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:
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:
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.
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:
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:
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:
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:
Implementation:
Pseudo code:
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,
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
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
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:
Algorithm :
1. [input the value of n]
Read n
2. [indicate the required no. of threads]
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
• 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;
•