0% found this document useful (0 votes)
109 views50 pages

System Software Manual

The document describes programs to implement lexical analysis and parsing. Program 1(a) uses LEX to recognize valid arithmetic expressions and count identifiers and operators. Program 1(b) uses YACC to evaluate arithmetic expressions. Program 2 uses YACC to recognize strings ending with b preceded by n a's. Program 3 constructs a parsing table for a given grammar and parses a sentence. Program 4 demonstrates shift-reduce parsing. Program 5 generates machine code using triples for an intermediate code statement.

Uploaded by

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

System Software Manual

The document describes programs to implement lexical analysis and parsing. Program 1(a) uses LEX to recognize valid arithmetic expressions and count identifiers and operators. Program 1(b) uses YACC to evaluate arithmetic expressions. Program 2 uses YACC to recognize strings ending with b preceded by n a's. Program 3 constructs a parsing table for a given grammar and parses a sentence. Program 4 demonstrates shift-reduce parsing. Program 5 generates machine code using triples for an intermediate code statement.

Uploaded by

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

System Software Laboratory - 18CSL66 VI Semester

PROGRAM 1(a):

Write a LEX program to recognize valid arithmetic expression. Identifiers


in the expression could be only integers and operators could be + and *.
Count the identifiers and operators present and print them separately.

%{
#include<stdio.h>
intnid=0,np=0,nm=0,nmul=0,nd=0;
int flag1=0,flag2=0;
%}
%%
[[] {flag2++;}
[]] {flag2--;}
[a-zA-Z 0-9]+ {nid++;flag1++;}
[+] {np++;flag1--;}
[-] {nm++;flag1--;}
[*] {nmul++;flag1--;}
[/] {nd++;flag1--;}
%%
main()
{
printf("Enter the arithematic operation");
yylex();
if(flag2!=0 || flag1!=1)
{
printf("Invalid expression \n");
return(0);
}
else

Dept. of CSE, RRCE . Page 1


System Software Laboratory - 18CSL66 VI Semester

{
printf("valid arithematic expression \n");
printf("no. of identifiers are %d \n",nid);
printf("no. of plus symbols are %d \n",np);
printf("no. of minus symbols are %d \n",nm);
printf("no. of multiplication symbols are %d \n",nmul);
printf("no. of divison symbols are %d \n",nd);
}
}

Output:
cd (directory name)
lex (file name) .l
cc lex.yy.c –ll
./a.out
Press cntrl d to get the ouput ; Take an output for invalid expression also.

Dept. of CSE, RRCE . Page 2


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 1(b):
Write YACC program to evaluate arithmetic expression involving
operators: +, -, *, and /
LEX PART:
%{
#include "y.tab.h"
externintyylval;
%}
%%
[0-9]* {yylval=atoi(yytext);
return NUM;
}
[\t] ;
\n return 0;
. returnyytext[0];
%%
YACC PART:
%{
#include<stdio.h>
%}
%token NUM
%left '+' '-'
%left '*' '/'
%left '(' ')'
%token .
%%

expr:e {printf("\n Result:%d\n",$$);


return 0;

Dept. of CSE, RRCE . Page 3


System Software Laboratory - 18CSL66 VI Semester

}
e:e'+'e {$$=$1+$3;}
|e'-'e {$$=$1-$3;}
|e'*'e {$$=$1*$3;}
|e'/'e {$$=$1/$3;}
|'('e')' {$$=$2;}
|NUM {$$=$1;}
|.
%%

main()
{
printf("enter the arithmetic expression \n");
yyparse();
printf("\n valid expression \n");
}
yyerror()
{
printf("\n Invalid expression \n");
exit(0);
}
OUTPUT:
lex (file name).l
yacc –d (file name).y
cc –c lex.yy.cy.tab.c
cc –o a.outlex.yy.oy.tab.o -ll
./a.out
Take an output for invalid expression also.

Dept. of CSE, RRCE . Page 4


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 2:
Dept. of CSE, RRCE . Page 5
System Software Laboratory - 18CSL66 VI Semester

Develop, implement and execute a program using YACC tool to recognize


all strings ending with b preceded by n a’s using the grammar a^nb(a
power n) (note:input n value)
LEX PART:
%{
#include "y.tab.h"
%}
%%
[a] return A;
[b] return B;
[\n] return yytext [0];
%%
YACC PART:
%{
#include <stdio.h>
%}
%token A B
%%
str:s '\n'
s: A s1 B|B
s1: ; | A s1
%%
int main()
{
printf("enter the string \n");
if(yyparse()==0)
printf("\n valid string\n");
return 0;
}

