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
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
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
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).
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.
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:
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.
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”
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.
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
SS AND OS LAB
PESIT –BSC SS and OS Lab Manual BE V-Sem CS, 10CSL58 - 9
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")); }
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) {
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
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);