Vous êtes sur la page 1sur 33

B.L.D.E.A’s V.P. Dr.P.G.

Halakatti College of Engineering & Technology,


Bijapur-586103

PART - A
LEX and YACC Programs:
Design, develop, and execute the following programs using LEX:

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

%{
#include<stdio.h>
int charcount=0;
int spacecount=0;
int linecount=0;
int wordcount=0;
%}

%%
[ ] {spacecount++;}
\n {linecount++;}
[^ \t\n]+ {wordcount++; charcount+=yyleng;}
%%
int main()
{
yyin=fopen("1a_input.txt","r");
yylex();
p rintf("the number of characters are=%d\n",charcount);

printf("the number of spaces are= %d\n",spacecount);

printf("the number of lines are= %d\n",linecount);

printf("the number of words are= %d\n",wordcount);

return 0;
}

OUTPUT
lex filename.l
cc lex.yy.c –lfl
./a.out

BLDEACET/Lab Manual/ISE/10CSL58 1
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

1b) Program to count the numbers of comment lines in a given C


program.Also eliminate them and copy the resulting program into
separate file.

%{
int count=0;
FILE *out;
%}

%%

"//".* {count++;}
"/*"[^/*]*"*/" {count++;}
.|\n { fprintf(out,"%s",yytext);}

%%
main()
{
FILE *fp;
yyin=fopen("ise","r");

out=fopen("cse","w");

yylex();
printf("Comment count=%d\n",count);

OUTPUT
vi ise //write pgm contain few comment line
cc lex filename.l
lex.yy.c –lfl
./a.out
cat cse //out put file with comment line removed from input file

BLDEACET/Lab Manual/ISE/10CSL58 2
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

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


identifiers and operators present. Print them separately.

%{
int oprt=0,oprn=0,ob=0,cb=0;
%}

id [a-zA-Z0-9]+

%%

{id} {oprn++;printf("\n The operand is : %s",yytext);}


[+] {oprt++;printf("\n The operator is : +");}
[-] {oprt++;printf("\n The operator is : -");}
[*] {oprt++;printf("\n The operator is : *");}
[/] {oprt++;printf("\n The operator is : /");}
[(] {ob++;printf("\n The bracket is : (");}
[)] {cb++;printf("\n The bracket is : )");}
%%
int main()
{
printf("\n Enter exp :\n ");
yylex();
if((ob-cb==0)&&(oprn>oprt))
printf("\n Valid");
else
printf("\n Invalid");
printf("number of operand=%d\n",oprn);
printf("number ofoper=%d\n",oprt);
return 0;
}

OUTPUT
lex filename.l
lex.yy.c -lfl
./a.out
Enter an exp.
A+B
Valid

BLDEACET/Lab Manual/ISE/10CSL58 3
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

Number of operand 2
Number of operator 1
Enter an exp
(a+b)-(c-d)*(e+f)
Valid
Number of operand 6
Number of operator 3
Enter an exp
Ab+
invalid

BLDEACET/Lab Manual/ISE/10CSL58 4
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

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

%{
int flag=0;
%}
%%

" "and" " {++flag;}


" "AND" " {++flag;}
" "or" " {++flag;}
" "OR" " {++flag;}
" "then" " {++flag;}
" "THEN" " {++flag;}
. {;}

%%

int main()
{
printf("enter the sentence\n");
yylex();
if(flag)

printf("\n compound entenence\n");


else
printf("\n simple\n");
return 0;
}

OUTPUT
lex filename.l
cc Lex .yy.c –lfl
./a.out
Enter the sentence
bld
ctrl+d
Simple sentence
Enter the sentence
bld and bjp

BLDEACET/Lab Manual/ISE/10CSL58 5
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

ctrl+d
compound sentence

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

%{
int idcount=0;
%}
datatype ["float"|"int"|"char"|"void"|"double"]
id [a-zA-Z][a-zA-Z0-9=\[\]\(\)_]*
%%
{datatype}[ \t]+{id} {idcount++;}
,[ \t]*{id} {idcount++;}
.|\n {}
%%
main()
{
FILE *in;
in=fopen("input.c", "r");
yyin=in;
yylex();
printf("the identifiers in the file are %d\n", idcount);
}
OUTPUT
vi input.c
lex filename.l
cc lex.yy.c –lfl
./a.out
Number of id..

BLDEACET/Lab Manual/ISE/10CSL58 6
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

YACC

Design, develop, and execute the following programs using YACC:


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

Lex file
%{
#include"y.tab.h”
%}
%%
[a-zA-Z][a-zA-z0-9_]* {return(id);}
[0-9]*"."[0-9]+ {return(rnum);}
[0-9]+ {return(inum);}
[ ]+ ;
.|\n {return(yytext[0]);}
%%

Yacc file
%{
#include<stdio.h>
%}
%token rnum inum
%token id
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS

%%
S: E '\n' {printf("valid\n");}
|id '=' E '\n' {printf("valid\n");}
|error {printf("invalid \n");}
;

E: E '+' E
|E '-' E
|E '*' E
|E '/' E
|'(' E ')'

BLDEACET/Lab Manual/ISE/10CSL58 7
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

|'-'E %prec UMINUS


| rnum
|inum
|id
%%
main()
{
Printf(“ente an expression\n”);
yyparse();
}
yyerror( )
{

Output
lex filename.l
yacc –d filename.y
cc y.tab.c lex.yy.c -lfl
./a.out

BLDEACET/Lab Manual/ISE/10CSL58 8
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

4b) Program to recognize a valid variable, which starts with a letter, followed
by any number of letters or digits.

Lex file
%{
#include"y.tab.h"
%}
%%
[ _ ]?[a-zA-z][a-zA-Z0-9_]* {return(id);}

[ ]+ ;
.|\n {return(yytext[0]);}
%%
Yacc file
%{
#include<stdio.h>
%}
%token id
%%
S: id '\n' {printf("valid \n");}
|error {printf("invalid\n");}
;
%%
main()
{
Printf(“enter the input\n”);
yyparse();
}
yyerror( )
{

}
Output
lex filename.l
yacc –d filename.y
cc y.tab.c lex.yy.c -lfl
./a.out
_ab
Valid
A09

BLDEACET/Lab Manual/ISE/10CSL58 9
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

Valid
0ab
Invalid
5a) Program to evaluate an arithmetic expression involving operators +, -, * and
/.
Lex file
%{
#include"y.tab.h“
Extern int yylval;
%}
%%
[0-9]+ {yylval = atoi(yytext); return(inum);}
[a-zA-z_][a-zA-z_]* {return(id);}
[ ]+ ;
.|\n {return(yytext[0]);}
%%
Yacc file
%{
#include<stdio.h>
%}
%token inum
%token id
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%%
S: E '\n' {printf("%d",$1);}
|id '-' E '\n' {printf("%d",$3);}
;
E: E '+' E {$$=$1+$3;}
|E '-' E {$$=$1-$3;}
|E '*' E {$$=$1*$3;}
|E '/' E {if($3!=0) {
$$=$1/$3;}
else {printf("divide by zero error\n");exit(0);}}
|'(' E ')' {$$=$2;}
|'-' E %prec UMINUS {$$=-$2;}
|inum {$$=$1;}
;

BLDEACET/Lab Manual/ISE/10CSL58 10
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

%%
main()
{
printf(“enter the exp to eval\n”);
yyparse();
}
yyerror( )
{

output
lex filename.l
yacc –d filename.y
cc y.tab.c lex.yy.c -lfl
./a.out
Enter the exp to eval
(2*3)+(5+8)
19

BLDEACET/Lab Manual/ISE/10CSL58 11
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

5b) Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using the grammar
(anbn, n>= 0).
Lex file
%{
#include"y.tab.h"
%}
%%
a {return(Ta);}
b {return(Tb);}
.|\n {return(yytext[0]);}
%%
Yacc file
%{
#include<stdio.h>
%}
%token Ta Tb
%%
s:exp '\n' {printf("valid\n");}
|error {printf("invalid\n");}
;
exp : Ta exp Tb
|
;
%%
main()
{
yyparse();
}
yyerror( )
{

output
lex filename.l
yacc –d filename.y
cc y.tab.c lex.yy.c -lfl
./a.out

BLDEACET/Lab Manual/ISE/10CSL58 12
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

aaab
Invalid
aabb
Valid
6) Program to recognize the grammar (anb, n>= 10).

Lex file
%{
#include"y.tab.h"
%}
%%
aaaaaaaaaa[a]*b {return(Ta);}
.|\n {return yytext[0];}
%%
Yacc file
%{
#include<stdio.h>
%}
%token Ta
%%
s: exp '\n' {printf("valid\n");}
|error {printf("invalid\n");}
;
exp: Ta
;
%%
main()
{
yyparse();
}
yyerror( )
{

}
Output
lex filename.l
yacc –d filename.y
cc y.tab.c lex.yy.c -lfl

BLDEACET/Lab Manual/ISE/10CSL58 13
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

./a.out

aaaab
Invalid
aaaaaaaaaab
Valid
aaaaaaaaaaaaaaaaaaaaaaab
valid

PART B
UNIX Programming:
Design, develop, and execute the following programs:

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

echo "number of arguments are: $#"


len=$#
while [ $len -ne 0 ]
do
eval echo \$$len
len=`expr $len - 1`
done
output:
sh filename.sh arg1 arg2 arg3
Number of arguments are 3
arg3
arg2
arg1

BLDEACET/Lab Manual/ISE/10CSL58 14
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

7b.c) 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>
#include<stdlib.h>
#include<sys/types.h>
int main()
{
char cmd[30];
pid_t pid;
int ch;
pid=fork();
if(pid==0)
{
do
{
printf("\n enter the command to be exe:");
scanf("%s",cmd);
system(cmd);
exit(0);
}
while(ch == 1);
}
wait();
}
Output
Cc filename.c
./a.out

