SS and OS Lab Manual 2013(VTU)

May 4, 2017 | Author: anandyelwal | Category: N/A
Share Embed Donate


Short Description

VTU ( Vishveshwariya Technological University ) LAB MANUAL FOR 5th SEM CSE SS and OS lab manual...

Description

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

PART - A LEX and YACC Programs: Design, develop, and execute 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 C program. Also eliminate them and copy the resulting program into separate file. 2. a) Program to recognize a valid arithmetic expression and to recognize 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. Design, develop, and execute 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 (anbn, n>= 0). 6. Program to recognize the grammar (anb, n>= 10). PART B UNIX Programming: Design, develop, and execute the following programs: 7. a) Non-recursive shell script that accepts any number of arguments and prints them in the 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).

1

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

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 RoundRobin algorithm. In all cases, determine the average turn-around time. The input can be read from key board or from a file. 11. 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. 12. Design, develop and run a program to implement the Banker’s Algorithm. Demonstrate its working with different data values. Instructions: In the examination, a combination of one LEX and one YACC problem has to be asked from Part A for a total of 30 marks and one programming exercise from Part B has to be asked for a total of 20 marks.

2

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

PART - A LEX PROGRAMS: 1) a. Program to count the number of characters, words, spaces and lines in a given input file. //1a.l %{ int wc=0,cc=0,lc=0,sc=0; %} %option noyywrap %% [^ \t\n]+ {wc++;cc+=yyleng;} [ ]+ {sc++;} [\n]+ {lc++;} %% main(int argc,char *argv[]) { FILE *fp; if(argc==2) { fp=fopen(argv[1],"r"); yyin=fp; } yylex(); printf("Number of character =%d\n",cc); printf("Number of words =%d\n",wc); printf("Number of spaces =%d\n",sc); printf("Number of lines =%d\n",lc); } OUTPUT: $ lex 1a.l $ cc lex.yy.c –ll $ ./a.out Hai dear you there Number of character =15 3

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

Number of words =4 Number of spaces =3 Number of lines =1 1) b.Program to count the numbers of comment lines in a given C program. Also eliminate them and copy the resulting program into separate file. //1b.l %{ int c=0; %} %option noyywrap %x CMT %x ct %% “//” .\n .

{ BEGIN ct;} {c++;BEGIN INITIAL ;} ;

"/*" \n .\n "*/" .

{BEGIN CMT;} {c++;} {c++;} {c++; BEGIN INITIAL;} ;

%% main(int argc,char * argv[]) { FILE *fp1,*fp2; if(argc==3) { fp1=fopen(argv[1],"r"); fp2=fopen(argv[2],"w"); yyin=fp1; yyout=fp2; } yylex(); printf("Number of comment lines = %d\n",c); } OUTPUT: $ lex 1b.l $ cc lex.yy.c –ll $ gedit 1.c $ ./a.out 1.c 1b.c Number of comment lines =2

4

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58 1.c

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

main() 1b.c main() { { int a,b; /* int declaration */ int a,b; float amt,avg; /* float declaration */ float amt,avg; } }

2) a. Program to recognize a valid arithmetic expression and to recognize the present. Print them separately. //2a.l

identifiers and operators

%{ int pc=0,state=0,i=0,j=0; enum{FIRST=0,OP,CP,OPRND,OPRT}; char a[20]; %} %option noyywrap %% [a-zA-Z][a-zA-Z0-9]*{ if(state==FIRST||state==OP||state==OPRT) { state=OPRND; printf("operand %d=%s\n",++i,yytext); } else return 0; } [+\-*/]

{ if(state==OPRND||state==CP) { state=OPRT; a[j++]=yytext[0]; } else return 0; }

\(

{ if(state==FIRST||state==OPRT||state==OP) { state=OP; pc++; } else return 0; }

5

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

\)

{ if(pc=ident) { printf("Invalid expression\n"); } else { printf("valid expression"); printf("\nAdd=%d\nSub=%d\nMul=%d\nDiv=%d\n",a,s,m,d); printf("Operators are: \n"); if(flaga) printf("+\n"); if(flags) printf("-\n"); if(flagm) printf("*\n"); if(flagd) 7

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

printf("\\ \n"); } return 0; } OUTPUT: Enter the expression: A+B A is an identifier B is an identifier Ctrl+d valid expression Add=1 Sub=0 Mul=0 Div=0 Operators are: + 2) b. Program to recognize whether a given sentence is simple or compound.

//2b.l