Dept. of CSE, RRCE . Page 6


System Software Laboratory - 18CSL66 VI Semester

yyerror()
{
printf("\n invalid string\n");
return 1;
}
OUTPUT:
lex (file name).l
yacc –d (file name).y
cc -c lex.yy.cy.tab.c
cc -o a.outlex.yy.oy.tab.o -ll
./a.out

PROGRAM3:

Dept. of CSE, RRCE . Page 7


System Software Laboratory - 18CSL66 VI Semester

Design, develop and implement YACC/C program to construct Predictive /


LL(1) Parsing Table for the grammar rules: A → aBa , B → bB | ε . Use
this table to parse the sentence: abba$
#include<stdio.h>
#include<string.h>
void main()
{
char fin[10][20],st[10][20],ft[20][20],fol[20][20];
int a=0,e,i,t,b,c,n,k,l=0,j,s,m,p;
printf("enter the no. of coordinates \n");
scanf("%d",&n);
printf("enter the production in a grammer\n");
for(i=0;i<n;i++)
scanf("%s",st[i]);
for(i=0;i<n;i++)
fol[i][0]='\0';
for(s=0;s<n;s++)
{
for(i=0;i<n;i++)
{
j=3;
l=0;
a=0;
l1:if(!((st[i][j]>64)&&(st[i][j]<91)))
{
for(m=0;m<l;m++)
{
if(ft[i][m]==st[i][j])
goto s1;

Dept. of CSE, RRCE . Page 8


System Software Laboratory - 18CSL66 VI Semester

}
ft[i][l]=st[i][j];
l=l+1;
s1:j=j+1;
}
else
{
if(s>0)
{
while(st[i][j]!=st[a][0])
{
a++;
}
b=0;
while(ft[a][b]!='\0')
{
for(m=0;m<l;m++)
{
if(ft[i][m]==ft[a][b])
goto s2;
}
ft[i][l]=ft[a][b];
l=l+1;
s2:b=b+1;
}
}
}
while(st[i][j]!='\0')
{

Dept. of CSE, RRCE . Page 9


System Software Laboratory - 18CSL66 VI Semester

if(st[i][j]=='|')
{
j=j+1;
goto l1;
}
j=j+1;
}
ft[i][l]='\0';
}
}
printf("first pos\n");
for(i=0;i<n;i++)
printf("FIRS[%c]=%s\n",st[i][0],ft[i]);
fol[0][0]='$';
for(i=0;i<n;i++)
{
k=0;
j=3;
if(i==0)
l=1;
else
l=0;

k1:while((st[i][0]!=st[k][j])&&(k<n))
{
if(st[k][j]=='\0')
{
k++;
j=2;

Dept. of CSE, RRCE . Page 10


System Software Laboratory - 18CSL66 VI Semester

}
j++;
}
j=j+1;
if(st[i][0]==st[k][j-1])
{
if((st[k][j]!='|')&&(st[k][j]!='\0'))
{
a=0;
if(!((st[k][j]>64)&&(st[k][j]<91)))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==st[k][j])
goto q3;
}
q3:
fol[i][l]=st[k][j];
l++;
}
else
{
while(st[k][j]!=st[a][0])
{
a++;
}
p=0;
while(ft[a][p]!='\0')
{

Dept. of CSE, RRCE . Page 11


System Software Laboratory - 18CSL66 VI Semester

if(ft[a][p]!='@')
{
for(m=0;m<l;m++)
{
if(fol[i][m]==ft[a][p])
goto q2;
}
fol[i][l]=ft[a][p];
l=l+1;
}
else
e=1;
q2:p++;
}
if(e==1)
{
e=0;
goto a1;
}
}
}
else
{
a1:c=0;
a=0;
while(st[k][0]!=st[a][0])
{
a++;
}

Dept. of CSE, RRCE . Page 12


System Software Laboratory - 18CSL66 VI Semester

while((fol[a][c]!='\0')&&(st[a][0]!=st[i][0]))
{
for(m=0;m<l;m++)
{
if(fol[i][m]==fol[a][c])
goto q1;
}
fol[i][j]=fol[a][c];
l++;
q1:c++;
}
}
goto k1;
}
fol[i][l]='\0';
}
printf("follow pos\n");
for(i=0;i<n;i++)
printf("FOLLOW[%c]=%s\n",st[i][0],fol[i]);
printf("\n");
s=0;
for(i=0;i<n;i++)
{
j=3;
while(st[i][j]!='\0')
{
if((st[i][j-1]=='|')||(j==3))
{
for(p=0;p<=2;p++)

Dept. of CSE, RRCE . Page 13


System Software Laboratory - 18CSL66 VI Semester

{
fin[s][p]=st[i][p];
}
t=j;
for(p=3;((st[i][j]!='|')&&(st[i][j]!='\0'));p++)
{
fin[s][p]=st[i][j];
j++;
}
fin[s][p]='\0';
if(st[i][t]=='@')
{
b=0;
a=0;
while(st[a][0]!=st[i][0])
{
a++;
}
while(fol[a][b]!='\0')
{
printf("m[%c,%c]=%s\n",st[i][0],fol[a][b],fin[s]);
b++;
}
}
else if(!((st[i][t]>64)&&(st[i][t]<91)))
printf("m[%c,%c]=%s\n",st[i][0],st[i][t],fin[s]);
else
{
b=0;

Dept. of CSE, RRCE . Page 14


System Software Laboratory - 18CSL66 VI Semester

a=0;
while(st[a][0]!=st[i][3])
{
a++;
}
while(ft[a][b]!='\0')
{
printf("m[%c,%c]=%s\n",st[i][0],ft[a][b],fin[s]);
b++;
}
}
s++;
}
if(st[i][j]=='|')
j++;
}
}
}
OUTPUT: p
cd (directory name)
cc (filename).c
./a.out