Enter the command to be executed: date


Mon Feb 16 13:59:13 IST 2012

BLDEACET/Lab Manual/ISE/10CSL58 15
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

8a.sh) 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.

ls -l $1 | cut -d " " -f1 > file1


ls -l $2 | cut -d " " -f1 >file2
if cmp file1 file2
then
echo "both the files have same permission"
cat file1
else
echo "files have diff permission"
echo "permission of first file $1 is "
cat file1
echo "permission of second file $2 is"
cat file2
fi

output
Sh filename.sh file1 file2l ←
Both the files have same permission
-rw-r--r--

chmod 777 file2


sh filename.sh file1 file2
Both the files have different permission
The permission of the first file file1 is
-rw-r--r--
The permission of the second file file2 is
-rwxrwxrwx

BLDEACET/Lab Manual/ISE/10CSL58 16
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

8b.c) 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.

#include<sys/types.h>
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
int main()
{
int fd;
Char buf1[ ]=“department of is”;
Char buf2[ ]=“department of mech”;
fd=creat(“ise”,0622);
if(fd<0)
{
printf(“\n error in creating file”);
exit(0);
}
write(fd,buf1,16);
lseek(fd,48,SEEK_SET);
write(fd,buf2,16);
exit(0);
}
Output
cc filename.c
./a.out
od –c ise
0000000 D e p a r t m e n t O f C S
0000020 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0
*
0000060 D e p a r t m e n t O f I S
0000100

