You are on page 1of 36

lex

1) a. Program to count the number of characters, words, spaces and lines in a given input file. %{ #include<stdio.h> int cc=0,bc=0,wc=0,lc=0; %} %% [^ \t\n]+ { wc++; cc=cc+yyleng; } \n lc++; " " bc++; \t bc=bc+5; %% main(int argc,char *argv[]) { if (argc!=2) { printf("\n usage:./a.out filename\n"); return(0); } yyin=fopen(argv[1],"r"); yylex(); printf("\n no of lines are %d\n",lc); printf("\n no of words are %d\n",wc); printf("\n no of blanks %d\n",bc); printf("\n no of character %d\n",cc); }

ex.c this is my book sslab lab exam Output: $ lex prg1.l $ cc lex.yy.c -ll $ ./a.out ex.c no of lines are 3 no of words are 7 no of blanks 9 no of character 24

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. %{ /* this is simple program*/ int comment=0; /*begging*/ %} int main() %% { *",* /*" {comment++;} inta,b; ^[\t]*"/*".*"*/" {comment++;} } .; /*end of the program*/ Output : %% $ lex prg1b.l main(int argc,char *argv[]) $ cc lex.yy.c -ll { $ ./a.out ex.c re.c if(argc!=3) Number of comment line=3 { $ cat re.c printf("Pass 3 Arguments \n"); int main() return(0); { } Int a,b; yyin=fopen(argv[1],"r"); } yyout=fopen(argv[2],"w"); yylex(); printf("Number of comment line=%d \n",comment); }
ex.c

2) a. Program to recognize a valid arithmetic expression and to recognize the identifiers and operators present. Print them separately.
%{ int iden=0,digit=0,opr=0,plus=0,minus=0,mul=0,divide=0,top=-1,valid=1; char stack[50]; %} dig[0-9] Output %% {dig}+ {digit++;} $ lex prg2a.l [a-zA-Z]+ {iden++;} $ cc lex.yy.c -ll [+] {opr++; plus++;} $ ./a.out [-] {opr++; minus++;} Enter Arithmetic Expression: [*] {opr++; mul++;} a+b*c/5*4 [/] {opr++; divide++;} Valid Input \{ {stack[++top]='{';} Number of Identifiers:3 \} {if(stack[top--]!='{') valid=0;} Number of Digits:2 \[ {stack[++top]='[';} Number of Operators:4 \] {if(stack[top--]!='[') valid=0;} Number of plus:1 \( {stack[++top]='(';} \) {if(stack[top--]!='(') valid=0;} Number of minus:0 %% Number of Multiplication:2 main() Number of Divide:1 { printf("Enter Arithmetic Expression:\n"); yylex(); if(valid==1 && top==-1&&((digit+iden)==(opr+1))) { printf("Valid Input \n"); printf("Number of Identifiers:%d\n",iden); printf("Number of Digits:%d\n",digit); printf("Number of Operators:%d \n",opr); printf("Number of plus:%d \n",plus); printf("Number of minus:%d \n",minus); printf("Number of Multiplication:%d \n",mul); printf("Number of Divide:%d \n",divide); } else { printf("Invalid Input \n"); } }
3

2) b. Program to recognize whether a given sentence is simple or compound. %{ int cs=0; %} %% [/t/n]+; and|or|but|there|either|nevertheless {cs=1;ECHO;} %% main() { printf("enter the statement\n"); yylex(); if(cs==1) { printf("statement is compound\n"); } else printf("statement is simple\n"); }

Output $ lex prg2b.l $ cc lex.yy.c -ll $ ./a.out enter the statement this is ball this is ball statement is simple

