0% found this document useful (0 votes)
52 views

Ss Os Manual

The document describes a laboratory course on system software and operating systems. It contains 12 experiments to be performed using Lex, Yacc and Unix programming. The Lex experiments include programs to count characters, words, lines in a file and programs to recognize arithmetic expressions and identifiers. The Yacc experiments include programs to recognize arithmetic expressions and variables. The Unix programming experiments include shell scripts and C programs involving processes. The document also describes experiments on scheduling algorithms and implementing the Banker's algorithm using C/C++.

Uploaded by

Manu Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Ss Os Manual

The document describes a laboratory course on system software and operating systems. It contains 12 experiments to be performed using Lex, Yacc and Unix programming. The Lex experiments include programs to count characters, words, lines in a file and programs to recognize arithmetic expressions and identifiers. The Yacc experiments include programs to recognize arithmetic expressions and variables. The Unix programming experiments include shell scripts and C programs involving processes. The document also describes experiments on scheduling algorithms and implementing the Banker's algorithm using C/C++.

Uploaded by

Manu Bhat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

BANGALORE INSTITUTE OF TECHNOLOGY


K.R. ROAD, V.V PURAM, BANGALORE – 560 004

(AFFILIATED TO VTU, BELGUAM)

CODE: 10CSL58

SYSTEM SOFTWARE & OPERATING SYSTEMS


LABORATORY.

FOR V SEMESTER CSE/ISE AS PRESCRIBED BY VTU

Academic year 2015-2016

Prepared By:

Prof. Shivakumar B R, M.Tech


Prof. Roopa H, M.Tech
Assistant Professors
Dept of ISE, BIT

DEPARTMENT OF
INFORMATION SCIENCE AND ENGINEERING

Dept of ISE Bangalore Institute Of Technology Page 1


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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.

Dept of ISE Bangalore Institute Of Technology Page 2


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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 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.

Dept of ISE Bangalore Institute Of Technology Page 3


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

PART – A

LEX PROGRAMS:

1) a. Program to count the number of characters, words, spaces and lines in a given
input file.

1a.l

%{
#include<stdio.h>
int lines=0,words=0,chars=0,blanks=0;
%}

%%
[\t] blanks++;
[\n] lines++;
[^ \t\n]+ {words++;chars+=yyleng;}
%%
yywrap()
{
printf("charcter=%d,lines=%d,blanks=%d,words=%d\n",chars,lines,blanks,words);
}

int main(int argc,char*argv[])


{
yyin=fopen(argv[1],"r");
yylex();
return 0;
}

OUTPUT:

[exam1@localhost ~]$ lex 1a.l


[exam1@localhost ~]$ ls
1a.l a.out a.txt lex.yy.c
[exam1@localhost ~]$ cc lex.yy.c -ll
[exam1@localhost ~]$ ./a.out a.txt
charcter=44,lines=3,blanks=7,words=10
[exam2@localhost ~]$ ./a.out
hello vijay kumar
charcter=15,lines=1,blanks=0,words=3

Dept of ISE Bangalore Institute Of Technology Page 4


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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 count=0,flag=0;
%}

%%
"/*" {
if(!flag)
{
flag=1;
fprintf(yyout," ");
}
}

[^ "*/"] {
if(flag==1)
fprintf(yyout," ");
else
fprintf(yyout,"%s",yytext);
}
"*/" {
if(flag)
{
flag=0;
fprintf(yyout," ");
count++;
}
}
%%

main(int argc,int *argv[])


{
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("The no. of comment lines=%d",count);
}

Dept of ISE Bangalore Institute Of Technology Page 5


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[exam1@localhost ~]$ lex 1b.l


[exam1@localhost ~]$ cc lex.yy.c –ll
[exam1@localhost ~]$ vi in.c
[exam1@localhost ~]$ ./a.out input.c output.c
Number of comment lines =2

input.c

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

output.c

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

Dept of ISE Bangalore Institute Of Technology Page 6


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

2. a) Program to recognize a valid arithmetic expression and to recognize the


identifiers and operators present. Print them separately.
2a.l

%{
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int oprt=0,opnd=0,bracket=0;
char ident[20][20],operator[10];
%}

%%
[+] {operator[oprt]='+'; oprt++;}
[-] {operator[oprt]='-'; oprt++;}
[*] {operator[oprt]='*'; oprt++;}
[/] {operator[oprt]='/';oprt++;}
[(] {bracket++;}
[)] {bracket--;}
[a-zA-Z][a-zA-Z0-9]* {strcpy(ident[opnd],yytext); opnd++;}
%%
main()
{
int i=0;
printf("Enter the exppression: ");
yylex();

printf("The no.of operators=%d\n",oprt);


printf("\n The operators are\t");
for( i=0;i<oprt;i++)
printf("%c\t",operator[i]);

printf("\n The no.of identifiers=%d\n",opnd);


printf("\n The identifiers are\t");
for(i=0;i<opnd;i++)
printf("%s\t",ident[i]);

if(bracket!=0 || (opnd+oprt)%2==0 || opnd ==0)


printf("\n Invalid expression \n");
else
printf("\n Valid Expression\n"); }
Dept of ISE Bangalore Institute Of Technology Page 7
SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[5is063@localhost ~]$ lex 2a.l


[5is063@localhost ~]$ cc lex.yy.c -ll
[5is063@localhost ~]$ ./a.out
enter the exppression: a+b*c/d

The no.of operators=3

The operators are + * /


The no.of identifiers=4

The identifiers are a b c d


Valid Expression

[5is063@localhost ~]$ ./a.out


Enter the exppression: a+b-

The no.of operators=2

The operators are + -


The no.of identifiers=2

The identifiers are a b


Invalid expression

Dept of ISE Bangalore Institute Of Technology Page 8


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

2. b) Program to recognize whether a given sentence is simple or compound.

%{
#include<stdio.h>
int simple=1;
%}

word [a-zA-Z]+
space [ ]+

%%
{word}{space}"and"|"but"|"or"{space}{word} {simple=0;}
%%

yywrap()
{
if(!simple)
printf("sentence is compound\n");
else
printf("sentence is simple\n");
}

int main()
{
printf("ENTER A SENTENCE : ");
yylex();
return 0;
}

OUTPUT:

[exam1@localhost ~]$ lex 2b.l


[exam1@localhost ~]$ cc lex.yy.c -ll
[exam1@localhost ~]$ ./a.out
ENTER A SENTENCE : Have a nice day
Have a nice day
sentence is simple
[exam1@localhost ~]$ ./a.out
ENTER A SENTENCE : you and me
sentence is compound

Dept of ISE Bangalore Institute Of Technology Page 9


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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


file.

3a.l

%{
int ident=0;
%}

ID [a-zA-Z][a-zA-Z0-9_()]*
DECLN "int"|"double"|"unsigned"|"char"|"long"
%x DEFN

%%
{DECLN} {BEGIN DEFN;}
<DEFN>{ID}, {ident++;}
<DEFN>{ID}; ident++; {BEGIN 0;}
<*>\n ;
<*>. ;
%%

main(int argc,char *argv[])


{
if(argc==2)
{
yyin=fopen(argv[1],"r");
yylex();
printf("\n Num of identifiers = %d\n",ident);
}
else
printf("\n Usage: %s<filename>\n",argv[0]);
}

OUTPUT:

[exam1@localhost ~]$ lex 3.l


[exam1@localhost ~]$ cc lex.yy.c -ll
[exam1@localhost ~]$ ./a.out input.c
Num of identifiers = 6

input.c

#include<stdio.h>
int i,ij;
double kld,uyt,t,y;
}

Dept of ISE Bangalore Institute Of Technology Page 10


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

PART – A

YACC PROGRAMS:

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


and /.

4a.l

%{
#include "y.tab.h"
%}

%%
[a-zA-Z] {return A;}
[\t] ;
\n return 0;
. return( yytext[0]);
%%

4b.y

%{
#include<stdio.h>
%}

%token A
%left '+''-'
%left '*''/'

%%
str: s
s: s'+'s
| s'-'s
| s'*'s
| s'/'s
| A;
%%

main()
{
printf("\n Type a Simple exp : \n");
if(!yyparse())
printf("\n Valid Simple exp : \n");
return 0;
}

Dept of ISE Bangalore Institute Of Technology Page 11


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

yyerror()
{
printf("\n Invalid simple expression \n");
exit(0);
}

OUTPUT:

[exam1@localhost ~]$ lex 4a.l


[exam1@localhost ~]$ yacc -d 4a.y
[exam1@localhost ~]$ cc lex.yy.c y.tab.c -ll
[exam1@localhost ~]$ ./a.out
Type a Simple exp :
a*b
Valid Simple exp :
[exam1@localhost ~]$ ./a.out
Type a Simple exp :
a*b(c*d
Invalid simple expression

Dept of ISE Bangalore Institute Of Technology Page 12


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

4. b) Program to recognize a valid variable, which starts with a letter, followed by


any number of letters or digits.
4b.l

%{
#include "y.tab.h"
%}

%%
[a-zA-Z][a-zA-Z0-9]* {return VAR;}
. return yytext[0];
\n return 0;
%%

4b.y

%{
#include<stdio.h>
%}

%token VAR
%%
str:VAR
%%
main()
{
printf("\n Print a Variable\n");
if(!yyparse())
printf("\n Valid variable\n");
}
yyerror()
{
printf("\n Invalid Variable \n");
}

OUTPUT:
[exam1@localhost ~]$ lex 4b.l
[exam1@localhost ~]$ yacc -d 4b.y
[exam1@localhost ~]$ cc lex.yy.c y.tab.c –ll
[exam1@localhost ~]$ ./a.out
Print a Variable
var
Valid variable
[exam1@localhost ~]$ ./a.out
Print a Variable
9var
Invalid Variable

Dept of ISE Bangalore Institute Of Technology Page 13


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

5. a) Program to evaluate an arithmetic expression involving operators +, -, * and /.

5a.l

%{
#include "y.tab.h"
#include<stdlib.h>
extern int yylval;
%}

%%
[0-9]+ {yylval=atoi(yytext);
return NUM;}
\n {return 0;}
. {return yytext[0];}
%%

5a.y

%{
#include<stdio.h>
%}

%{
int valid=1;
%}
%token NUM
%left '+''-'
%left '*''/'
%%
str:s {printf("\n Expression result:%d\n",$$);}
s:s'+'s {$$=$1+$3;}
|s'-'s {$$=$1-$3;}
|s'*'s {$$=$1*$3;}
|s'/'s {if($3==0)
{printf("Divide by zero error");
exit(0);}
$$=$1/$3;}
|'('s')' {$$=$2;}
|NUM {$$=$1;}
|'-'s {$$=-$2;}
|'+'s {$$=$2;}
%%

Dept of ISE Bangalore Institute Of Technology Page 14


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

main()
{
printf("Print a valid arithmetic expression\n");
yyparse();
}

yyerror()
{
printf("Invalid arithmetic expression\n");
}

OUTPUT:

[5is063@localhost ~]$ lex 5a.l


[5is063@localhost ~]$ yacc -d 5a.y
[5is063@localhost ~]$ cc lex.yy.c y.tab.c -ll
[5is063@localhost ~]$ ./a.out
Print a valid arithmetic expression
5+5

Expression result:10
[5is063@localhost ~]$ ./a.out
Print a valid arithmetic expression
9*9

Expression result:81

[5is063@localhost ~]$ ./a.out


Print a valid arithmetic expression
8+5/
Invalid arithmetic expression
[5is063@localhost ~]$ ./a.out
Print a valid arithmetic expression
9*3+
Invalid arithmetic expression

Dept of ISE Bangalore Institute Of Technology Page 15


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

5. b) Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar (anbn,
n>= 0).

5b.l

%{
#include "y.tab.h"
%}

%%
a return A;
b return B;
. return yytext[0];
\n return 0;
%%

5b.y

%{
#include<stdio.h>
%}

%token A B

%%
gra:Q
Q:AAM
| MBB
| A
| M;
M:AN
N:TB
T:M
|;
%%

main()
{
printf("\n Enter: \n");
if(!yyparse())
printf("\n Recognised \n");
}
yyerror()
{
printf("\n Not recognised \n");
}

Dept of ISE Bangalore Institute Of Technology Page 16


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[exam1@localhost ~]$ lex 5b.l


[exam1@localhost ~]$ yacc -d 5b.y
[exam1@localhost ~]$ cc lex.yy.c y.tab.c -ll
[exam1@localhost ~]$ ./a.out

Enter:
aabb

Recognised
[exam1@localhost ~]$ ./a.out

Enter:
abbaaba

Not recognised
[exam1@localhost ~]$

Dept of ISE Bangalore Institute Of Technology Page 17


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

6. Program to recognize the grammar (anb, n>= 10).

6.l

%{
#include"y.tab.h"
%}

%%
a return A;
b return B;
. return yytext[0];
\n return 0;
%%

6.y

%{
#include<stdio.h>
%}

%token A B

%%
start: Q B
Q:A A A A A A A A A D;
D:A D;
|;
%%

main()
{
printf("Enter the grammar\n");
if(!yyparse())
printf("VALID\n");
}
yyerror()
{
printf("Not Recognised\n");
}

Dept of ISE Bangalore Institute Of Technology Page 18


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[exam1@localhost ~]$ lex 6.l


[exam1@localhost ~]$ yacc -d 6.y
[exam1@localhost ~]$ cc lex.yy.c y.tab.c -ll
[exam1@localhost ~]$ ./a.out
Enter the grammar
aaaaaaaaab
VALID
[exam1@localhost ~]$ ./a.out
Enter the grammar
aaab
Not Recognised

Dept of ISE Bangalore Institute Of Technology Page 19


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

PART B

UNIX Programming:

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).

7a.sh

c=$#
if [ $c -eq 0 ]
then
echo -e "Please pass the arguments"
else
while [ $c -ge 1 ]
do
eval echo \$$c
c=`expr $c - 1 `
done
fi

OUTPUT:

"7a.sh" 11L, 129C written


[exam1@localhost ~]$ sh 7a.sh b I T
T
I
b
[exam1@localhost ~]$ sh 7a.sh B A N G A L O R E
E
R
O
L
A
G
N
A
B
[exam1@localhost ~]$ sh 7a.sh CAT RAT BAT
BAT
RAT
CAT

Dept of ISE Bangalore Institute Of Technology Page 20


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

7. 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.

7b.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
int main()

{
char str[10];
int pid;
pid=fork();
if(!pid)

{
printf("Child Process\n");
printf("Enter the process\n");
scanf("%s",str);
system(str);
printf("Ending child process\n");
}
else
{
wait(0);
}
}

Dept of ISE Bangalore Institute Of Technology Page 21


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[exam1@localhost ~]$ cc 7b.c


[exam1@localhost ~]$ ./a.out
Child Process
Enter the process
date
Mon Sep 28 15:17:20 IST 2015
Ending child process
[exam1@localhost ~]$ ./a.out
Child Process
Enter the process
ls
10.c 1a.l 2b.l 4a.y 5a.l 5b.y 7a.sh 8b.c abc.c clab.c lex.yy.c y.tab.h
11.c 1b.l 3.l 4b.l 5a.y 6.l 7b.c 9a.sh a.out file1.hole out.c y.tab.h.gch
12.c 2a.l 4a.l 4b.y 5b.l 6.y 8a.sh 9b.c a.txt in.c y.tab.c
Ending child process

Dept of ISE Bangalore Institute Of Technology Page 22


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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

c=$#
if [ $c -ne 2 ]
then
echo -e "Error! exactly two filenames should be specified"
else
file1=$1
file2=$2
set -- `ls -l $file1`
f1p=$1
set -- `ls -l $file2`
f2p=$1
if [ "$f1p" = "$f2p" ]
then
echo -e "\n Permission are identical:$f1p"
else
echo -e "\n Permission are different\n $file1=$f1p\n $file2=$f2p"
fi
fi

OUTPUT:

[exam1@localhost ~]$ sh 8a.sh 5a.l 5b.l

Permission are different


5a.l=-rwxrwxrwx
5b.l=-rw-rw-r—

[exam1@localhost ~]$ sh 8a.sh 5a.l 5a.y

Permission are identical:-rwxrwxrwx

Dept of ISE Bangalore Institute Of Technology Page 23


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>

char buf[]="ABCDEFGHIJKLMNOP";
char buf1[]="QRSTUVWXYZ012345";
int main()

{
int fd;
fd=creat("file1.hole",S_IRUSR|S_IWUSR|S_IWGRP|S_IROTH);
write(fd,buf,16);
lseek(fd,48,SEEK_SET);
write(fd,buf1,16);
exit(0);
}

OUTPUT:

[exam1@localhost ~]$ cc 8b.c


[exam1@localhost ~]$ ./a.out
[exam1@localhost ~]$ vi file1.hole

ABCDEFGHIJKLMNOP^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^
@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@QRSTUVWXYZ012
345

Dept of ISE Bangalore Institute Of Technology Page 24


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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).

9a.sh

#/bin/sh
if [ $# -eq 0 ]
then
echo "enter the valid input"
exit
fi
for i in $*
do
echo "cat>$i<<end">>bundle.sh
cat $i>>bundle.sh
echo "end">>bundle.sh
done

OUTPUT:

[exam2@localhost ~]$ sh 9a.sh file1 file2


[exam2@localhost ~]$ rm file1 file2
[exam2@localhost ~]$ ls
9a.sh a.out bundle.sh
[exam2@localhost ~]$ sh bundle.sh
[exam2@localhost ~]$ ls
9a.sh a.out bundle.sh file1 file2

Dept of ISE Bangalore Institute Of Technology Page 25


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

9. 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<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>

int main()
{
pid_t pid;
pid=fork();
if(pid==0)
{
printf("In Child.....\n");
printf("child id=%d\n",getpid());
printf("Parent id=%d\n",getppid());
}

else

{
pid=wait(0);
printf("In Parent.....\n");
printf("Child id=%d\n",pid);
printf("Parent id=%d\n",getpid());
}
}

OUTPUT:

[exam1@localhost ~]$ cc 9b.c


[exam1@localhost ~]$ ./a.out
In Child.....
child id=7493
Parent id=7492
In Parent.....
Child id=7493
Parent id=7492

Dept of ISE Bangalore Institute Of Technology Page 26


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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.

#include<stdio.h>
#include<stdlib.h>
struct proc
{
int id;
int arrival;
int burst;
int rem;
int wait;
int finish;
int turnaround;
float ratio;
}

process[10]; //structure to hold the process information


struct proc temp;
int no;
int chkprocess(int);
int nextprocess();
void roundrobin(int, int, int[], int[]);
void srtf(int);

main()
{
int n,tq,choice;
int bt[10],st[10],i;
for(; ;)
{
printf("Enter the choice \n");
printf(" 1. Round Robin\n 2. SRT\n 3. Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Round Robin scheduling algorithm\n");
printf("Enter number of processes:\n");
scanf("%d",&n);
printf("Enter burst time for sequences:");

Dept of ISE Bangalore Institute Of Technology Page 27


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i]; //service time
}
printf("Enter time quantum:");
scanf("%d",&tq);
roundrobin(n,tq,st,bt);
break;
case 2:
printf("\n \n ---SHORTEST REMAINING TIME NEXT---\n \n ");
printf("\n \n Enter the number of processes: ");
scanf("%d", &n);
srtf(n);
break;
case 3: exit(0);
} // end of switch
} // end of for
} //end of main()

void roundrobin(int n,int tq,int st[],int bt[])


{
int time=0;
int tat[10],wt[10],i,count=0,swt=0,stat=0,temp1,sq=0;
float awt=0.0,atat=0.0;
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp1=tq;
if(st[i]==0) //when service time of a process equals zero then count
value is incremented
{
count++;
continue;
}
if(st[i]>tq) // when service time of a process greater than time
quantum then time
st[i]=st[i]-tq; //quantum value subtracted from service time
else
if(st[i]>=0)
{
temp1=st[i]; // temp1 stores the service time of a process
st[i]=0; // making service time equals 0
}
sq=sq+temp1; // utilizing temp1 value to calculate turnaround time

Dept of ISE Bangalore Institute Of Technology Page 28


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

tat[i]=sq; // turn around time


} //end of for
if(n==count) // it indicates all processes have completed their task
because the count value
break; // incremented when service time equals 0
} //end of while
for(i=0;i<n;i++) // to calculate the wait time and turnaround time of
each process
{
wt[i]=tat[i]-bt[i]; // waiting time calculated from the turnaround
time - burst time
swt=swt+wt[i]; // summation of wait time
stat=stat+tat[i]; // summation of turnaround time
}
awt=(float)swt/n; // average wait time
atat=(float)stat/n; // average turnaround time
printf("Process_no Burst time Wait time Turn around time\n");
for(i=0;i<n;i++)
printf("%d\t\t%d\t\t%d\t\t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Avg wait time is %f\n Avg turn around time is %f\n",awt,atat);
}// end of Round Robin

int chkprocess(int s) // function to check process remaining time is zero or


not
{
int i;
for(i = 1; i <= s; i++)
{
if(process[i].rem != 0)
return 1;
}
return 0;
} // end of chkprocess

int nextprocess() // function to identify the next process to be executed


{
int min, l, i;
min = 32000; //any limit assumed
for(i = 1; i <= no; i++)
{
if( process[i].rem!=0 && process[i].rem < min)
{
min = process[i].rem;
l = i;
}
}

Dept of ISE Bangalore Institute Of Technology Page 29


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

return l;
} // end of nextprocess

void srtf(int n)
{
int i,j,k,time=0;
float tavg=0,wavg=0;
for(i = 1; i <= n; i++)
{
process[i].id = i;
printf("\n\nEnter the arrival time for process %d: ", i);
scanf("%d", &(process[i].arrival));
printf("Enter the burst time for process %d: ", i);
scanf("%d", &(process[i].burst));
process[i].rem = process[i].burst;
}
for(i = 1; i <= n; i++)
{
for(j = i + 1; j <= n; j++)
{
if(process[i].arrival > process[j].arrival) // sort arrival time of a process
{
temp = process[i];
process[i] = process[j];
process[j] = temp;
}
}
}

no = 0;
j = 1;
while(chkprocess(n) == 1)
{
if(process[no + 1].arrival == time)
{
no++;
if(process[j].rem==0)
process[j].finish=time;
j = nextprocess();
}
if(process[j].rem != 0) // to calculate the waiting time of a process
{
process[j].rem--;
for(i = 1; i <= no; i++)
{
if(i != j && process[i].rem != 0)

Dept of ISE Bangalore Institute Of Technology Page 30


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

process[i].wait++;
}
}
else
{
process[j].finish = time;
j=nextprocess();
time--;
k=j;
}

time++;
}
process[k].finish = time;
printf("\n\n\t\t\t---SHORTEST REMAINING TIME FIRST---");
printf("\n\n Process Arrival Burst Waiting Finishing turnaround Tr/Tb \n");
printf("%5s %9s %7s %10s %8s %9s\n\n", "id", "time", "time", "time",
"time", "time");
for(i = 1; i <= n; i++)
{
process[i].turnaround = process[i].wait + process[i].burst; // calc of turnaround
process[i].ratio = (float)process[i].turnaround / (float)process[i].burst;
printf("%5d %8d %7d %8d %10d %9d %10.1f ", process[i].id,
process[i].arrival, process[i].burst, process[i].wait, process[i].finish,
process[i].turnaround, process[i].ratio);
tavg=tavg+ process[i].turnaround; //summation of turnaround time
wavg=wavg+process[i].wait; // summation of waiting time
printf("\n\n");
}
tavg=tavg/n; // average turnaround time
wavg=wavg/n; // average wait time
printf("tavg=%f\t wavg=%f\n",tavg,wavg);
} // end of srtf

Dept of ISE Bangalore Institute Of Technology Page 31


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

OUTPUT:

[exam1@localhost ~]$ cc 10.c


[exam1@localhost ~]$ ./a.out

Enter the choice


1. Round Robin
2. SRT
3. Exit
1
Round Robin scheduling algorithm
Enter number of processes:
3
Enter burst time for sequences:24
3
3
Enter time quantum:4
Process_no Burst time Wait time Turn around time
1 24 6 30
2 3 4 7
3 3 7 10
Avg wait time is 5.666667
Avg turn around time is 15.666667

Enter the choice


1. Round Robin
2. SRT
3. Exit
2

---SHORTEST REMAINING TIME NEXT---

Enter the number of processes: 4

Enter the arrival time for process 1: 0


Enter the burst time for process 1: 8

Enter the arrival time for process 2: 1


Enter the burst time for process 2: 4

Dept of ISE Bangalore Institute Of Technology Page 32


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

Enter the arrival time for process 3: 2


Enter the burst time for process 3: 9

Enter the arrival time for process 4: 3


Enter the burst time for process 4: 5

---SHORTEST REMAINING TIME FIRST---

Process Arrival Burst Waiting Finishing turnaround Tr/Tb


id time time time time time

1 0 8 9 17 17 2.1

2 1 4 0 5 4 1.0

3 2 9 15 26 24 2.7

4 3 5 2 10 7 1.4

tavg=13.000000 wavg=6.500000
Enter the choice
1. Round Robin
2. SRT
3. Exit

Dept of ISE Bangalore Institute Of Technology Page 33


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

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.

11.c

#include<stdio.h>
#include<omp.h>
#include<stdlib.h>

int main(int argc,char* argv[])


{
long int Fibnumber[100]={0};

int j,id,i=0;
int n,tid,threads;
printf("Enter the no. range\n");
scanf("%d",&n);

omp_set_num_threads(2);

#pragma omp parallel


{
printf("The no. of threads are %d\n",omp_get_num_threads());
#pragma omp critical
{
if (omp_get_thread_num()==0)
{
printf("Thread id %d for fib series generation\n",
omp_get_thread_num());
Fibnumber[1]=1;
Fibnumber[2]=1;
for(i=3;i<=n;i++)
Fibnumber[i]=Fibnumber[i-1]+Fibnumber[i-2];
}
}

#pragma omp critical


{
if(omp_get_thread_num()==1)
{
printf("Thread id %d for fib series printing\n",
omp_get_thread_num());
printf("The no. value is %d\n",n);
for(i=1;i<=n;i++)

Dept of ISE Bangalore Institute Of Technology Page 34


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

printf("%d\t",Fibnumber[i]);
printf("\n\n");
}
}
}

}
OUTPUT:

[exam2@localhost ~]$ cc -fopenmp 11.c


[exam2@localhost ~]$ ./a.out
Enter the no. range
4
The no. of threads are 2
Thread id 0 for fib series generation
The no. of threads are 2
Thread id 1 for fib series printing
The no. value is 4
1 1 2 3

[exam2@localhost ~]$ ./a.out


Enter the no. range
5
The no. of threads are 2
Thread id 0 for fib series generation
The no. of threads are 2
Thread id 1 for fib series printing
The no. value is 5
1 1 2 3 5

Dept of ISE Bangalore Institute Of Technology Page 35


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

12. Design, develop and run a program to implement the Banker’s Algorithm.
Demonstrate its working with different data values.

#include<stdio.h>
struct process
{
int all[6],max[6],need[6],finished,request[6];
}p[10];
int avail[6],sseq[10],ss=0,check1=0,check2=0,n,pid,work[6];
int nor,nori;
void main()
{
int safeseq(void);
int ch,i=0,j=0,k,pid,ch1;
int violationcheck=0,waitcheck=0;
do
{
printf("\n\n\t 1. Input");
printf("\n\n\t 2. New Request");
printf("\n\n\t 3. Safe State or Not");
printf("\n\n\t 4. print");
printf("\n\n\t 5. Exit");
printf("\n\n\t Enter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n\t Enter number of processes : ");
scanf("%d",&n);
printf("\n\n\t Enter the Number of Resources : ");
scanf("%d",&nor);
printf("\n\n\t Enter the Available Resouces : ");
for(j=0;j<n;j++)
{
for(k=0;k<nor;k++)
{
if(j==0)
{
printf("\n\n\t For Resource type %d : ",k);
scanf("%d",&avail[k]);
}
p[j].max[k]=0;
p[j].all[k]=0;
p[j].need[k]=0;
p[j].finished=0;

Dept of ISE Bangalore Institute Of Technology Page 36


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

p[j].request[k]=0;
}
}
for(i=0;i<n;i++)
{
printf("\n\n\t Enter Max and Allocated resources for P : ",i);
for(j=0;j<nor;j++)
{
printf("\n\n\t Enter the Max of resource %d : ",j);
scanf("%d",&p[i].max[j]);
printf("\n\n\t Allocation of resource %d :",j);
scanf("%d",&p[i].all[j]);
if(p[i].all[j]>p[i].max[j])
{
printf("\n\n\t Allocation should be less < or == max");
j--;
}
else
p[i].need[j]=p[i].max[j]-p[i].all[j];
avail[j]=avail[j]-p[i].all[j];
}
}
break;
case 2:
violationcheck=0;
waitcheck=0;
printf("\n\n\t Requesting process id :");
scanf("%d",&pid);
for(j=0;j<nor;j++)
{
printf("\n\n\t Number of Request for resource %d :",j);
scanf("%d",&p[pid].request[j]);
if(p[pid].request[j]>p[pid].need[j])
violationcheck=1;
if(p[pid].request[j]>avail[j])
waitcheck=1;
}
if (violationcheck==1)
printf("\n\n\t The Process Exceeds it's Max Need: Terminated");
else if(waitcheck==1)
printf("\n\n\t Lack of Resourcess : Process State - Wait");
else
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]-p[pid].request[j];

Dept of ISE Bangalore Institute Of Technology Page 37


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

p[pid].all[j]=p[pid].all[j]+p[pid].request[j];
p[pid].need[j]=p[pid].need[j]-p[pid].request[j];
}
ch1=safeseq();
if(ch1==0)
{
for(j=0;j<nor;j++)
{
avail[j]=avail[j]+p[pid].request[j];
p[pid].all[j]=p[pid].all[j]-p[pid].request[j];
p[pid].need[j]=p[pid].need[j]+p[pid].request[j];
}
}
else if(ch1==1)
printf("\n\n\t Request Committed ");
}
break;
case 3:
if(safeseq()==1)
printf("\n\n\t The System is in safe state ");
else
printf("\n\n\t The System is Not in safe state ");
break;
case 4:
printf("\n\n\t Number of processes : %d",n);
printf("\n\n\t Number of Resources : %d",nor);
printf("\n\n\t Pid \t Max \t Allocated \t Need ");
for(i=0;i<n;i++)
{
printf("\n\n\t P%d : ",i);
for(j=0;j<nor;j++)
printf(" %d",p[i].max[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].all[j]);
printf("\t");
for(j=0;j<nor;j++)
printf("%d",p[i].need[j]);
}
printf("\n\n\t Available :");
for(i=0;i<nor;i++)
printf("%d",avail[i]);
break;
case 5:
break;
}

Dept of ISE Bangalore Institute Of Technology Page 38


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

}while(ch!=5);
}
int safeseq()
{
int tj,tk,i,j,k;
ss=0;
for(j=0;j<nor;j++)
work[j]=avail[j];
for(j=0;j<n;j++)
p[j].finished=0;
for( tk=0;tk<nor;tk++)
{
for(j=0;j<n;j++)
{
if(p[j].finished==0)
{
check1=0;
for(k=0;k<nor;k++)
if(p[j].need[k]<=work[k])
check1++;
if(check1==nor)
{
for(k=0;k<nor;k++)
{
work[k]=work[k]+p[j].all[k];
p[j].finished=1;
}
sseq[ss]=j;
ss++;
}
}
}
}
check2=0;
for(i=0;i<n;i++)
if(p[i].finished==1)
check2++;
printf("\n\n\t");
if(check2>=n)
{
printf("\n\n\t The system is in safe state");
for( tj=0;tj<n;tj++)
printf("P%d,",sseq[tj]);
return 1;
}
else

Dept of ISE Bangalore Institute Of Technology Page 39


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

printf("\n\n\t The system is Not in safe state");


return 0;
}

[exam2@localhost ~]$ cc 12.c


[exam2@localhost ~]$ ./a.out

1. Input

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice : 1

Enter number of processes : 3

Enter the Number of Resources : 3

Enter the Available Resouces :

For Resource type 0 : 10

For Resource type 1 : 10

For Resource type 2 : 10

Enter Max and Allocated resources for P :

Enter the Max of resource 0 : 3

Allocation of resource 0 :2

Enter the Max of resource 1 : 3

Allocation of resource 1 :2

Enter the Max of resource 2 : 3

Allocation of resource 2 :2

Dept of ISE Bangalore Institute Of Technology Page 40


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

Enter Max and Allocated resources for P :

Enter the Max of resource 0 : 2

Allocation of resource 0 :1

Enter the Max of resource 1 : 2

Allocation of resource 1 :1

Enter the Max of resource 2 : 2

Allocation of resource 2 :1

Enter Max and Allocated resources for P :

Enter the Max of resource 0 : 2

Allocation of resource 0 :1

Enter the Max of resource 1 : 2

Allocation of resource 1 :1

Enter the Max of resource 2 : 2

Allocation of resource 2 :1

1. Input

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice :3

The system is in safe stateP0,P1,P2,

The System is in safe state

1. Input

Dept of ISE Bangalore Institute Of Technology Page 41


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice :4

Number of processes : 3

Number of Resources : 3

Pid Max Allocated Need

P0 : 3 3 3 222 111

P1 : 2 2 2 111 111

P2 : 2 2 2 111 111

Available :666

1. Input

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice :2

Requesting process id :2

Number of Request for resource 0 :1

Number of Request for resource 1 :0

Number of Request for resource 2 :0

The system is in safe stateP0,P1,P2,

Dept of ISE Bangalore Institute Of Technology Page 42


SYSTEM SOFTWARE & OPERATING SYSTEMS LABORATORY (10CSL58)

Request Committed

1. Input

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice :4

Number of processes : 3

Number of Resources : 3

Pid Max Allocated Need

P0 : 3 3 3 222 111

P1 : 2 2 2 111 111

P2 : 2 2 2 211 011

Available :566

1. Input

2. New Request

3. Safe State or Not

4. print

5. Exit

Enter ur choice :

Dept of ISE Bangalore Institute Of Technology Page 43

You might also like