BLDEACET/Lab Manual/ISE/10CSL58 17
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

9a.sh) 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).

echo ‘#to unbundle sh filename’


for i in $*
do
echo “echo $i”
echo “cat>$i <<‘end of $i ’ ”
cat $i
echo “end of $i”
done

output
$cat > file1
This is the first file

$cat > file2


This is the second file

$ls
10b.c 4a.sh 5b.c 6b.c 8a.sh a file2
1b.c 5a.sh 6a.sh 7a.sh 9a.sh file1

$sh 4a.sh file1 file2 > new.sh

$ls
10b.c 4a.sh 5b.c 6b.c 8a.sh a file2
1b.c 5a.sh 6a.sh 7a.sh 9a.sh file1 new.sh

$rm file1
rm: remove regular file ‘file1’? y
$rm file2
rm: remove regular file ‘file2’? y

$chmod 777 new.sh

$./new.sh

BLDEACET/Lab Manual/ISE/10CSL58 18
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

file1
file2

$ls
10b.c 4a.sh 5b.c 6b.c 8a.sh a file2
1b.c 5a.sh 6a.sh 7a.sh 9a.sh file1 new.sh

$cat file1
This is the first file

$cat file2
This is the second file

BLDEACET/Lab Manual/ISE/10CSL58 19
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