Dept. of CSE, RRCE . Page 15


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 16


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 4:
Design, develop and implement YACC/C program to demonstrate Shift
Reduce Parsing technique for the grammar rules: E → E+T | T, T → T*F
| F, F → (E) | id and parse the sentence: id + id * id.
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
void main()
{
puts("GRAMMAR Is E->E+E\nE->E*E\nE->(E)\nE->id");
puts("enter the input string");
gets(a);
c=strlen(a);
strcpy(act,"SHIFT->");
puts("stack \tinput \t\taction");
for(k=0,i=0;j<c;k++,i++,j++)
{
if(a[j]=='i'&&a[j+1]=='d')
{
stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);

Dept. of CSE, RRCE . Page 17


System Software Laboratory - 18CSL66 VI Semester

check();
}
else
{
stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check();
}
}
}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0;z<c;z++)
if(stk[z]=='i'&&stk[z+1]=='d')
{
stk[z]='E';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0;z<c;z++)
if(stk[z]=='E'&&stk[z+1]=='+'&&stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';

Dept. of CSE, RRCE . Page 18


System Software Laboratory - 18CSL66 VI Semester

printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0;z<c;z++)
if(stk[z]=='E'&&stk[z+1]=='*'&&stk[z+2]=='E')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0;z<c;z++)
if(stk[z]=='('&&stk[z+1]=='E'&&stk[z+2]==')')
{
stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
}
OUTPUT:
cd (directory name)
cc (filename).c
./a.out

Dept. of CSE, RRCE . Page 19


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 20


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 5:
Design, develop and implement a C/Java program to generate the machine
code using triples for the statement
A= -B*(C+D) whose intermediate code in three-address form:
T1= -B
T2= C+D
T3= T1*T2
A= T3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
char op[2],arg1[5],arg2[5],res[5];
void main()
{
FILE *fp1,*fp2;
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
while(!feof(fp1))
{
fscanf(fp1,"%s%s%s%s",res,arg1,op,arg2);
if(strcmp(op,"+")==0)
{
fprintf(fp2,"\n MOV R0,%s",arg1);
fprintf(fp2,"\n ADD R0,%s",arg2);
fprintf(fp2,"\n MOV %s,R0",res);
}
if(strcmp(op,"*")==0)
{
fprintf(fp2,"\n MOV R0,%s",arg1);

Dept. of CSE, RRCE . Page 21


System Software Laboratory - 18CSL66 VI Semester

fprintf(fp2,"\n MUL R0,%s",arg2);


fprintf(fp2,"\n MOV %s,R0",res);
}
if(strcmp(op,"-")==0)
{
fprintf(fp2,"\n MOV R0,%s",arg1);
fprintf(fp2,"\n SUB R0,%s",arg2);
fprintf(fp2,"\n MOV %s,R0",res);
}
if(strcmp(op,"/")==0)
{
fprintf(fp2,"\n MOV R0,%s",arg1);
fprintf(fp2,"\n DIV R0,%s",arg2);
fprintf(fp2,"\n MOV %s,R0",res);
}
if(strcmp(op,"=")==0)
{
fprintf(fp2,"\n MOV R0,%s",arg1);
//fprintf(fp2,"\n ADD R0,%s",arg2);
fprintf(fp2,"\n MOV %s,R0",res);
}
}
fclose(fp1);
fclose(fp2);
//getch();
}
Save the program in .c extension.
CREATE AN INPUT FILE (input.txt).