%{ int flag=0; %} %option noyywrap %% "and" | "AND" | "or" | "OR" | "if" | "IF" | "else" | "ELSE" | "not" | "NOT" | "but" | "BUT" . \n

{flag=1;} ; {return;}

%% main() { yylex(); 8

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

if(flag==0) printf("Sentence is Simple\n "); else printf("Sentence is Compound\n"); } OUTPUT: $ lex 2b.l $ cc lex.yy.c –ll $./a.out Hai dear you there Sentence is Simple

$ lex 2b.l $ cc lex.yy.c –ll $./a.out Hai dear I am not cumg Sentence is Compound

3.Program to recognize and count the number of identifiers in the given input file.

//3.l

%{ int idc=0; %} %option noyywrap %x IDENT ID [a-zA-Z][a-zA-Z0-9]* DTYPE "int "|"float "|"char " %% {DTYPE} { [^ ,]{ID} ; {ID}"," { {ID}";" {

BEGIN IDENT;

}

idc++; } idc++;BEGIN INITIAL; }

%% main(int argc,char*argv[]) { FILE *fp; if(argc==2) { fp=fopen(argv[1],"r"); yyin=fp; yylex(); printf(" Number of identifiers is = %d\n",idc); } else printf("input file is missing"); } OUTPUT: $ lex 3.l 9

3.c Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

$ cc lex.yy.c –ll $./a.out 3.c Number of identifiers is = 5

V SEM CSE

main() { int abc,sum,a; float avg,amt; }

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

// 4a.y

%{ #include #include %} %token DIG LET %left '+''-' %left '*''/' %% line:

exp:

num:

lts:

line exp '\n' | | line '\n' | error '\n' ; exp '+' exp | exp '-' exp |exp '*' exp |exp '/' exp |'(' exp ')' | num | lts ;

{

printf("valid Expression\n"); }

{

yyerror("invalid Expression\n");yyerrok;

}

DIG | num DIG ; LET | lts LET | lts DIG ;

%% main() { yyparse(); } yyerror(char*s) 10

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

{ printf("%s\n",s); }

yylex() { int c; while((c=getchar())==' '); if(isdigit(c)) return DIG; if(isalpha(c)) return LET; return c; } OUTPUT: $ yacc 4a.y $ cc y.tab.c $ ./a.out (a+b)*c valid Expression a+(b+c syntax error invalid Expression a++b syntax error invalid Expression a+b*c valid Expression a+b*c a*b+c syntax error invalid Expression 4.b) Program to recognize a valid variable, which starts with a letter followed by any number of letters or digits. //4b.y %{ #include #include %} %token DIG LET %% 11

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58 line:

var:

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

line var '\n' | | line '\n' | error '\n' ; LET | var LET | var DIG ;

{

printf("\n valid Variable\n"); }

{

yyerror("\ninvalid variable\n");yyerrok

V SEM CSE

}

%% main() { yyparse(); } yyerror(char*s) { printf("%s\n",s); } yylex() { char c; while((c=getchar())==' '); if(isdigit(c)) return DIG; if(isalpha(c)) return LET; return c; } OUTPUT: $ yacc 4b.y $ cc y.tab.c $ ./a.out abc23 valid Variable a123 valid Variable 133asd syntax error invalid variable 5.a) Program to evaluate an arithmetic expression involving operators +,-,*,/.

// 5a.y

%{ #include #include %} 12

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

%token DIG %left '+' '-' %left '*' '/' %left UMINUS %% line : line exp '\n' { | | line '\n' | error '\n' { ; exp

: exp '+' exp | exp '-' exp |exp '*' exp |exp '/' exp

{ { { {

printf("Res=%d\n",$2);

V SEM CSE

}

yyerror("Invalid Exp:\n");yyerrok; }

$$=$1+$3; $$=$1-$3; $$=$1*$3;

} } }

if($3==0) { printf("\n Divide by 0 error\n"); return 0; } else $$=$1/$3; } | '-'exp %prec UMINUS | num ; num

: DIG | num DIG ;

{ {

{

$$=-$2;

}

$$=$1; } $$=$1*10+$2; }

%% main() { yyparse(); } yyerror(char *s) { printf("%s\n",s); } yylex() { int c; while((c=getchar())==' '); if(isdigit(c)) { 13

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

yylval=c-'0'; return DIG; } return c; } OUTPUT: $ yacc 5a.y $ cc y.tab.c $ ./a.out 3+4+5 Res=12 7-4+ Invalid Exp: 4/0 Divide by 0 error 5.b) Program to recognize a valid string 'aaab','abbb','ab' and 'a' using the grammer {anbn,n>=0}. //5b.y %{ #include #include %} %token A B %% line : line str '\n' { printf("\n valid expression\n"); } | | error '\n' { yyerror("\ninvalid expression\n"); yyerrok; } ; str : | A str B ; %% main() { yyparse(); } yyerror(char *s) { printf("%s\n",s); } yylex() { int c; while((c=getchar())==' '); if(c=='a') return A; 14

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