9b.c) C program to do the following: Using fork( ) create a child process. The
child process prints its own process-id and id of its parent and then exits. The
parent process waits for its child to finish (by executing the wait( )) and prints its
own process-id and the id of its child process and then exits

#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
main()
{
int pid;
pid=fork();
if(pid<0)
printf("fork error");
if(pid==0)
{
printf("\n this is child process");
printf("\n child PID: %d ",getpid());
printf("\n parent PID: %d",getppid());
execlp("/bin/1s",NULL);
exit(0);
}
else
{
wait(NULL);
printf("\n this is parent process");
printf("\n child PID: %d ",pid);
printf("\n parent PID: %d",getpid());
exit(0);
}
}
Output:
Cc filename.c
./a.out
This is child process
Child PID: 3122
Parent PID: 3121

BLDEACET/Lab Manual/ISE/10CSL58 20
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

This is parent process


Parent PID: 3121
Child PID: 3122

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
cases,determine the average turn-around time. The input can be read from key
board or from a file.

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include <string.h>
typedef struct process
{
int arr_time, bur_time, tar_time;
} Proc;
Proc p[10];
int completed[10]={0};
int completion[10]={0};
int n;
int isCompleted()
{
int i;

for(i=0;i<n;i++)
{
if(completed[i]!=1)
return i;
}
return -1;
}

int getprocess()
{
int i,small;
if((small=isCompleted())==-1)

BLDEACET/Lab Manual/ISE/10CSL58 21
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

return -1;
for(i=0;i<n;i++)
{
if(completed[i]!=1 && completion[i]<completion[small])
small=i;
}
return small;
}