3) Program to recognize and count the number of identifiers in a given input file. %{ #include<stdio.h> new.c int count=0; #include<stdio.h> %} main() %% { "int" | int a; "float" | float b; "double" | char x; "char" { char ch; } ch=input(); Output : while(1) { $ lex lab3.l if(ch==',') count++; $ cc lex.yy.c -lfl if(ch==';') { $ ./a.out new.c count++; break; #include<stdio.h> } main() if(ch=='\n') break; { ch=input(); } } } No of identifiers = 3 . | '\n' ; %% int main(int argc,char *argv[]) { if(argc!=2) printf("\n\nINVALID INPUT\n\n"); else { yyin=fopen(argv[1],"r"); yylex(); printf("\nNo of identifiers = %d\n",count); } }

PART A YACC 4) a. Program to recognize a valid arithmetic expression that uses operators +, -, * and /. Lex part : %{ #include "y.tab.h" %} %% [a-zA-Z0-9]+ return ID; = return EQ; \( return LB; \) return RB; [-+] return SA; [*/] return MD; %% Yacc part : %{ #include<stdio.h> %} %token ID EQ LB RB SA MD %% assign:ID EQ expr|expr; expr:term|expr SA term; term:factor|term MD factor; factor:ID|LB expr RB; %% main() { printf("Enter the Expression: \n"); if(yyparse()==0) printf("Valid Expression \n"); } yyerror() { printf("Invalid Expression \n"); }
6

Output $ lex lab4a.l $ yacc -d lab4a.y $ cc y.tab.c lex.yy.c -ll $ ./a.out Enter the Expression: a+c*d/g Valid Expression $ ./a.out Enter the Expression: a*c/+ Invalid Expression

4) b. Program to to recognize a valid variable, which starts with a letter, followed by any number of letters or digits. Lex Part : %{ #include"y.tab.h" %} %% [a-zA-Z]+[0-9]* {return VALID;} [0-9]+[a-zA-Z]* {return INVALID;} %% Yacc Part : %{ #include<stdio.h> %} %token VALID INVALID %% start: start VALID {printf("Valid Format.\n");} |start INVALID {printf("Invalid Format.\n");} |"\n" |; %% main() { printf("Enter the Variable:"); yyparse(); } yyerror(char *s) { }

Output $ lex lab4b.l $ yacc -d lab4b.y $ cc y.tab.c lex.yy.c -ll $ ./a.out Enter the Variable: abc12 Valid Format. $ ./a.out Enter the Variable: 12ert Invalid Format.

5) a. Program to evaluate an arithmetic expression involving operators +, -, *,/ Lex part : %{ #include<stdio.h> #include"y.tab.h" extern int yylval; %} %% [0-9]+ { yylval=atoi(yytext); return NUM; } Output [\t] ; \n return 0; $ pluma lab5a.l . return yytext[0]; $ pluma lab5a.y %% $ lex lab5a.l Yacc Part: $ yacc -d lab5a.y %{ $ cc lex.yy.c y.tab.c -ll #include<stdio.h> $ ./a.out #include<stdlib.h> enter the arithmetic expression: %} 2+8 %token NUM result:10 %left '+' '-' valid expression %left '*' '/' %left '(' ')' $ ./a.out %% expr: e enter the arithmetic expression: { printf("result:%d\n",$$); 1+9* return 0; invalid expression } e:e'+'e {$$=$1+$3;} |e'-'e {$$=$1-$3;} |e'*'e {$$=$1*$3;} |e'/'e {$$=$1/$3;} |'('e')' {$$=$2;} | NUM {$$=$1;} ; %%

main() { printf("\n enter the arithematic expression:\n"); yyparse(); printf("\n valid expression\n"); } yyerror() { printf("\n invalid expression\n"); exit(0); }

5 b) Program to recognize strings 'aaab', 'abbb', 'ab', 'a' using the grammer.(a^n b^n ,n>=0) Lex part : %{ #include "y.tab.h" %} %% a return A; b return B; .|\n return yytext[0]; %% Yacc Part : %{ #include<stdio.h> int valid=1; %} %token A B %% %% S:S1 S2; S1:AS1/A; S2:BS2/B/ ; ,

Output $ lex lab5b.l $ yacc -d lab5b.y $ cc lex.yy.c y.tab.c -ll $ ./a.out Enter the string: aaabbb valid string $ ./a.out Enter the string: aasd invalid string

main() { printf("Enter the string:\n"); yyparse(); if(valid==1) printf("\n valid string"); } yyerror() { valid=0; printf("\n invalid string"); return 1; }

6) Program to recognize the grammer (an b ,n>=10). Lex Part : %{ #include "y.tab.h" %} %% a {return X;} b {return Y;} \n {return 0;} %% Yacc Part : %{ #include<stdio.h> #include<stdlib.h> %} %token X Y %% exp:X X X X X X X X X X exp1 Y; exp1: X exp1 |; %%

Output $ lex lab6.l $ yacc -d lab6.y $ cc lex.yy.c y.tab.c -ll $ ./a.out Enter the String: aaaaadfdff String unrecognised. $ ./a.out Enter the String: aaaaaaaaaab String is Recognised.

10

main() { printf("Enter the String:"); yyparse(); printf("String is Recognised.\n"); } int yyerror() { printf("String unrecognised.\n"); exit(1); } 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 ranges, then executing ranges A B C should produce C B A on the standard output). if [ $# -eq 0 ] then echo "No Arguments Passed" exit fi num=$# while [ $num -gt 0 ] do eval echo \$"$num" let num=$num-1 done

OUTPUT $ pluma 7a.sh $ chmod +x 1a.sh $ ./1a.sh A B C D E F

F E D C B A

11

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. include<stdio.h> int main() { char str[10]; int pid; pid=fork(); if(!pid) { printf("Child process..."); printf("\nEnter a command:"); scanf("%s",str); system(str); printf("Finished with child"); } else { wait(); printf("\nParent Process"); } return 0; }

OUTPUT
$ pluma 7b.c $ chmod +x 7b.c $ cc 7b.c $ ./a.out Child process Created Enter the Command to be executed:cal August 2012 Su Mo Tu We Th Fr Sa 1234 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Finished with child Parent Process

12

8) a. Write a shell script that accepts two filenames as arguments, checks if the permissions for these files are identical & if the permissions are identical, outputs the common permissions, otherwise outputs each file names followed by its permissions. if [ ! -e $1 ] then echo "File 1 doesnot exist" exit fi if [ ! -e $2 ] then echo "File 2 doesnot exist" exit fi if [ $# -ne 2 ] then echo "2 arguments are not given" exit fi ls -l $1 | cut -c 2-10 > t ls -l $2 | cut -c 2-10 > s cmp t s if [ $? -eq 0 ] then echo "Common File Permission" cat t else echo "File 1" cat t echo "File 2" cat s fi

OUTPUT $ pluma 8a.sh $ chmod +x 8a.sh $ ./8a.sh 1a.sh a.out Common File Permission rwxr-xr-x $ ./2a.sh 1a.sh file1 t s differ: byte 3, line 1 File 1 rwxr-xr-x File 2 rw-rr--

13

8) b. Write a C program to create a file with 16 bytes of arbitrary data from the beginning and another 16 bytes of arbitrary data from an offset of 48.Display the file contents to demonstrate how the hole in file is handled.

