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
26
Embed
V SEMESTER LAB MANUAL SUBJECT: SS & OS LAB SUBJECT ...
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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).
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
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
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
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
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:
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”
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”
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
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.
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
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:
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,
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