void srtf()
{
int exec,i,time,totalwaiting,totalturnaround;
printf("Enter the number of processes: ");
scanf("%d",&n);

for(i=0;i<n;i++)
{
printf("\n\nEnter burst time for process %d: ",i+1);
scanf("%d",&p[i].bur_time);
printf("Enter arrival time for process %d: ",i+1);
scanf("%d",&p[i].arr_time);
completion[i]=p[i].arr_time+p[i].bur_time;
}
exec=time=0;
for(i=1;i<n;i++)
{
if(completion[i]<completion[exec])
{
time=p[i].arr_time;
completion[exec]-=p[i].arr_time;
exec=i;
}
else
completion[i]=p[i].bur_time;
}
time+=p[exec].bur_time;
p[exec].tar_time=time;
completed[exec]=1;
while((exec=getprocess())!=-1)
{
time+=completion[exec];
p[exec].tar_time=time;

BLDEACET/Lab Manual/ISE/10CSL58 22
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

completed[exec]=1;
}
printf("\n\npid\tBTime\tAtime\tTAT\tWT");
totalwaiting=totalturnaround=0;
for(i=0;i<n;i++)
{
printf("\n%d\t%d\t%d\t%d\t%d",i+1,p[i].bur_time,p[i].arr_time,p[i].tar_time
,p[i].tar_time-p[i].arr_time-p[i].bur_time);
totalwaiting+=p[i].tar_time-p[i].arr_time-p[i].bur_time;
totalturnaround+=p[i].tar_time;
}
printf("\n\n Average waiting time= %f", ((float)totalwaiting/n));
printf("\n\n Average turn aound time= %f", ((float)totalturnaround/n));
}
void rr()
{
int s[10],p[10],n,i,j,w1=0,w[10],t[10], st[10],tq,tst=0;
int tt=0,tw=0;
float aw,at;
printf("enter no.of process");
scanf("%d",&n);
printf("\n enter time quanum");
scanf("%d",&tq);
printf("\n enter process&service time");
for(i=0;i<n;i++)
scanf("%d%d",&p[i],&s[i]);
for(i=0;i<n;i++)
{
st[i]=s[i];
tst=tst+s[i];
}
for(j=0;j<tst;j++)
for(i=0;i<n;i++)
{
if(s[i]>tq)
{
s[i]=s[i]-tq;
w1=w1+tq;
t[i]=w1;
w[i]=t[i]-st[i];
}
else if(s[i]!=0)

BLDEACET/Lab Manual/ISE/10CSL58 23
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

{
w1=w1+tq;
t[i]=w1;
w[i]=t[i]-st[i];
s[i]=s[i]-tq;
}
}
for(i=0;i<n;i++)
{
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
printf("process\tst\twt\ttt");
for(i=0;i<n;i++)
printf("\n%d\t%d\t%d\t%d",p[i],st[i],w[i],t[i]);
printf("\n\nawt=%f",aw);
printf("\n\natt=%f",at);
}
void main()
{

int ch=1;
clrscr();
do
{
// clrscr();
printf("1: SRTF 2: ROUND ROBIN 3: Exit\n Enter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: srtf(); break;
case 2: rr(); break;
case 3: break;
default: printf("Invalid input"); getch(); break;
}
}
while(ch!=3);

BLDEACET/Lab Manual/ISE/10CSL58 24
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

OUTPUT
[root@localhost ~]# cc srtf_rr.c
[root@localhost ~]# ./a.out
1: SRTF 2: ROUND ROBIN 3: Exit
Enter your Choice: 1
Enter the number of processes: 4
Enter burst time for process 1: 4
Enter arrival time for process 1: 0
Enter burst time for process 2: 3
Enter arrival time for process 2: 1
Enter burst time for process 3: 2
Enter arrival time for process 3: 2
Enter burst time for process 4: 1
Enter arrival time for process 4: 3
pid BTime Atime TAT WT
1 4 0 4 0
2 3 1 10 6
3 2 2 7 3
4 1 3 5 1
Average waiting time= 2.500000
Average turn aound time= 6.500000
1: SRTF 2: ROUND ROBIN 3: Exit
Enter your Choice: 2
enter no.of process:2
enter time quanum:2
enter process&service time 1 6
24
process st wt tt
1 6 4 10
2 4 4 8
awt=4.000000
att=9.000000
1: SRTF 2: ROUND ROBIN 3: Exit
Enter your Choice: 2
enter no of process 3
enter time quantum 2
enter process&service time
1 4
2 6
3 2

process st wt tt

BLDEACET/Lab Manual/ISE/10CSL58 25
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

1 4 4 8
2 6 6 12
3 2 4 6
awt = 4.000000
att = 8.000000

BLDEACET/Lab Manual/ISE/10CSL58 26
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

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<conio.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 random_num()
{
int temp;
temp = rand();
temp = temp%24;
MAX = temp;
return(MAX);
}

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


{

int FibNumber[25] = {0};


int j, temp,tmp,id,i = 0;
int n, tid, nthreads;

BLDEACET/Lab Manual/ISE/10CSL58 27
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

printf("Please Enter the number Range :");


scanf("%d",&n);
printf("\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] = Fibonacci(i);
printf("The number value is %d:",tmp);
for(i = 1; i <= tmp; i++)
printf("%d \t", FibNumber[i]);
printf("\n\n");
getch();
}
}
}
}