#include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<unistd.h> #include<stdio.h> int main() { char buf1[]="abcdefghijklmnop"; char buf2[]="ABCDEFIJKLMNOP"; int fd; fd=creat("t.txt",O_WRONLY|777); write(fd,buf1,16); lseek(fd,48,SEEK_SET); write(fd,buf2,16); return 0; }

OUTPUT
$ pluma 8b.c $ chmod +x 8b.c $ cc 8b.c $ ./a.out $ vi t.txt abcdefghijklmnop^@^@^@^@^@^@^@^ @^@^@^@^@^@^@^@^@^@^@^@^ @^@^@^@^@^@^@^@^@^@^@^@^ @ABCDEFIJKLMNOP^@^@ ~ ~ "t.txt" [readonly][noeol] 1L, 64C 1,1 All

14

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

for i do echo echo code to recreate the file $i >>cr.sh echo cat $i << endf >> cr.sh cat $i>>cr.sh echo endf >> cr.sh echo echo creation done >> cr.sh done

9) b. Write a C program to create 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 & prints its own process id & the id of its child process and then exits. #include<stdio.h> int main() { char str[10]; int pid; pid=fork(); if(!pid) { printf("Child process..."); printf("\n\nChild PID : %d",getpid()); printf("\nParent PID : %d",getppid()); printf("\n\nFinished with child\n");
15

} else { wait(); printf("\nParent process"); printf("\nPARENT PID : %d",getpid()); printf("\nChild PID : %d",pid); } return 0; }

OUTPUT $ pluma 9b.c $ chmod +x 9b.c $ cc 9b.c $ ./a.out Child process... Child PID : 3034 Parent PID : 3033 Finished with child Parent process PARENT PID : 3033 Child PID : 3034

16

OS PROGRAMS:
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 the cases determine the average turn-around time. The input can be read from keyboard or from a file.
pluma rr.c

#include<stdio.h> #include<stdlib.h> struct proc { int id,arrival,burst,rem,wait,finish,turnaround; float ratio; }process[10]; struct proc temp; int no,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 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 the burst time for sequences"); for(i=0;i<n;i++)
17