if(c=='b') return c;

V SEM CSE

return B;

}

OUTPUT: $ yacc 5b.y $ cc y.tab.c $ ./a.out ab valid expression aaab syntax error invalid expression abbb syntax error invalid expression a syntax error invalid expression 6. Program to recognize the grammer {anb,n>=10} // 6.y %{ #include #include %} %token A B %% line : line str '\n' { printf("\n valid expression\n"); } | | line '\n' | error '\n' { yyerror("\ninvalid expression\n"); yyerrok; } ; str

:AAAAAAAAAAB | A str ;

%% main() { yyparse(); } yyerror(char *s) 15

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

{ printf("%s\n",s); }

yylex() { int c; while((c=getchar())==' '); if(c=='a') return A; if(c=='b') return B; return c; } OUTPUT: $ yacc 6.y $ cc y.tab.c $ ./a.out aaaaaaaaaab valid expression ab syntax error invalid expression aaaaaaaaaaaaaaab valid expression

16

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

PART B UNIX PROGRAMS: 7.a) Non-recursive shell script that accepts any number of argument and prints them in the 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). //7a.sh if [ $# -eq 0 ] then echo "No arguments" else for i in "$@" do rev=$i' '$rev done echo "The Reverse string is $rev" fi OUTPUT: $ chmod 777 7a.sh

$sh 7a.sh A B C The Reverse string is C B A

OR The Reverse string is C B A

7.b) C program that creates a child process to read commands from the standard input and execute them(minimal implementation of shell like program). You can assume that no arguments will be passed to the commands to be executed. //7b.c #include #include #include #include int main() { pid_t pid; char cmd[10]; pid=fork(); if (pid==0) { printf("Enter a UNIX command\n"); scanf("%s",cmd); execlp(cmd,cmd,(char*)0); exit(0); 17

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

} waitpid(pid,0,0); return 0; } OUTPUT: $ cc 7b.c $ ./a.out Enter a UNIX command date Tue Aug 12 15:06:17 IST 2012 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. //8a.sh if [ $# -ne 2 ] then echo "no arguments pls enter 2 arguments" elif [ ! -e $1 -o ! -e $2 ] then echo "File does not exist" else per1=`ls -l $1 | cut -c 2-10` per2=`ls -l $2 | cut -c 2-10` if [ $per1 = $per2 ] then echo "Permissions are Identical:permission is $per1" else echo "Permissions are not Identical" echo "permission of $1 is $per1" echo "permission of $2 is $per2" fi fi OUTPUT: $ sh 8a.sh no arguments pls enter 2 arguments

1.c This is System Software Lab

$ sh 8a.sh 1.c 2.c Permissions are Identical:permission is rw-r—r— $ sh 8a.sh 1.c 2.c //if 1.c or 2.c doesn’t created means File does not exist

2.c HKBK college of Engineering

$ chmod 777 1.c 18

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

$ chmod 770 2.c $ sh 2a.sh 1.c 2.c Permissions are not Identical permission of 1.c is rwxrwxrwx permission of 2.c is rw-r--r-8.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. // 8b.c #include #include #include #include int main() { char a[]="1111111111111111"; char b[]="2222222222222222"; int fd; fd=open("hole",O_CREAT|O_WRONLY,0777); write(fd,a,16); lseek(fd,48,SEEK_SET); write(fd,b,16); close(fd); return 0; } OUTPUT: $ cc 2b.c $ ./a.out $ od -bc hole 0000000 061 061 061 061 061 061 061 061 061 061 061 061 061 061 061 061 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0000020 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 * 0000060 062 062 062 062 062 062 062 062 062 062 062 062 062 062 062 062 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0000100

9.a) Shell script that accepts filenames 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 . //9a.sh if [ $# -eq 0 ] then 19

Prepared By: B. Fakruddin , Dept of CSE, HKBKCE

2013

10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL

V SEM CSE

echo "No Arguments" exit fi for i in "$@" do

if [ ! -e $i ] then echo "$i not existing" else echo "cat >$i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF