SS and OS Lab Manual 2013.pdf
-
Upload
aravinda-gowda -
Category
Documents
-
view
358 -
download
1
description
Transcript of SS and OS Lab Manual 2013.pdf
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
1 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
PART - ALEX 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 onthe standard output).
b) C program that creates a child process to read commands from the standard input and execute them (aminimal implementation of a shell – like program). You can assume that no arguments will be passed tothe commands to be executed.
8. a) Shell script that accepts two file names as arguments, checks if the permissions for these files areidentical and if the permissions are identical, outputs the common permissions, otherwise outputs each filename followed by its permissions.
b) C program to create a file with 16 bytes of arbitrary data from the beginning and another 16 bytes ofarbitrary 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 fileas well as the code to recreate these files. Thus if the script generated by your script is executed, it wouldrecreate the original files(This is same as the “bundle” script described by Brain W. Kernighan and RobPike in “ The Unix Programming Environment”, Prentice – Hall India).
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
2 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
b) C program to do the following: Using fork( ) create a child process. The child process prints its ownprocess-id and id of its parent and then exits. The parent process waits for its child to finish (by executingthe 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.
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
3 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
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.outHai dear you thereNumber of character =15
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
4 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
Number of words =4Number of spaces =3Number of lines =1
1) b.Program to count the numbers of comment lines in a given C program. Also eliminate them andcopy the resulting program into separate file. //1b.l
%{int c=0;
%}%option noyywrap%x CMT%x ct%%
“//” { BEGIN ct;}<ct>.\n {c++;BEGIN INITIAL ;}<ct>. ;
"/*" {BEGIN CMT;}<CMT>\n {c++;}<CMT>.\n {c++;}<CMT>"*/" {c++; BEGIN INITIAL;}<CMT>. ;
%%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.cNumber of comment lines =2
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
5 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
1.c main(){
int a,b; /* int declaration */float amt,avg; /* float declaration */
}
1b.c main(){
int a,b;float amt,avg;
}
2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operatorspresent. Print them separately. //2a.l
%{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;
}
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
6 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
\) {if(pc<=0) return 0;if(state==OPRND||state==CP)
{state=CP;pc--;
}else return 0;
}
\n {return 1;
}
%%
main(){
int c,k;c=yylex();if(c==1&&pc==0&&i==j+1){
if(state==CP||state==OPRND){
printf("Valid Expression\n");printf("Valid Operators are:\n");for(k=0;k<j;k++)printf("%c\n",a[k]);
}}
elseprintf("Invalid Expression\n");
}OUTPUT:$ lex 2a.l$ cc lex.yy.c –ll$./a.outa+b-coperand 1=aoperand 2=b
$ lex 2a.l$ cc lex.yy.c –ll$./a.outa + b - - coperand 1=aoperand 2=b
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
7 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
operand 3=cValid ExpressionValid Operators are:+-
Invalid Expression
2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operatorspresent. Print them separately. //2aa.l
OR%{
int a=0,s=0,m=0,d=0,ob=0,cb=0,va=0 ,ident=0;int flaga=0, flags=0, flagm=0, flagd=0;
%}ID [a-zA-Z][a-zA-Z 0-9]*NOTID [0-9a-zA-Z]+%%{ID} {ident++;printf("\n %s is an identifier\n",yytext);}{NOTID} {va++;}[+] {a++;flaga=1;}[-] {s++;flags=1;}[*] {m++;flagm=1;}[/] {d++;flagd=1;}[(] {ob++;}[)] {cb++;}return;%%int main(){
printf("Enter the expression:\n");yylex();if(ob-cb!=0 | va>0 |ident==0|a+s+m+d>=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)
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
8 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
printf("\\ \n");}
return 0;}
OUTPUT:Enter the expression:A+BA is an identifierB is an identifierCtrl+dvalid expressionAdd=1Sub=0Mul=0Div=0Operators 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" {flag=1;}. ;\n {return;}
%%main(){
yylex();
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
9 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
if(flag==0)printf("Sentence is Simple\n ");elseprintf("Sentence is Compound\n");
}OUTPUT:$ lex 2b.l$ cc lex.yy.c –ll$./a.outHai dear you thereSentence is Simple
$ lex 2b.l$ cc lex.yy.c –ll$./a.outHai dear I am not cumgSentence 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 IDENTID [a-zA-Z][a-zA-Z0-9]*DTYPE "int "|"float "|"char "%%
{DTYPE} { BEGIN IDENT; }<IDENT>[^ ,]{ID} ;<IDENT>{ID}"," { idc++; }<IDENT>{ID}";" { 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 3.c
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
10 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
$ cc lex.yy.c –ll$./a.out 3.cNumber of identifiers is = 5
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<stdio.h>#include<ctype.h>
%}%token DIG LET
%left '+''-'
%left '*''/'
%%line: line exp '\n' { printf("valid Expression\n"); }
|| line '\n'| error '\n' { yyerror("invalid Expression\n");yyerrok; };
exp: exp '+' exp| exp '-' exp|exp '*' exp|exp '/' exp|'(' exp ')'| num| lts;
num: DIG| num DIG;
lts: LET| lts LET| lts DIG;
%%main(){
yyparse();}yyerror(char*s)
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
11 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
{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)*cvalid Expression
a+(b+csyntax errorinvalid Expression
a++bsyntax errorinvalid Expression
a+b*cvalid Expression
a+b*c a*b+csyntax errorinvalid Expression
4.b) Program to recognize a valid variable, which starts with a letter followed by any number of lettersor digits. //4b.y
%{#include<stdio.h>#include<ctype.h>
%}
%token DIG LET
%%
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
12 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
line: line var '\n' { printf("\n valid Variable\n"); }|| line '\n'| error '\n' { yyerror("\ninvalid variable\n");yyerrok };
var: LET| var LET| var DIG;
%%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.outabc23valid Variable
a123valid Variable
133asdsyntax errorinvalid variable
5.a) Program to evaluate an arithmetic expression involving operators +,-,*,/. // 5a.y
%{#include<stdio.h>#include<ctype.h>
%}
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
13 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
%token DIG%left '+' '-'%left '*' '/'%left UMINUS%%line : line exp '\n' { printf("Res=%d\n",$2); }
|| line '\n'| error '\n' { yyerror("Invalid Exp:\n");yyerrok; };
exp : exp '+' exp { $$=$1+$3; }| exp '-' exp { $$=$1-$3; }|exp '*' exp { $$=$1*$3; }|exp '/' exp {
if($3==0){
printf("\n Divide by 0 error\n");return 0;
}else
$$=$1/$3;}
| '-'exp %prec UMINUS { $$=-$2; }| num;
num : DIG { $$=$1; }| num DIG { $$=$1*10+$2;};
%%main(){
yyparse();}yyerror(char *s){
printf("%s\n",s);}yylex(){
int c;while((c=getchar())==' ');if(isdigit(c)){
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
14 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
yylval=c-'0';return DIG;
}return c;
}OUTPUT:
$ yacc 5a.y$ cc y.tab.c$ ./a.out3+4+5Res=127-4+Invalid Exp:4/0Divide 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<stdio.h>#include<ctype.h>
%}%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;
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
15 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
if(c=='b') return B;return c;
}
OUTPUT:$ yacc 5b.y$ cc y.tab.c$ ./a.out
abvalid expressionaaabsyntax errorinvalid expression
abbbsyntax errorinvalid expression
asyntax errorinvalid expression
6. Program to recognize the grammer {anb,n>=10} // 6.y%{
#include<stdio.h>#include<ctype.h>
%}%token A B%%line : line str '\n' { printf("\n valid expression\n"); }
|| line '\n'| error '\n' { yyerror("\ninvalid expression\n"); yyerrok; };
str : A A A A A A A A A A B| A str;
%%main(){
yyparse();}yyerror(char *s)
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
16 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
{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
aaaaaaaaaabvalid expression
absyntax errorinvalid expression
aaaaaaaaaaaaaaabvalid expression
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
17 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
PART BUNIX 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 thestandard output). //7a.sh
if [ $# -eq 0 ]then echo "No arguments"elsefor i in "$@"dorev=$i' '$revdoneecho "The Reverse string is $rev"fi
OUTPUT:
$sh 7a.sh A B C
The Reverse string is C B AOR
$ chmod 777 7a.sh
The Reverse string is C B A
7.b) C program that creates a child process to read commands from the standard input and executethem(minimal implementation of shell like program). You can assume that no arguments will bepassed to the commands to be executed. //7b.c
#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>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);
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
18 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
}waitpid(pid,0,0);return 0;
}
OUTPUT:$ cc 7b.c$ ./a.outEnter a UNIX commanddateTue 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 areidentical and if the permissions are identical, outputs the common permissions, otherwise outputseach file name followed by its permissions. //8a.sh
if [ $# -ne 2 ]thenecho "no arguments pls enter 2 arguments"elif [ ! -e $1 -o ! -e $2 ]thenecho "File does not exist"elseper1=`ls -l $1 | cut -c 2-10`per2=`ls -l $2 | cut -c 2-10`if [ $per1 = $per2 ]thenecho "Permissions are Identical:permission is $per1"elseecho "Permissions are not Identical"echo "permission of $1 is $per1"echo "permission of $2 is $per2"fifiOUTPUT:$ sh 8a.shno arguments pls enter 2 arguments
1.cThis is System Software Lab
$ sh 8a.sh 1.c 2.cPermissions 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 exist2.cHKBK college of Engineering
$ chmod 777 1.c
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
19 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
$ chmod 770 2.c$ sh 2a.sh 1.c 2.cPermissions are not Identicalpermission of 1.c is rwxrwxrwxpermission 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 ishandled. // 8b.c
#include<stdio.h>#include<sys/types.h>#include<unistd.h>#include<fcntl.h>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 hole0000000 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 10000020 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 20000100
9.a) Shell script that accepts filenames specified as arguments and creates a shell script that contains this fileas well as the code to recreate these files . thus if the script generated by your script is executed, itwould recreate the original files . //9a.sh
if [ $# -eq 0 ]then
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
20 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
echo "No Arguments"exitfifor i in "$@"do
if [ ! -e $i ]thenecho "$i not existing"elseecho "cat >$i <<'end of $i'"cat $iecho "end of $i"echo "echo $i is Recreated "fidone
OUTPUT:
$ sh 9a.sh 2a.l > t
$ mkdir k
$ cd k
$ ../t
2a.l is Recreated
9.b) C program to do the following: using fork() create a child process. The child process prints its ownprocess id and id of its parent and then exits. The parent process waits for its child to finish(byexecuting the wait()) and prints its own process id and the id of its child process and then exits. //9b.c
#include<stdio.h>#include<sys/types.h>#include<unistd.h>int main(){
pid_t pid;pid=fork();if(pid==0){
printf("Output from child process:\n");printf("The id of child process=%d\n",getpid());printf("The id of parent process=%d\n",getppid());exit(0);
}
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
21 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
pid=waitpid(pid,0,0);printf("Output from parent process:\n");printf("The id of child process=%d\n",pid());printf("The id of parent process=%d\n",getpid());exit(1);
}OUTPUT:$ cc 9b.c$ ./a.out
Output from child process:
The id of child process=2898
The id of parent process=2897
Output from parent process:
The id of child process=2898
The id of parent process=2897
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
22 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
Operating Systems Programs: Using OpenMP
10: Design, develop and execute a program in C/C++ to simulate the working of Shortest Remainingtime and Round Robin Scheduling algorithms. Experiment with different Quantum sizes for theRound Robin algorithm. In all cases, determine the average turn Around time. Input can be readfrom keyboard or from a file.
#include<stdio.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 informationstruct 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,j,k;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");
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
23 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
scanf("%d",&n);printf("Enter burst time for sequences:");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,j,k;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 timest[i]=st[i]-tq; //quantum value subtracted from service time
elseif(st[i]>=0){
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
24 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
temp1=st[i]; // temp1 stores the service time of a processst[i]=0; // making service time equals 0
}sq=sq+temp1; // utilizing temp1 value to calculate turnaround timetat[i]=sq; // turn around time
} //end of forif(n==count) // it indicates all processes have completed their task because the count valuebreak; // 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 timeswt=swt+wt[i]; // summation of wait timestat=stat+tat[i]; // summation of turnaround time
}awt=(float)swt/n; // average wait timeatat=(float)stat/n; // average turnaround timeprintf("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 assumedfor(i = 1; i <= no; i++){
if( process[i].rem!=0 && process[i].rem < min){
min = process[i].rem;l = i;
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
25 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
}}return l;
} // end of nextprocess
void srtf(int n){int i,j,k,time=0;float tavg,wavg;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--;
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
26 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
for(i = 1; i <= no; i++){
if(i != j && process[i].rem != 0)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 turnaroundprocess[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 timewavg=wavg+process[i].wait; // summation of waiting timeprintf("\n\n");
}
tavg=tavg/n; // average turnaround timewavg=wavg/n; // average wait timeprintf("tavg=%f\t wavg=%f\n",tavg,wavg);
}// end of srtf
OUTPUT:Enter the choice1) Round Robin 2) SRT3) Exit1Round Robin scheduling algorithm
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
27 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
**********************************Enter number of processes:3Enter burst time for sequences:2433
Enter time quantum:4Process_no Burst time Wait time Turnaround time
1 24 6 302 3 4 73 3 7 10
Avg wait time is 5.666667Avg turnaround time is 15.666667Enter the choice1) Round Robin 2) SRT3) Exit2---SHORTEST REMAINING TIME NEXT---Enter the number of processes: 4Enter the arrival time for process 1: 0Enter the burst time for process 1: 8Enter the arrival time for process 2: 1Enter the burst time for process 2: 4Enter the arrival time for process 3: 2Enter the burst time for process 3: 9Enter the arrival time for process 4: 3Enter the burst time for process 4: 51 24 6 302 3 4 73 3 7 10---SHORTEST REMAINING TIME FIRST---Enter the number of processes: 4Enter the arrival time for process 1: 0Enter the burst time for process 1: 8Enter the arrival time for process 2: 1Enter the burst time for process 2: 4Enter the arrival time for process 3: 2Enter the burst time for process 3: 9Enter the arrival time for process 4: 3Enter the burst time for process 4: 5
---SHORTEST REMAINING TIME NEXT---
Process Arrival Burst Waiting Finishing turnaround Tr/Tbid time time time time time time1 0 8 9 17 17 2.12 1 4 0 5 4 1.0
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
28 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
3 2 9 15 26 24 2.74 3 5 2 10 7 1.4
tavg=13.000000wavg=6.50000011: Design develop and run a multi-threaded program to generate and print Fibonacci series. Onethread has to generate the numbers up to the specified limit and Another thread has to print them.Ensure proper synchronization.# include<stdio.h># include<omp.h># include<stdlib.h>
int MAX;int Fibonacci(int n){
int x, y;if (n < 2)return n;else{x = Fibonacci(n - 1);y = Fibonacci(n - 2);return (x + y);}
}
int FibonacciTask(int n){
int x, y;if (n < 2)
return n;else{
x = Fibonacci(n - 1);y = Fibonacci(n - 2);return (x + y);
}}/* random number generation upto 24 */int random_num(){
int temp;temp = rand();temp = temp%24;MAX = temp;return(MAX);
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
29 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
}
int main(int argc, char * argv[]){
int FibNumber[25] = {0};int j, temp,tmp,id,i = 0;int n, tid, nthreads;
printf("Please Enter the number Range :");scanf("%d",&n);printf("\n");omp_set_num_threads(2);
//Parallel region# pragma omp parallel{
printf("The number of threads are %d\n",omp_get_num_threads());# pragma omp for private (tid, tmp, FibNumber)for(j = 1; j<=n; j++){
tmp = random_num();/* Get thread number *//* tid = omp_get_thread_num();
printf("The number of threads are %d\n",omp_get_num_threads());printf("The thread id is = %d\n", tid); */
/* The critical section here will enable, not more then onethread to execute in this section (synchronization) */
# pragma omp critical{
/* Get thread number *//* tid = omp_get_thread_num();printf("********************* inside critical section
******************\n");printf("The thread id is = %d\n", tid); */for(i = 1; i <= tmp; i++)
FibNumber[i] = FibonacciTask(i);printf("The number value is %d:",tmp);for(i = 1; i <= tmp; i++)
printf("%d \t", FibNumber[i]);printf("\n\n");
}}
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
30 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
}}
OUTPUT:lease Enter the number Range : 7The number value is 7:1 1 2 3 5 8 13
The number value is 22:1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597 2584 4181 6765 10946 17711
The number value is 9:1 1 2 3 5 8 13 21 34
The number value is 19:1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597 2584 4181
The number value is 17:1 1 2 3 5 8 13 21 34 55 89144 233 377 610 987 1597
The number value is 7:1 1 2 3 5 8 13
The number value is 10:1 1 2 3 5 8 13 21 34 55
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;int 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");
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
31 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
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;p[j].request[k]=0;
}}for(i=0;i<n;i++){
printf("\n\n\t Enter Max and Allocated resources for P %d : ",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--;
}elsep[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);
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
32 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
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];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");
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
33 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
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;
}//getch();
}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++)
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
34 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
printf("P%d,",sseq[tj]);return 1;
}elseprintf("\n\n\t The system is Not in safe state");return 0;
}
OUTPUT:
1. Input2. New Request3. Safe State or Not4. print5. ExitEnter ur choice :1
Enter number of processes : 5Enter the Number of Resources : 3Enter the Available Resouces :For Resource type 0 : 10For Resource type 1 : 5For Resource type 2 : 7
Enter Max and Allocated resources for P 0 :
Enter the Max of resource 0 : 7
Allocation of resource 0 :0
Enter the Max of resource 1 : 5
Allocation of resource 1 :1
Enter the Max of resource 2 : 3
Allocation of resource 2 :0
Enter Max and Allocated resources for P 1 :
Enter the Max of resource 0 : 3
Allocation of resource 0 :2
Enter the Max of resource 1 : 2
Allocation of resource 1 :0
Enter the Max of resource 2 : 2
Allocation of resource 2 :0
Enter Max and Allocated resources for P 2 :
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
35 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
Enter the Max of resource 0 : 9
Allocation of resource 0 :3
Enter the Max of resource 1 : 0
Allocation of resource 1 :0
Enter the Max of resource 2 : 2
Allocation of resource 2 :2
Enter Max and Allocated resources for P 3 :
Enter the Max of resource 0 : 2
Allocation of resource 0 :2
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 4 :
Enter the Max of resource 0 : 4
Allocation of resource 0 :0
Enter the Max of resource 1 : 3
Allocation of resource 1 :0
Enter the Max of resource 2 : 3
Allocation of resource 2 :2
1. Input
2. New Request
3. Safe State or Not
4. print
5. Exit
Enter ur choice :3
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
36 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
The system is in safe stateP1,P3,P4,P0,P2,
The System is in safe state
1. Input
2. New Request
3. Safe State or Not
4. print
5. Exit
Enter ur choice :2
Requesting process id :1
Number of Request for resource 0 :1
Number of Request for resource 1 :0
Number of Request for resource 2 :2
The system is in safe stateP1,P3,P4,P0,P2,
Request Committed1. Input2. New Request3. Safe State or Not4. print5. Exit
Enter ur choice :2
Requesting process id :2
Number of Request for resource 0 :2
Number of Request for resource 1 :2
Number of Request for resource 2 :3
The Process Exceeds it�s Max Need: Terminated
-------------------------------------------------------- END-----------------------------------------------------
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
37 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013