SS and OS Lab Manual 2013 (VTU)
SS and OS Lab Manual 2013 (VTU)
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.
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).
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.
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++;}
%%
OUTPUT:
$ lex 1a.l
$ cc lex.yy.c –ll
$ ./a.out
Hai dear you there
Number of character =15
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
%%
%%
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
2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operators
present. 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;
}
\) {
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]);
}
}
else
printf("Invalid Expression\n");
}
OUTPUT:
$ lex 2a.l $ lex 2a.l
$ cc lex.yy.c –ll $ cc lex.yy.c –ll
$./a.out $./a.out
a+b-c a+b--c
operand 1=a operand 1=a
operand 2=b operand 2=b
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)
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:
+
%{
int flag=0;
%}
%option noyywrap
%%
"and" |
"AND" |
"or" |
"OR" |
"if" |
"IF" |
"else" |
"ELSE" |
"not" |
"NOT" |
"but" |
"BUT" {flag=1;}
. ;
\n {return;}
%%
main()
{
yylex();
if(flag==0)
printf("Sentence is Simple\n ");
else
printf("Sentence is Compound\n");
}
OUTPUT:
$ lex 2b.l $ lex 2b.l
$ cc lex.yy.c –ll $ cc lex.yy.c –ll
$./a.out $./a.out
Hai dear you there Hai dear I am not cumg
Sentence is Simple 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 "
%%
%%
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);
}
OUTPUT:
$ lex 3.l 3.c
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)
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<stdio.h>
#include<ctype.h>
%}
%%
a123
valid Variable
133asd
syntax error
invalid variable
%{
#include<stdio.h>
#include<ctype.h>
%}
%token DIG
%left '+' '-'
%left '*' '/'
%left UMINUS
%%
line : line exp '\n' { printf("Res=%d\n",$2); }
|
| line '\n'
| error '\n' { yyerror("Invalid Exp:\n");yyerrok; }
;
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<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;
if(c=='b') return B;
return c;
}
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
str :AAAAAAAAAAB
| A str
;
%%
main()
{
yyparse();
}
yyerror(char *s)
{
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
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:
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<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);
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 1.c
no arguments pls enter 2 arguments This is System Software Lab
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
if [ ! -e $i ]
then
echo "$i not existing"
else
echo "cat >$i <<'end of $i'"
cat $i
echo "end of $i"
echo "echo $i is Recreated "
fi
done
OUTPUT:
$ 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 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. //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);
}
20 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
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
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. Input can be read
from 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 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,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");
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()
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
}
}
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();
}
process[j].finish = time;
j=nextprocess();
time--;
k=j;
}
time++;
}
process[k].finish = time;
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");
}
OUTPUT:
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
3 2 9 15 26 24 2.7
4 3 5 2 10 7 1.4
tavg=13.000000
wavg=6.500000
11: 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.
# 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);
//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 one
thread 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");
}
}
}
}
OUTPUT:
lease Enter the number Range : 7
The number value is 7:1 1 2 3 5 8 13
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");
30 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
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--;
}
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);
31 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
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");
32 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
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++)
33 Prepared By: B. Fakruddin , Dept of CSE, HKBKCE 2013
10CSL58 SYSTEM SOFTWARE & OPERATING SYSTEMS LAB MANUAL V SEM CSE
printf("P%d,",sseq[tj]);
return 1;
}
else
printf("\n\n\t The system is Not in safe state");
return 0;
}
OUTPUT:
1. Input
2. New Request
3. Safe State or Not
4. print
5. Exit
Enter ur choice :1
Allocation of resource 0 :0
Allocation of resource 1 :1
Allocation of resource 2 :0
Allocation of resource 0 :2
Allocation of resource 1 :0
Allocation of resource 2 :0
Allocation of resource 0 :3
Allocation of resource 1 :0
Allocation of resource 2 :2
Allocation of resource 0 :2
Allocation of resource 1 :1
Allocation of resource 2 :1
Allocation of resource 0 :0
Allocation of resource 1 :0
Allocation of resource 2 :2
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :3
1. Input
2. New Request
4. print
5. Exit
Enter ur choice :2
Requesting process id :1
Request Committed
1. Input
2. New Request
3. Safe State or Not
4. print
5. Exit
Enter ur choice :2
Requesting process id :2
-------------------------------------------------------- END-----------------------------------------------------