Dept. of CSE, RRCE . Page 22


System Software Laboratory - 18CSL66 VI Semester

CREATE AN OUTPUT FILE(output.txt).


OUTPUT:
cc (file name).c
./a.out input.txt output.txt
gedit output.txt

Dept. of CSE, RRCE . Page 23


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 6(A):
Write a LEX program to eliminate comment lines in a C program and copy
the resulting program into a separate file.
%{
#include<stdio.h>
int c=0;
%}
%%
[/][*][^."*/"]*[*][/] {c++;fprintf(yyout," ");}
"//".* {c++;fprintf(yyout," ");}
%%
int main(intargc,char *argv[])
{
if(argc!=3)
{
printf("invalid input");
return 0;
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("no of comment lines are %d ",c);
}
Save the program in .l extension.

CREATE AN INPUT FILE (6a.c).

Dept. of CSE, RRCE . Page 24


System Software Laboratory - 18CSL66 VI Semester

CREATE AN OUTPUT FILE(new.c).


OUTPUT:

The output is generated without the comment lines.

PROGRAM 6(B):
Write YACC program to recognize valid identifier, operators and
keywords in the given text(C program) file.
LEX PART:
%{
#include<stdio.h>
#include "y.tab.h"
externyylval;

Dept. of CSE, RRCE . Page 25


System Software Laboratory - 18CSL66 VI Semester

%}
%%
[\t];
[+|-|*|/|=|<|>] {printf("Operator is %s\n",yytext);return OP;}
[0-9]+ {yylval=atoi(yytext);printf("Numbers is %d \n",yylval);return DIGIT;}
int|char|bool|float|void|for|do|while|if|else|return|void {printf("Keyword is %s
\n",yytext);return KEY;}
[a-zA-Z0-9]+ {printf("Identifier is %s \n",yytext);return ID;}
.;
%%
YACC PART:
%{
#include<stdio.h>
#include<stdlib.h>
int id=0,dig=0,key=0,op=0;
%}
%token DIGIT ID KEY OP
%%
input:
DIGIT input {dig++;}
|ID input {id++;}
|KEY input {key++;}
|OP input {op++;}
|DIGIT {dig++;}
|ID {id++;}
|KEY {key++;}
|OP {op++;}
;
%%

Dept. of CSE, RRCE . Page 26


System Software Laboratory - 18CSL66 VI Semester

#include<stdio.h>
externintyylex();
externintyyparse();
extern FILE *yyin;
main()
{
FILE *myfile = fopen("a.c","r");
if(!myfile)
{
printf("I cant open sam_input.c !");
return -1;
}
yyin=myfile;
do{
yyparse();
}while(!feof(yyin));
printf("Numbers = %d \n Keywords = %d \n Identifiers=%d \n Operators=
%d \n",dig,key,id,op);
}
voidyyerror(){
printf("EEk, parse error!Message: ");
exit(-1);
}
OUTPUT:
cd (directory name)
lex (file name).l
yacc -d (file name).y
cc lex.yy.cy.tab.c -ll
./a.out

Dept. of CSE, RRCE . Page 27


System Software Laboratory - 18CSL66 VI Semester

CREATE AN INPUT FILE


(“a.c”)

Dept. of CSE, RRCE . Page 28


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 7:
Design, develop and implement a C/C++/Java program to simulate the
working of Shortest remaining time and Round Robin (RR) scheduling
algorithms. Experiment with different quantum sizes for RR algorithm

#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];
struct proc temp;
int no;
intchkprocess(int);
intnextprocess();
void roundrobin(int ,int,int[],int[]);
void srtf(int);
main()
{
intn,tq,choice;

Dept. of CSE, RRCE . Page 29


System Software Laboratory - 18CSL66 VI Semester

intbt[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 the no. of process:\n");
scanf("%d",&n);
printf("Enter the burst time for sequences:");
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
st[i]=bt[i];
}
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 no. of process:");
scanf("%d",&n);
srtf(n);
break;
case 3:exit(0);

Dept. of CSE, RRCE . Page 30


System Software Laboratory - 18CSL66 VI Semester

}
}
}
void roundrobin(intn,inttq,intst[],intbt[])
{
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];
st[i]=0;
}
sq=sq+temp1;
tat[i]=sq;
}