lease Enter the number Range : 7


The 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 89 144 233 377 610 987 1597 2584 4181
6765 10946 17711

The number value is 9:1 1 2 3 5 8 13 21 34

BLDEACET/Lab Manual/ISE/10CSL58 28
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

The number value is 19:1 1 2 3 5 8 13 21


34 55 89 144 233 377 610 987 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

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

BLDEACET/Lab Manual/ISE/10CSL58 29
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

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

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 10
struct process
{
char pid[4];
int maxneed[3];
int altd[3];
int need[3];
int finish;
}p[MAX];
int avl[3];
char safe[10][4];
void bankers(int);
void main()
{

int n,i;
clrscr();
printf("How Many processes? ");
scanf("%d",&n);
printf("Enter processes ");
for(i=0;i<n;i++)
{

printf("\nEnter pid :");


scanf("%s",&p[i].pid);
printf("Enter Maxneed :");

scanf("%d%d%d",&p[i].maxneed[0],&p[i].maxneed[1],&p[i].maxneed[2]);
printf("Enter Allocated :");
scanf("%d%d%d",&p[i].altd[0],&p[i].altd[1],&p[i].altd[2]);
p[i].finish=0;
}
printf("Enter Available Resource :");
scanf("%d%d%d",&avl[0],&avl[1],&avl[2]);

BLDEACET/Lab Manual/ISE/10CSL58 30
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

bankers(n);
getch();

void bankers(int n)
{
int i,k=0;
int j=0,m;
int count=0;
m=n*n;
for(i=0;i<n;i++)
{
p[i].need[0]=p[i].maxneed[0] - p[i].altd[0];
p[i].need[1]=p[i].maxneed[1] - p[i].altd[1];
p[i].need[2]=p[i].maxneed[2] - p[i].altd[2];

}
while(k<=m)
{
for(i=0;i<n;i++)
{
if(p[i].finish!=1 && p[i].need[0]<=avl[0] && p[i].need[1]<=avl[1] &&
p[i].need[2]<=avl[2])
{
strcpy(safe[j],p[i].pid);
j++;
count++;
p[i].finish=1;
avl[0]+=p[i].altd[0];
avl[1]+=p[i].altd[1];
avl[2]+=p[i].altd[2];
}
else
{
continue;
}
}
k++;

BLDEACET/Lab Manual/ISE/10CSL58 31
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

}
if(count==n)
{
printf("\nState is safe that is :\n");
for(i=0;i<n;i++)
{
printf(" %s",safe[i]);
}

}
else
{
printf("\n\t!Deadlock Occure");
}

OUTPUT
[root@localhost ~]# cc bankers.c
[root@localhost ~]# ./a.out
How Many processes? 5
Enter processes
Enter pid :p1
Enter Maxneed :7 5 3
Enter Allocated :0 1 0

Enter pid :p2


Enter Maxneed :3 2 2
Enter Allocated :2 0 0

Enter pid :p3


Enter Maxneed :9 0 2
Enter Allocated :3 0 2

BLDEACET/Lab Manual/ISE/10CSL58 32
B.L.D.E.A’s V.P. Dr.P.G.Halakatti College of Engineering & Technology,
Bijapur-586103

Enter pid :p4


Enter Maxneed :2 2 2
Enter Allocated :2 1 1

Enter pid :p5


Enter Maxneed :4 3 3
Enter Allocated :0 0 2
Enter Available Resource :3 3 2

State is safe that is :


p2 p4 p5 p1 p3

BLDEACET/Lab Manual/ISE/10CSL58 33

Vous aimerez peut-être aussi