{ scanf("%d",&bt[i]); st[i]=bt[i]; } printf("enter te quantum time:"); 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); } } } 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) { count++; continue; } if(st[i]>tq) st[i]=st[i]-tq; else if(st[i]>=0) { temp1=st[i];
18

st[i]=0; } sq=sq+temp1; tat[i]=sq; } if(n==count) break; } for(i=0;i<n;i++) { wt[i]=tat[i]-bt[i]; swt=swt+wt[i]; stat=stat+tat[i]; } awt=(float)swt/n; atat=(float)stat/n; printf("process n0 burst time waittime turnaround 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); } int chkprocess(int s) { int i; for(i=1;i<=s;i++) { if(process[i].rem!=0) return 1; } return 0; } int nextprocess() { int min,l,i; min=32000; for(i=1;i<=no;i++) { if(process[i].rem!=0&&process[i].rem<min) { min=process[i].rem;
19

l=i; } } return l; } 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) { 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();
20

} if(process[j].rem!=0) { process[j].rem--; 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\n---SHORTEST REMAINING TIME FIRST==="); printf("\n\nprocess arrival burst waiting finishing turnaround Tr/Tb\n\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; 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].burs t,process[i].wait,process[i].finish,process[i].turnaround,process[i].ratio); tavg=tavg+process[i].turnaround; wavg=wavg+process[i].wait; printf("\n\n"); } tavg=tavg/n; wavg=wavg/n; printf("tavg=%f\t wavg=%f\n",tavg,wavg); } OUTPUT: student@csadmin-pc ~ $ chmod +x rr.c student@csadmin-pc ~ $ cc rr.c
21

student@csadmin-pc ~ $ ./a.out enter the choice 1. round robin 2.SRT 3.exit 1 round robin scheduling algorithm enter number of processes: 3 enter the burst time for sequences24 3 3 enter te quantum time:4 process n0 burst time waittime turnaround 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

enter the arrival time for process 3: 2


22

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 1 2 3 4 time 0 1 2 3 time 8 4 9 5 9 0 15 2 time 17 5 26 10 time 17 4 24 7 time 2.1 1.0 2.7 1.4

tavg=12.686142 wavg=6.500000 enter the choice 1. round robin 2.SRT 3.exit 3 -----------------------------------------------------------------------------------------------------------------------

23

11) Using openmp, design, develop and run a multithreaded program to generate and print Fibonacci series. One thread has to generate the numbers up to specified limit and another thread has to print them. Ensure proper synchronization.
pluma fi.c

#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); } } int random_num() { max=rand()%24; return (max); }
24

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); omp_set_num_threads(2); #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(); #pragma omp critical { 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: student@csadmin-pc ~ $ pluma fi.c student@csadmin-pc ~ $ chmod +x fi.c student@csadmin-pc ~ $ cc -fopenmp fi.c student@csadmin-pc ~ $ ./a.out please enter the number range:5 the number of threads are 2 the number value is 7:1 the number of threads are 2 1 2 3 5 8 13 the number value is 22:1 89 144 233 1 377 2 610 3 987 5 8 13 21 34 55 1597 2584 4181 6765 10946 17711

25

the number value is 9:1 the number value is 19:1 89 144 233

1 1 377

2 2 610

3 3 987

13

21

34 34 55

5 8 13 21 1597 2584 4181

the number value is 17:1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 --------------------------------------------------------------------------------------------------------------------

12) Design, develop and run a program to implement the banker's algorithm. Demonstrate its working with different data values.
pluma b.c

#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.safestate 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:
26

printf("\n\n\tenter the 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 resources:"); for(j=0;j<n;j++) { for(k=0;k<nor;k++) { if(j==0) { printf("\n\n\t for resource tyype %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]; } }
27

break; case 2: violationcheck=0; waitcheck=0; printf("\n\n\t request 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 resources;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(ch==1) printf("\n\t request committed"); }
28

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 safestate"); 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; } //getch(); } while(ch!=5); } int safeseq() { int tj,tk,i,j,k,ss=0; //ss=0; for(j=0;j<nor;j++) work[j]=avail[j];
29

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 printf("\n\n\t the system is not in safe state"); return 0;
30

} OUTPUT: student@csadmin-pc ~ $ pluma b.c student@csadmin-pc ~ $ chmod +x b.c student@csadmin-pc ~ $ cc b.c student@csadmin-pc ~ $ ./a.out 1.input 2.new request 3.safestate or not 4.print 5.exit enter ur choice:1

enter the number of processes:5

enter the number of resources3

enter the available resources: for resource tyype 0: 10

for resource tyype 1: 5

for resource tyype 2: 7

enter max and allocated resources for P 0: enter the max of resource 0: 7

31

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: enter the max of resource 0: 9

32

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

33

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.safestate or not 4.print 5.exit enter ur choice:3

the system is in safe stateP1P3P4P0P2 the system is in safe state 1.input 2.new request 3.safestate or not
34

4.print 5.exit enter ur choice:2

request 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 stateP1P3P4P0P2 1.input 2.new request 3.safestate or not 4.print 5.exit enter ur choice:2

request process id: 2

number of request for resource 0: 2

35

number of request for resource 1: 2

number of request for resource 2: 3

the process exceeds it's max need:terminated 1.input 2.new request 3.safestate or not 4.print 5.exit enter ur choice:5 -------------------------------------------------------------------------

ALL THE BEST

36

You might also like