Dept. of CSE, RRCE . Page 31


System Software Laboratory - 18CSL66 VI Semester

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_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 avgturn around time is %f\n",awt,atat);
}
intchkprocess(int s)
{
int i;
for(i=1;i<=s;i++)
{
if(process[i].rem!=0)
return 1;
}
return 0;
}
intnextprocess()
{
intmin,l,i;

Dept. of CSE, RRCE . Page 32


System Software Laboratory - 18CSL66 VI Semester

min=32000;
for(i=1;i<=no;i++)
{
if(process[i].rem!=0&&process[i].rem<min)
{
min=process[i].rem;
l=i;
}
}
return l;
}
void srtf(int n)
{
inti,j,k,time=0;
float tavg,wavg;
for(i=1;i<=n;i++)
{
process[i].id=i;
printf("\n \n enter vthe 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)

Dept. of CSE, RRCE . Page 33


System Software Laboratory - 18CSL66 VI Semester

{
temp=process[i];
process[i]=process[j];
process[j]=temp;
}
}
}
no=0;
j=1;
while(chkprocess(n)==1)
{
if(process[no+1].arrival==time)
{
while(process[no+1].arrival==time)
no++;
if(process[j].rem==0)
process[j].finish=time;
j=nextprocess();
}
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

Dept. of CSE, RRCE . Page 34


System Software Laboratory - 18CSL66 VI Semester

{
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\nProcess Arrival Burst Waiting Finish 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;
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;
wavg=wavg+process[i].wait;
printf("\n\n");
}
tavg=tavg/n;
wavg=wavg/n;
printf("tavg=%f\t wavg=%f\n",tavg,wavg);
}
OUTPUT:

Dept. of CSE, RRCE . Page 35


System Software Laboratory - 18CSL66 VI Semester

cd (directory name)
cc (filename).c
./a.out

Dept. of CSE, RRCE . Page 36


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 37


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 8:
Design, develop and implement a C/C++/Java program to implement
Banker’s algorithm. Assume suitable input required to demonstrate the
results.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int max[10][10],need[10][10],alloc[10]
[10],avail[10],completed[10],safeSequence[10];
intp,r,i,j,process,count;
count=0;
printf("enter the no. of process:");
scanf("%d",&p);
for(i=0;i<p;i++)
completed[i]=0;
printf("\n\n enter the no. of resources:");
scanf("%d",&r);
printf("\n\n enter the max matrix for each process:");
for(i=0;i<p;i++)
{
printf("\nfor process %d:",i+1);
for(j=0;j<r;j++)
scanf("%d",&max[i][j]);
}
printf("\n\n enter the allocation for each process:");
for(i=0;i<p;i++)
{
printf("\n for process %d:",i+1);

Dept. of CSE, RRCE . Page 38


System Software Laboratory - 18CSL66 VI Semester

for(j=0;j<r;j++)
scanf("%d",&alloc[i][j]);
}
printf("\n\n enter the available resource:");
for(i=0;i<r;i++)
scanf("%d",&avail[i]);
for(i=0;i<p;i++)
for(j=0;j<r;j++)
need[i][j]=max[i][j]-alloc[i][j];
do
{
printf("\n max matrix :\t allocation matrix:\n");
for(i=0;i<p;i++)
{
for(j=0;j<r;j++)
printf("%d",max[i][j]);
printf("\t\t");
for(j=0;j<r;j++)
printf("%d",alloc[i][j]);
printf("\n");
}

process=-1;
for(i=0;i<p;i++)
{
if(completed[i]==0)
{
process=i;
for(j=0;j<r;j++)

Dept. of CSE, RRCE . Page 39


System Software Laboratory - 18CSL66 VI Semester

{
if(avail[j]<need[i][j])
{
process=-1;
break;
}
}
}
if(process!=-1)
break;
}
if(process!=-1)
{
printf("\n process %d runs to completion!",process+1);
safeSequence[count]=process+1;
count++;
for(j=0;j<r;j++)
{
avail[j]+=alloc[process][j];
alloc[process][j]=0;
max[process][j]=0;
completed[process]=1;
}
}
}
while(count!=p && process!=-1);
if(count==p)
{
printf("\n the system is in a safe state!!\n");

Dept. of CSE, RRCE . Page 40


System Software Laboratory - 18CSL66 VI Semester

printf("safe sequence:<");
for(i=0;i<p;i++)
printf("%d",safeSequence[i]);
printf(">\n");
}
else

printf("the system is in an unsafe state!!");


}
OUTPUT:p

Dept. of CSE, RRCE . Page 41


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 42


System Software Laboratory - 18CSL66 VI Semester

PROGRAM 9:

Design, develop and implement a C/C++/Java program to implement page


replacement algorithms LRU and FIFO. Assume suitable input required to
demonstrate the results.
#include<stdio.h>
#include<stdlib.h>
void FIFO(char[],char[],int,int);
void lru(char[],char[],int,int);
void opt(char[],char[],int,int);
int main()
{
intch,YN=1,i,l,f;
char F[10],s[25];

printf("\n\n\t enter the no. of empty frames:");


scanf("%d",&f);
printf("\n\n\tenter the length of the string:");

Dept. of CSE, RRCE . Page 43


System Software Laboratory - 18CSL66 VI Semester

scanf("%d",&l);
printf("\n\n\tenter the string:");
scanf("%s",s);
for(i=0;i<f;i++)
F[i]=-1;
do
{
printf("\n\n\t********MENU*********");
printf("\n\n\t1:FIFO\n\n2:LRU\n\n3:EXIT");
printf("\n\n\tenter your choice:");
scanf("%d",&ch);

switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
F[i]=-1;
}
FIFO(s,F,l,f);
break;

case 2:
for(i=0;i<f;i++)
{
F[i]=-1;
}
lru(s,F,l,f);
break;

Dept. of CSE, RRCE . Page 44


System Software Laboratory - 18CSL66 VI Semester

case 3:
exit(0);
}

printf("\n\n\tDo you want to continue IF YES press 1\n\n\t IF NO press 0:");


scanf("%d",&YN);
}while(YN==1);
return(0);
}

void FIFO(char s[],char F[],intl,int f)


{
inti,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])
flag=1;
}
if(flag==0)
{
printf("\n\t%c\t",s[i]);
F[j]=s[i];
j++;
for(k=0;k<f;k++)
{

Dept. of CSE, RRCE . Page 45


System Software Laboratory - 18CSL66 VI Semester

printf("%c",F[k]);
}
printf("\tpage-fault%d",cnt);
cnt++;
}
else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
{
printf("%c",F[k]);
}
printf("\tNo page-fault");
}
if(j==f)
j=0;
}
}

void lru(char s[],char F[],intl,int f)


{
inti,j=0,k,m,flag=0,cnt=0,top=0;
printf("\n\t PAGE\t FRAMES\t FAULTS");
for(i=0;i<l;i++)
{
for(k=0;k<f;k++)
{
if(F[k]==s[i])

Dept. of CSE, RRCE . Page 46


System Software Laboratory - 18CSL66 VI Semester

{
flag=1;
break;
}
}
printf("\n\t%c\t",s[i]);
if(j!=f&&flag!=1)
{
F[top]=s[i];
j++;
if(j!=f)
top++;
}
else
{
if(flag!=1)
{
for(k=0;k<top;k++)
{
F[k]=F[k+1];
}
F[top]=s[i];
}
if(flag==1)
{
for(m=k;m<top;m++)
{
F[m]=F[m+1];
}

Dept. of CSE, RRCE . Page 47


System Software Laboratory - 18CSL66 VI Semester

F[top]=s[i];
}
}
for(k=0;k<f;k++)
{
printf("%c",F[k]);
}
if(flag==0)
{
printf("\tPage-fault%d",cnt);
cnt++;
}
else
printf("\tNo page-fault");
flag=0;
}
}OUTPUT:

Dept. of CSE, RRCE . Page 48


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 49


System Software Laboratory - 18CSL66 VI Semester

Dept. of CSE, RRCE . Page 50

You might also like