You are on page 1of 31

SS & OS Lab Manual

Solution to LEX and YACC Problems:


Design, develop, and execute the ollo!ing programs using LEX:
"# a$ Program to count the number o characters, !ords, spaces and lines in a given input ile#
%{
/* 1a.l Lex Program to count Chars, Wors, S!aces, L"nes "n a g"#en "n!ut $le
*/
"nt %c & ', cc & ', lc & ', bc&'(
char "n$le)*+,(
%-
%or ). /t/n,0
eol /n
%%
{%or- {%c00( cc0&11leng(-
{eol- {lc00( cc00( -
) , {bc00( cc00( -
)/t, {bc0&2( cc00(-
. {cc00(-
%%
ma"n34
{
!r"nt536 7ea the 8n!ut 9"le :ame /n 64(
scan53;%s<,"n$le4(
11"n&5o!en3"n$le,6r64( /* O7 !r"nt536 7ea the =ext 3 Cntrl0> to
term"nate 4/n 64( %"thout $le */
11lex34(
5close311"n4(
!r"nt536 :o. o5 Chars? %/n :o. o5 Wors? %/n :o.o5 L"nes? %/n :o. o5 @lanAs?
%/n6,cc,%c,lc,bc4( -
%&
%{
"nt %c & ', cc & ', lc & ', bc&'(
char "n$le)*+,(
%-
%%
/n {lc00,cc00(-
). /t/n,0 {%c00,cc0&11leng(-
6 6 {bc00,cc00(-
. {cc00(-
%%
"nt ma"n34
S8B=, =CMDC7
Page 1
SS & OS Lab Manual
{
!r"nt536 7ea the 8n!ut 9"le :ame /n 64(
scan53;%s<,"n$le4(
11"n&5o!en3"n$le,6r64( /* O7 !r"nt536 7ea the =ext 3 Cntrl0> to
term"nate 4/n 64( %"thout $le */
11lex34(
5close311"n4(
!r"nt536 :o. o5 Chars? %/n :o. o5 Wors? %/n :o.o5 L"nes? %/n :o. o5 @lanAs?
%/n6,cc,%c,lc,bc4( -
-
b$ Program to count the numbers o comment lines in a given C program# Also eliminate them and cop' the
resulting Program into separate ile#
%{
/*1b.l Program to count the numbers o5 comment l"nes "n a g"#en C !rogram, an
Bl"m"nate them */
"nt cc&'(
%-
%x CM:=
%%
6/*6 {@BE8: CM:=( cc00(-
FCM:=G. (
FCM:=G/n (
FCM:=G6*/6 {@BE8: '( -
%%
ma"n3"nt argc, char *arg#),4
{
"53argc H&I 4
{ !r"nt536 Csage? %s Fsrc $leG Fst $leG/n6,arg#)',4(
return(
-
11"n&5o!en3arg#)1,,6r64(
11out&5o!en3arg#)*,,6%64(
11lex34(
!r"nt536 :o. o5 Comment L"nes? %/n6,cc4(
-
%&
%{
int cc=0;
%}
%x CMNTML CMNTSL
%%
"/*" {BEGIN CMNTML;cc++;}
<CMNTML>. ;
<CMNTML>\n ;
<CMNTML>"*/" {BEGIN 0;}
"//" {BEGIN CMNTSL;cc++;}
<CMNTSL>. ;
<CMNTSL>\n {BEGIN 0;}
%%
S8B=, =CMDC7
Page *
SS & OS Lab Manual
min!int "#c$c%" *"#&'()
{
i*!"#c+=,)
{
-"int*!"./#01%/</"c *i20><3/t *i20>\n"$"#&'0();
"0t."n;
}
44in=*5-0n!"#&'6($""");
445.t=*5-0n!"#&'7($"8");
4420x!);
-"int*!"N5.5* c5mm0nt 2in0/1%3\n"$cc);
}
(# a$ Program to recogni)e a valid arithmetic expression and to recogni)e the identiiers and operators present#
Print them separatel'#
%{
/**a.l Program to recogn"Je a #al" ar"thmet"c ex!ress"on an to recogn"Je
"ent"$ers an o!erators */
"nt a),&{',',','-,",#al"&1,o!n&',lb&',rb&',Kag&'(
%-
%x OPB7
%%
)aLJMLN'LO,0 { @BE8: OPB7( o!n00(-
FOPB7G606 { "53#al"4 { #al"&'("&'(- else ext34(-
FOPB7G6L6 { "53#al"4 { #al"&'("&1(- else ext34(-
FOPB7G6*6 { "53#al"4 { #al"&'("&*(- else ext34(-
FOPB7G6/6 { "53#al"4 { #al"&'("&I(- else ext34(-
FOPB7G)aLJMLN'LO,0 { o!n00( "53#al"&&'4 { #al"&1( a)",00(- else ext34(-
FOPB7G6/n6 { "53#al"&&'4 ext34( else return '(-
./n ext34(
%%
ext34
{ !r"nt536 8n#al" Bx!ress"on /n64( ex"t3'4( -
ma"n34
{
!r"nt536 =1!e the ar"thmet"c Bx!ress"on /n64(
11lex34(
"5333rbLlb4&&'4 && 3Kag&'44 return '(
!r"nt536 Pal" Mr"thmet"c Bx!ress"on /n64(
!r"nt536 :o. o5 O!erans/8ent"$ers ? % /n 6,o!n4(
!r"nt536 :o. o5 M"t"ons ? % /n :o. o5 Subtract"ons ? % /n6,a)',,a)1,4(
!r"nt536 :o. o5 Mult"!l"cat"ons ? % /n :o. o5 >"#"s"ons ? % /n6,a)*,,a)I,4(
-

b$ Program to recogni)e !hether a given sentence is simple or compound#
%{
S8B=, =CMDC7
Page I
SS & OS Lab Manual
/* Program to recognize whether a given sentence is simple or compound.*/
%}
8/ ' \n\t(+
%%
{8/}"n3"{8/} 9
{8/}":N;"{8/} 9
{8/}"5""{8/} 9
{8/}"<="{8/} 9
{8/}">.t"{8/} 9
{8/}"B?T"{8/} 9
{8/}">0c./0"{8/} 9
{8/}"n0&0"t%020/"{8/} {-"int*!"c5m-5.n3 /0nt0nc0\n"); 0xit!0);}
. ;
\n "0t."n 0;
%%
min!)
{
-"int*!"t4-0 t%0 /0nt0nc0\n");
4420x!);
-"int*!"/im-20 /0nt0nc0\n");
}
%&
%{
%}
%%
':('nN('3;(9'5<('"=(9'nN('5<('tT(9'>B('.?('tT( { -"int*!"c5m-5.n3 /0nt0nc0\n");
0xit!0);}
. ;
\n "0t."n 0;
%%
min!)
{
-"int*!"t4-0 t%0 /0nt0nc0\n");
4420x!);
-"int*!"/im-20 /0nt0nc0\n");
}
*# Program to recogni)e and count the number o identiiers in a given input ile#
%{
/* I.l Lex Program to 7ecogn"Je an count the 8ent"$ers "n a g"#en "n!ut $le 3 C
Program4 */
S8B=, =CMDC7
Page Q
SS & OS Lab Manual
"nt "c&'(
%-
WS ) /t/n,*
8> )RaLJMLN,)RaLJMLN'LO,*
>BCL: 6"nt6S6Koat6S6char6S6short6S6long6S6uns"gne6
%x >B9:
%%
{>BCL:- {@BE8: >B9:(-
F>B9:G{WS-{8>-{WS-/, "c00(
F>B9:G{WS-{8>-{WS-/( "c00(
F*G/n(
F*G. (
%%
ma"n3"nt argc, char **arg#4
{
"53argc&&*4
{
11"n&5o!en3arg#)1,,6r64(
11lex34(
!r"nt536 =otal :o. o5 8ent"$ers ? %/n6,"c4(
-
else
!r"nt536 Csage ? %s F$leG /n/n6,arg#)',4(
-
Design, develop, and execute the ollo!ing programs using YACC:
+# a$ Program to recogni)e a valid arithmetic expression that uses operators ,, -, . and /#
Lex Part
%{
/* Qa.l */
T"nclue 61.tab.h6
%-
%%
)'LO,03/.)'LO,04U { return :CM(-
)aLJMLN,)RaLJMLN'LO,* { return 8>( -
)/t, (
/n return '(
. return 11text)',(
%%
Yacc Part
%{
/* Qa.1 Vacc Program to checA the #al""t1 o5 an ar"thmet"c Bx!ress"on*/
T"nclueFst"o.hG
%-
%toAen :CM 8>
%le5t W0W WLW
S8B=, =CMDC7
Page +
SS & OS Lab Manual
%le5t W*W W/W
%%
e ? e W0W e
S e WLW e
S e W*W e
S e W/W e
S W3WeW4W
S :CM
S 8> (
%%
ma"n34
{
!r"nt536 =1!e the Bx!ress"on & Press Bnter Ae1/n64(
11!arse34(
!r"nt536 Pal" Bx!ress"on /n64(
-
11error34
{
!r"nt536 8n#al" Bx!res"onHHHH/n64(
ex"t3'4(
-

b$ Program to recogni)e a valid variable, !hich starts !ith a letter, ollo!ed b' an' number o letters or
digits#
Lex part
%{
/* Qb.l */
T"nclue 61.tab.h6
%-
%%
)aLJ, return L(
)'LO, return >(
S8B=, =CMDC7
Page X
To Run the above program:
$lex 1b.l &G generates lex.11.c $le
$yacc d 1b.y &G generates 1.tab.c an 1.tab.h3$le o5 toAen
e$n"t"ons4
$cc lex.yy.c y.tab.c ly ll
$a.out &G to run O7 1ou can create 1our o%n executable $le
SS & OS Lab Manual
%%
Yacc part
%{
/* Qb.1 Vacc Program to recogn"Je a #ar"able 3Letter 5ollo%e b1 an1 number o5
Letters or >"g"ts 4
%-
%toAen L >
%%
#ar ? L B { !r"nt536 Pal" Par"able /n64( return '(-
B? B L (
SB > (
S (
%%
ma"n34
{ !r"nt536 =1!e the Par"able /n64( 11!arse34( -
11error34
{ !r"nt536 8n#al" #ar"able HHH/n64( ex"t3'4( -
0# a$ Program to evaluate an arithmetic expression involving operators ,, -, . and /#
Lex Part
%{
/* +a.l */
T"nclueFstl"b.hG
T"nclue 61.tab.h6
extern "nt 11l#al(
%-
%%
)'LO,0 { 11l#al&ato"311text4( return :CM( -
)/t, (
/n return '(
. return 11text)',(
%%
Yacc Part
%{
T"nclue Fst"o.hG
/* +a.1 Vacc Program to e#aluate a #al" ar"thmet"c Bx!ress"on*/
%-
%toAen :CM
%le5t W0W WLW
%le5t W*W W/W
%%
ex!r ? e { !r"nt536 7esult ? %/n6,Y14( return '(-(
e ? e W0W e {YY&Y10YI(-
S e WLW e {YY&Y1LYI(-
S e W*W e {YY&Y1*YI(-
S8B=, =CMDC7
Page Z
SS & OS Lab Manual
S e W/W e {YY&Y1/YI(-
S W3WeW4W {YY&Y*(-
S :CM {YY&Y1(-(
%%
ma"n34
{
!r"nt536 =1!e the Bx!ress"on & Press Bnter Ae1/n64( 11!arse34(
!r"nt536 Pal" Bx!ress"on /n64(
-
11error34
{ !r"nt536 8n#al" Bx!res"onHHHH/n64( ex"t3'4( -
b$ Program to recogni)e strings 1aaab2, 1abbb2, 1ab2 and 1a2 using the grammar 3a
n
b
n
, n45 6$#
Lex Part
%{
/* +b.l*/
T"nclue 61.tab.h6
%-
%%
a return M(
b return @(
. return 11text)',(
/n return 11text)',(
%%
Yacc part
/* b.y Program !or the grammar "
n
#
n
$here n%& */
%toAen M @
%%
str? s W/nW { return '(-
s ? M s @ ( S (
%%
ma"n34
{
!r"nt536 =1!e the Str"ng U/n64(
"53H11!arse344
!r"nt536 Pal" Str"ng/n 64( -
"nt 11error34
{
!r"nt536 8n#al" Str"ng./n64( ex"t3'4(
-
7# Program to recogni)e the grammar 3a
n
b, n45 "6$#
Lex Part
%{
/* X.l */
T"nclue 61.tab.h6
%-
S8B=, =CMDC7
Page 2
'rammar: !or a
n
b
n
n
[ '
S\ a S b S
(or a
m
b
n
n ,m [ '
S \a S b S aS S Sb S Sa Sb
SS & OS Lab Manual
%%
a return M(
b return @(
. return 11text)',(
/n return 11text)',(
%%
Yacc part
%{
/* X.1 Vacc !rogram to recogn"Je the grammar M
n
@ %here n [ 1' */
%-
%toAen M @
%%
str? s W/nW { return '(-
s ? x @ (
x ? M M M M M M M M M M = (
=? = M
S (
%%
ma"n34
{
!r"nt536 =1!e the Str"ng U /n64(
"53H11!arse344
!r"nt536 Pal" Str"ng/n 64(
-
"nt 11error34
{
!r"nt536 8n#al" Str"ng./n64(
ex"t3'4(
S-
P"RT #
)*+, PR-'R"..+*'
/0a. *on1recur23ve 2hell 2cr3pt that accept2 any number o! argument2 and pr3nt2
them 3n the Rever2e order. 4(or example5 3! the 2cr3pt 32 named argument25 then
execut3ng argument2 " # 6 2hould produce 6 # " on the 2tandard output0.
TH /b"n/sh
T Program to re#erse the "n!ut arguments
"5 test YT Le] '
then
echo 6:ot enough arguments to be "s!la1e6
else
echo 6number o5 arguments are?YT6
echo 6the arguments !asse "s Y*6
S8B=, =CMDC7
Page O
SS & OS Lab Manual
echo 6the re#erse o5 "t "s6
c&YT
%h"le ) Yc Lne ' ,
o
e#al echo /YYc
c&^ex!r Yc L 1^
one
echo 6Bn o5 the !rogram6
$
ex"t '
or
TH /b"n/sh
T!rogram to re#erse comman l"ne arguments
"5 test YT Le] '
then
echo 6:ot enough arguments to be "s!la1e6
else
5or actstr "n Y*
o
re#&^echo Yactstr Yre#^
one
echo Yre#
$
ex"t '
-)TP)T
)root_localhost,T sh Za1.sh M @ C
number o5 arguments are?I
the arguments !asse "s M @ C
the re#erse o5 "t "s
C
@
M
Bn o5 the !rogram
/0 b. 6 program that create2 a ch3ld proce22 to read command2 !rom the
2tandard 3nput and execute them4a m3n3mal 3mplementat3on o! a 2hell 1l37e
program0. You can a22ume that no argument2 $3ll be pa22ed to the command2
to be executed.
S8B=, =CMDC7
Page 1'
SS & OS Lab Manual
T"nclueFst"o.hG
T"nclueFun"st.hG
"nt ma"n34
{
"nt x,ch(
char cm)*',(
"nt !"&5orA34(
"53!"&&'4 /* ch"l !rocess execut"on */
{
!r"nt536/n Ch"l !rocess64(
o
{
!r"nt536/n Bnter the comman?64(
scan536%s6,&cm4(
s1stem3cm4(
!r"nt536/n Bnter 1 to cont"nue or ' to ex"t?64(
scan536%6,&ch4(
-%h"le3chH&'4(
-
else/* !arent execut"on*/
%a"t34( /* @locA the !arent unt"l the com!let"on o5 the ch"l*/
-
-)TP)T
)root_localhost,T cc Zb.c
)root_localhost,T ./a.out
Ch"l !rocess
Bnter the comman?ate
=hu Ma1 'Z 1I?1Z?I* 8S= *''O
Bnter 1 to cont"nue or ' to ex"t?1
Bnter the comman?ls
1a.sh *a.sh Ia.sh Qa.sh +a.sh Xa.sh Za.sh 1b.c *b.c Ib.c Qb.c +b.c Xb.c Zb.c
a.out cc
Bnter 1 to cont"nue or ' to ex"t?1
Bnter the comman?%ho
root ?' Se! 1' 1*?+Q
root !ts/1 Se! 1' 1*?++ 3?'.'4
S8B=, =CMDC7
Page 11
SS & OS Lab Manual
Bnter 1 to cont"nue or ' to ex"t?1
Bnter the comman?!s
P8> ==V =8MB CM>
IX*' !ts/1 ''?''?'' bash
IZ'+ !ts/1 ''?''?'' a.out
IZ'X !ts/1 ''?''?'' a.out
IZ11 !ts/1 ''?''?'' !s
Bnter 1 to cont"nue or ' to ex"t?1
Bnter the comman?!%
/root/hema/un"x
Bnter 1 to cont"nue or ' to ex"t?'
)root_localhost,T
80a. 9hell 2cr3pt that accept2 t$o :lename2 a2 argument25 chec72 3! the
perm3223on2 !or the2e :le2 are 3dent3cal and 3! the perm3223on2 are 3dent3cal5
output2 the common perm3223on25 other$32e output2 each :lename !ollo$ed by
3t2 perm3223on2.
TH /b"n/sh
T!rogram to checA $le !erm"ss"on
"5 ) YT Lne * ,
then
echo ;execut"on "s sh *a.sh $rstR$le seconR$le<
ex"t *
$
T!erm"ss"on o5 $rst $le
!1&^ls Ll Y1 S cut L 6 6 L5 1^
T!erm"ss"on o5 secon $le
!*&^ls Ll Y*S cut L 6 6 L5 1^
"5 test Y!1 & Y!*
then
echo 6=he !erm"ss"ons are same6
echo Y!1
else
echo 6=he !erm"ss"ons are "`erent6
echo 69"lename? Y1 !erm"ss"on? Y!1 6
echo 69"lename? Y* !erm"ss"on? Y!* 6
S8B=, =CMDC7
Page 1*
SS & OS Lab Manual
$
ex"t '
-)TP)T
)root_localhost,T sh 2a.sh aaa tem
=he !erm"ss"ons are "`erent
9"lename? aaa !erm"ss"on? r%xrLxrLx
9"lename? tem !erm"ss"on? Lr%LrLLrLL
)root_localhost,T sh 2a.sh tem tem!
=he !erm"ss"ons are same
Lr%Lrara
80 b. 6 program to create a :le $3th 1; byte2 o! arb3trary data !rom the
beg3nn3ng and another 1; byte2 o! arb3trary data !rom an o<2et o! =8. >32play
the :le content2 to demon2trate ho$ the hole 3n :le 32 handled.
T"nclueFst"o.hG
T"nclueFstl"b.hG
T"nclueF5cntl.hG
T"nclueFs1s/stat.hG
T"nclueFs1s/t1!es.hG
// !rogram to create a $le %"th 1X b1tes o5 arb"trar1 ata 5rom
// beg"nn"ng an another 1X b1te at the en
"nt ma"n34
{
char bu51),&6abce5gh"bAlmno!6(
char bu5*),&6M@C>B9Ec8dDLM:OP6(
"nt 5(
// creat"ng $le ne%.at
5&o!en36ne%.at6,ORW7O:LVSORC7BM=,'ZZZ4(
//%r"t"ng 1X b1tes o5 ata
%r"te35,&bu51,1X4(
// o`set o5 Q2
lseeA35,Q2,SBBDRSB=4(
// %r"t"ng 1X b1tes o5 ata
%r"te35,&bu5*,1X4(
close354(
S8B=, =CMDC7
Page 1I
SS & OS Lab Manual
!r"nt536contens o5 $le are/n64(
s1stem36o Lc ne%.at64(
ex"t3'4(
-
-)TP)T
)root_localhost,T cc 2b.c
)root_localhost,T ./a.out
''''''' a b c e 5 g h " b A l m n o !
'''''*' /' /' /' /' /' /' /' /' /' /' /' /' /' /' /' /'
*
'''''X' M @ C > B 9 E c 8 d D L M : O P
''''1''
?0a. 9hell 2cr3pt that accept2 :le name2 2pec3:ed a2 argument2 and create2 a
2hell 2cr3pt that conta3n2 th32 :le a2 $ell a2 the code to recreate the2e :le2.
Thu2 3! the 2cr3pt generated by your 2cr3pt 32 executed5 3t $ould recreate the
or3g3nal :le24Th32 32 2ame a2 the @bundleA 2cr3pt de2cr3bed by #ra3n
B.Cern3ghan and Rob P37e 3n @The )n3x Programm3ng Dnv3ronmentA 5 Prent3ce
hall +nd3a0.
TH /b"n/sh
T!rogram to create a bunle scr"!t
"5 ) YT Le] ' ,
then
echo 6no arguments !asse6
ex"t 1
$
echo 6 6Gcr.sh
5or " "n Y*
o
echo 6echo 6coe to recreate the $le Y"66GGcr.sh
echo 6cat Y"FFen56GGcr.sh
cat Y"GG cr.sh
echo 6en56GGcr.sh
echo 6echo 6creat"on one66GGcr.sh
one
ex"t '
-)TP)T
)root_localhost,T cat G51FFen5
7:S8=
en5
)root_localhost,T cat G5*FFen5
S8B=, =CMDC7
Page 1Q
SS & OS Lab Manual
@M:EMLO7B
en5
)root_localhost,T sh Oa.sh 51 5*
)root_localhost,T #" cs.sh
echo coe to recreate the $le 51
cat 51FFen5
7:S8=
en5
echo creat"on one
echo coe to recreate the $le 5*
cat 5*FFen5
@M:EMLO7B
en5
echo creat"on one
?0 b. 6 program to do the !ollo$3ng: )23ng !or740 create a ch3ld parent and then
ex3t2. the parent proce22 $a3t2 !or 3t2 ch3ld to :n32h4by execut3ng the $a3t400
and pr3nt2 3t2 o$n proce2213d and the 3d o! 3t2 ch3ld proce22 and then ex3t2.
T"nclueFstl"b.hG
T"nclueFst"o.hG
T"nclueFun"st.hG
"nt ma"n34
{
"nt ch"l(
ch"l&5orA34(
"53ch"l&&'4 /*ch"l !rocess execut"on*/
{
!r"nt536/n Ch"l !rocess64(
!r"nt536/n M1 P8>&%6,get!"344(
!r"nt536/n M1 !arent P8>&%6,get!!"344(
ex"t3'4(
-
else
{ /* !arent !rocess execut"on*/
%a"t34(
!r"nt536/n/n Parent !rocess64(
!r"nt536/n m1 P8>&%6,get!"344(
!r"nt536/n M1 Ch"l P8>&%/n6,ch"l4(
-
-
S8B=, =CMDC7
Page 1+
SS & OS Lab Manual
-)TP)T
)root_localhost,T cc Ob.c
)root_localhost,T ./a.out
Ch"l !rocess
M1 P8>&IO'*
M1 !arent P8>&IO'1
Parent !rocess
m1 P8>&IO'1
M1 Ch"l P8>&IO'*
Operating System Programs
"6: Design, develop and execute a program in C/C,, to simulate the !or8ing o Shortest
&emaining time and &ound &obin Scheduling algorithms# Experiment !ith dierent 9uantum
si)es or the &ound &obin algorithm# :n all cases, determine the average turn Around time# :nput
can be read rom 8e'board or rom a ile#
#include<stdio.h>
struct proc
{
int id;
int arrival;
int burst;
int rem;
int wait;
int finish;
int turnaround;
float ratio;
process!"#$; //structure to hold the process information
struct proc temp;
int no;
int ch%process&int';
int ne(tprocess&';
void roundrobin&int) int) int!$) int!$';
void srtf&int';
main&'
{
int n)t*)choice;
int bt!"#$)st!"#$)i)+)%;
for&; ;'
{
printf&,-nter the choice .n,';
S8B=, =CMDC7
Page 1X
SS & OS Lab Manual
printf&, ". /ound /obin.n 0. 1/2.n 3. -(it .n,';
scanf&,4d,)5choice';
switch&choice'
{
case "6
printf&,/ound /obin scheduling algorithm.n,';
printf&,-nter number of processes6.n,';
scanf&,4d,)5n';
printf&,-nter burst time for se*uences6,';
for&i7#;i<n;i88'
{
scanf&,4d,)5bt!i$';
st!i$7bt!i$; //service time

printf&,-nter time *uantum6,';
scanf&,4d,)5t*';
roundrobin&n)t*)st)bt';
brea%;
case 06
printf&,.n .n 9991:;/2-12 /-<=>?>?@ 2><- ?-A2999.n .n ,';
printf&,.n .n -nter the number of processes6 ,';
scanf&,4d,) 5n';
srtf&n';
brea%;
case 36 e(it&#';
// end of switch
// end of for
//end of main&'
void roundrobin&int n)int t*)int st!$)int bt!$'
{
int time7#;
int tat!"#$)wt!"#$)i)count7#)swt7#)stat7#)temp")s*7#)+)%;
float awt7#.#)atat7#.#;
while&"'
{
for&i7#)count7#;i<n;i88'
{
temp"7t*;
if&st!i$77#' // when service time of a process e*uals zero then
//count value is incremented
{
count88;
continue;
S8B=, =CMDC7
Page 1Z
SS & OS Lab Manual

if&st!i$>t*' // when service time of a process greater than time
//*uantum then time
st!i$7st!i$9t*; //*uantum value subtracted from service time
else
if&st!i$>7#'
{
temp"7st!i$; // temp" stores the service time of a process
st!i$7#; // ma%ing service time e*uals #

s*7s*8temp"; // utilizing temp" value to calculate turnaround time


tat!i$7s*; // turn around time
//end of for
if&n77count' // it indicates all processes have completed their tas% because the count
value
brea%; // incremented when service time e*uals #
//end of while
for&i7#;i<n;i88' // to calculate the wait time and turnaround time of each process
{
wt!i$7tat!i$9bt!i$; // waiting time calculated from the turnaround time 9 burst time
swt7swt8wt!i$; // summation of wait time
stat7stat8tat!i$; // summation of turnaround time

awt7&float'swt/n; // average wait time
atat7&float'stat/n; // average turnaround time
printf&,ProcessBno Curst time Dait time 2urn around time.n,';
for&i7#;i<n;i88'
printf&,4d.t.t4d.t.t4d.t.t4d.n,)i8")bt!i$)wt!i$)tat!i$';
printf&,=vg wait time is 4f.n =vg turn around time is 4f.n,)awt)atat';
// end of /ound /obin
int ch%process&int s' // function to chec% process remaining time is zero or not
{
int i;
for&i 7 "; i <7 s; i88'
{
if&process!i$.rem E7 #'
return ";

return #;
// end of ch%process

int ne(tprocess&' // function to identifF the ne(t process to be e(ecuted
{
S8B=, =CMDC7
Page 12
SS & OS Lab Manual
int min) l) i;
min 7 30###; //anF limit assumed
for&i 7 "; i <7 no; i88'
{
if& process!i$.remE7# 55 process!i$.rem < min'
{
min 7 process!i$.rem;
l 7 i;

return l;
// end of ne(tprocess
void srtf&int n'
{
int i)+)%)time7#;
float tavg)wavg;
for&i 7 "; i <7 n; i88'
{
process!i$.id 7 i;
printf&,.n.n-nter the arrival time for process 4d6 ,) i';
scanf&,4d,) 5&process!i$.arrival'';
printf&,-nter the burst time for process 4d6 ,) i';
scanf&,4d,) 5&process!i$.burst'';
process!i$.rem 7 process!i$.burst;

for&i 7 "; i <7 n; i88'
{
for&+ 7 i 8 "; + <7 n; +88'
{
if&process!i$.arrival > process!+$.arrival' // sort arrival time of a process
{
temp 7 process!i$;
process!i$ 7 process!+$;
process!+$ 7 temp;

no 7 #;
+ 7 ";
while&ch%process&n' 77 "'
{
if&process!no 8 "$.arrival 77 time'
{
no88;
S8B=, =CMDC7
Page 1O
SS & OS Lab Manual
if&process!+$.rem77#'
process!+$.finish7time;
+ 7 ne(tprocess&';

if&process!+$.rem E7 #' // to calculate the waiting time of a process


{
process!+$.rem99;
for&i 7 "; i <7 no; i88'
{
if&i E7 + 55 process!i$.rem E7 #'
process!i$.wait88;

else
{
process!+$.finish 7 time;
+7ne(tprocess&';
time99;
%7+;

time88;

process!%$.finish 7 time;
printf&,.n.n.t.t.t9991:;/2-12 /-<=>?>?@ 2><- G>/12999,';
printf&,.n.n Process =rrival Curst Daiting Ginishing turnaround 2r/2b .n,';
printf&,4Hs 4Is 4Js 4"#s 4Ks 4Is.n.n,) ,id,) ,time,) ,time,) ,time,) ,time,) ,time,';
for&i 7 "; i <7 n; i88'
{
process!i$.turnaround 7 process!i$.wait 8 process!i$.burst; // calc of turnaround
process!i$.ratio 7 &float'process!i$.turnaround / &float'process!i$.burst;
printf&,4Hd 4Kd 4Jd 4Kd 4"#d 4Id 4"#."f ,) process!i$.id) process!i$.arrival)
process!i$.burst) process!i$.wait) process!i$.finish) process!i$.turnaround)
process!i$.ratio';
tavg7tavg8 process!i$.turnaround; //summation of turnaround time
wavg7wavg8process!i$.wait; // summation of waiting time
printf&,.n.n,';

tavg7tavg/n; // average turnaround time


wavg7wavg/n; // average wait time
printf&,tavg74f.t wavg74f.n,)tavg)wavg';
// end of srtf
S8B=, =CMDC7
Page *'
SS & OS Lab Manual
;L2PL26
-nter the choice
"' /ound /obin 0' 1/2
3' -(it
"
/ound /obin scheduling algorithm
**********************************
-nter number of processes63
-nter burst time for se*uences60M
3
3
-nter time *uantum6M
ProcessBno Curst time Dait time 2urnaround time
" 0M N 3#
0 3 M J
3 3 J "#
=vg wait time is H.NNNNNJ
=vg turnaround time is "H.NNNNNJ
-nter the choice
"' /ound /obin 0' 1/2
3' -(it
0
9991:;/2-12 /-<=>?>?@ 2><- ?-A2999
-nter the number of processes6 M
-nter the arrival time for process "6 #
-nter the burst time for process "6 K
-nter the arrival time for process 06 "
-nter the burst time for process 06 M
-nter the arrival time for process 36 0
-nter the burst time for process 36 I
-nter the arrival time for process M6 3
-nter the burst time for process M6 H
" 0M N 3#
0 3 M J
3 3 J "#
9991:;/2-12 /-<=>?>?@ 2><- G>/12999
-nter the number of processes6 M
-nter the arrival time for process "6 #
-nter the burst time for process "6 K
-nter the arrival time for process 06 "
-nter the burst time for process 06 M
-nter the arrival time for process 36 0
S8B=, =CMDC7
Page *1
SS & OS Lab Manual
-nter the burst time for process 36 I
-nter the arrival time for process M6 3
-nter the burst time for process M6 H
9991:;/2-12 /-<=>?>?@ 2><- ?-A2999
Process =rrival Curst Daiting Ginishing turnaround 2r/2b
id time time time time time time
" # K I "J "J 0."
0 " M # H M ".#
3 0 I "H 0N 0M 0.J
M 3 H 0 "# J ".M
tavg7"3.######
wavg7N.H#####
Lsing ;pen<P
"": Design develop and run a multi-threaded program to generate and print ;ibonacci series#
%ne thread has to generate the numbers up to the speciied limit and Another thread has to print
them# Ensure proper s'nchroni)ation#
# include<stdio.h>
# include<omp.h>
# include<stdlib.h>
int <=A;
int Gibonacci&int n'
{
int () F;
if &n < 0'
return n;
else
{
( 7 Gibonacci&n 9 "';
F 7 Gibonacci&n 9 0';
return &( 8 F';

int Gibonacci2as%&int n'


{
int () F;
if &n < 0'
return n;
else
{
S8B=, =CMDC7
Page **
SS & OS Lab Manual
( 7 Gibonacci&n 9 "';
F 7 Gibonacci&n 9 0';
return &( 8 F';

/* random number generation upto 0M */


int randomBnum&'
{
int temp;
temp 7 rand&';
temp 7 temp40M;
<=A 7 temp;
return&<=A';

int main&int argc) char * argv!$'


{

int Gib?umber!0H$ 7 {#;
int +) temp)tmp)id)i 7 #;
int n) tid) nthreads;
printf&,Please -nter the number /ange 6,';
scanf&,4d,)5n';
printf&,.n,';
ompBsetBnumBthreads&0';
//Parallel region
# pragma omp parallel
{
printf&,2he number of threads are 4d.n,)ompBgetBnumBthreads&'';
# pragma omp for private &tid) tmp) Gib?umber'
for&+ 7 "; +<7n; +88'
{
tmp 7 randomBnum&';
/* @et thread number */
/* tid 7 ompBgetBthreadBnum&';
printf&,2he number of threads are 4d.n,)ompBgetBnumBthreads&'';
printf&,2he thread id is 7 4d.n,) tid'; */
/* 2he critical section here will enable) not more then one
thread to e(ecute in this section &sFnchronization' */
# pragma omp critical
{
/* @et thread number */
/* tid 7 ompBgetBthreadBnum&';
printf&,********************* inside critical section
S8B=, =CMDC7
Page *I
SS & OS Lab Manual
******************.n,';
printf&,2he thread id is 7 4d.n,) tid'; */
for&i 7 "; i <7 tmp; i88'
Gib?umber!i$ 7 Gibonacci2as%&i';
printf&,2he number value is 4d6,)tmp';
for&i 7 "; i <7 tmp; i88'
printf&,4d .t,) Gib?umber!i$';
printf&,.n.n,';

lease -nter the number /ange 6 J


2he number value is J6" " 0 3 H K "3
2he number value is 006" " 0 3 H K "3 0" 3M HH KI
"MM 033 3JJ N"# IKJ "HIJ 0HKM M"K" NJNH "#IMN "JJ""
2he number value is I6" " 0 3 H K "3 0" 3M
2he number value is "I6" " 0 3 H K "3 0" 3M HH KI
"MM 033 3JJ N"# IKJ "HIJ 0HKM M"K"
2he number value is "J6" " 0 3 H K "3 0" 3M HH KI
"MM 033 3JJ N"# IKJ "HIJ
2he number value is J6" " 0 3 H K "3
2he number value is "#6" " 0 3 H K "3 0" 3M HH
"(: Design, develop and run a program to implement the <an8er2s Algorithm# Demonstrate its =or8ing
!ith dierent data values#
#include<stdio.h>
struct process
{
int all!N$)ma(!N$)need!N$)finished)re*uest!N$;
S8B=, =CMDC7
Page *Q
SS & OS Lab Manual
p!"#$;
int avail!N$)sse*!"#$)ss7#)chec%"7#)chec%07#)n)pid)wor%!N$;
int nor)nori;
int main&'
{
int safese*&void';
int ch)i7#)+7#)%)pid)ch";
int violationchec%7#)waitchec%7#;
do
{
printf&,.n.n.t ". >nput,';
printf&,.n.n.t 0. ?ew /e*uest,';
printf&,.n.n.t 3. 1afe 1tate or ?ot,';
printf&,.n.n.t M. print,';
printf&,.n.n.t H. -(it,';
printf&,.n.n.t -nter ur choice 6,';
scanf&,4d,)5ch';
switch&ch'
{
case "6
printf&,.n.n.t -nter number of processes 6 ,';
scanf&,4d,)5n';
printf&,.n.n.t -nter the ?umber of /esources 6 ,';
scanf&,4d,)5nor';
printf&,.n.n.t -nter the =vailable /esouces 6 ,';
for&+7#;+<n;+88'
{
for&%7#;%<nor;%88'
{
if&+77#'
{
printf&,.n.n.t Gor /esource tFpe 4d 6 ,)%';
scanf&,4d,)5avail!%$';

p!+$.ma(!%$7#;
p!+$.all!%$7#;
p!+$.need!%$7#;
p!+$.finished7#;
p!+$.re*uest!%$7#;

for&i7#;i<n;i88'
{
printf&,.n.n.t -nter <a( and =llocated resources for P 4d 6 ,)i';
for&+7#;+<nor;+88'
{
printf&,.n.n.t -nter the <a( of resource 4d 6 ,)+';
scanf&,4d,)5p!i$.ma(!+$';
printf&,.n.n.t =llocation of resource 4d 6,)+';
scanf&,4d,)5p!i$.all!+$';
if&p!i$.all!+$>p!i$.ma(!+$'
S8B=, =CMDC7
Page *+
SS & OS Lab Manual
{
printf&,.n.n.t =llocation should be less < or 77
ma(,';
+99;

else
p!i$.need!+$7p!i$.ma(!+$9p!i$.all!+$;
avail!+$7avail!+$9p!i$.all!+$;

brea%;
case 06
violationchec%7#;
waitchec%7#;
printf&,.n.n.t /e*uesting process id 6,';
scanf&,4d,)5pid';
for&+7#;+<nor;+88'
{
printf&,.n.n.t ?umber of /e*uest for resource 4d 6,)+';
scanf&,4d,)5p!pid$.re*uest!+$';
if&p!pid$.re*uest!+$>p!pid$.need!+$'
violationchec%7";
if&p!pid$.re*uest!+$>avail!+$'
waitchec%7";

if &violationchec%77"'
printf&,.n.n.t 2he Process -(ceeds itOs <a( ?eed6 2erminated,';
else if&waitchec%77"'
printf&,.n.n.t Pac% of /esourcess 6 Process 1tate Q Dait,';
else
{
for&+7#;+<nor;+88'
{
avail!+$7avail!+$9p!pid$.re*uest!+$;
p!pid$.all!+$7p!pid$.all!+$8p!pid$.re*uest!+$;
p!pid$.need!+$7p!pid$.need!+$9p!pid$.re*uest!+$;

ch"7safese*&';
if&ch"77#'
{
for&+7#;+<nor;+88'
{
avail!+$7avail!+$8p!pid$.re*uest!+$;
p!pid$.all!+$7p!pid$.all!+$9p!pid$.re*uest!+$;
p!pid$.need!+$7p!pid$.need!+$8p!pid$.re*uest!+$;

else if&ch"77"'
printf&,.n.n.t /e*uest Rommitted ,';

brea%;
case 36
if&safese*&'77"'
S8B=, =CMDC7
Page *X
SS & OS Lab Manual
printf&,.n.n.t 2he 1Fstem is in safe state ,';
else
printf&,.n.n.t 2he 1Fstem is ?ot in safe state ,';
brea%;
case M6
printf&,.n.n.t ?umber of processes 6 4d,)n';
printf&,.n.n.t ?umber of /esources 6 4d,)nor';
printf&,.n.n.t Pid .t <a( .t =llocated .t ?eed ,';
for&i7#;i<n;i88'
{
printf&,.n.n.t P4d 6 ,)i';
for&+7#;+<nor;+88'
printf&, 4d,)p!i$.ma(!+$';
printf&,.t,';
for&+7#;+<nor;+88'
printf&,4d,)p!i$.all!+$';
printf&,.t,';
for&+7#;+<nor;+88'
printf&,4d,)p!i$.need!+$';

printf&,.n.n.t =vailable 6,';


for&i7#;i<nor;i88'
printf&,4d,)avail!i$';
brea%;
case H6
brea%;

//getch&';
while&chE7H';

int safese*&'
{
int t+)t%)i)+)%;
ss7#;
for&+7#;+<nor;+88'
wor%!+$7avail!+$;
for&+7#;+<n;+88'
p!+$.finished7#;
for& t%7#;t%<nor;t%88'
{
for&+7#;+<n;+88'
{
if&p!+$.finished77#'
{
chec%"7#;
for&%7#;%<nor;%88'
if&p!+$.need!%$<7wor%!%$'
chec%"88;
if&chec%"77nor'
{
for&%7#;%<nor;%88'
{
S8B=, =CMDC7
Page *Z
SS & OS Lab Manual
wor%!%$7wor%!%$8p!+$.all!%$;
p!+$.finished7";

sse*!ss$7+;
ss88;

chec%07#;
for&i7#;i<n;i88'
if&p!i$.finished77"'
chec%088;
printf&,.n.n.t,';
if&chec%0>7n'
{
printf&,.n.n.t 2he sFstem is in safe state,';
for& t+7#;t+<n;t+88'
printf&,P4d),)sse*!t+$';
return ";

else
printf&,.n.n.t 2he sFstem is ?ot in safe state,';
return #;

S ./os"0
". >nput
0. ?ew /e*uest
3. 1afe 1tate or ?ot
M. print
H. -(it
-nter ur choice 6"
-nter number of processes 6 H
-nter the ?umber of /esources 6 3
-nter the =vailable /esouces 6
Gor /esource tFpe # 6 "#
Gor /esource tFpe " 6 H
Gor /esource tFpe 0 6 J
-nter <a( and =llocated resources for P # 6
-nter the <a( of resource # 6 J
=llocation of resource # 6#
-nter the <a( of resource " 6 H
=llocation of resource " 6"
-nter the <a( of resource 0 6 3
S8B=, =CMDC7
Page *2
SS & OS Lab Manual
=llocation of resource 0 6#
-nter <a( and =llocated resources for P " 6
-nter the <a( of resource # 6 3
=llocation of resource # 60
-nter the <a( of resource " 6 0
=llocation of resource " 6#
-nter the <a( of resource 0 6 0
=llocation of resource 0 6#
-nter <a( and =llocated resources for P 0 6
-nter the <a( of resource # 6 I
=llocation of resource # 63
-nter the <a( of resource " 6 #
=llocation of resource " 6#
-nter the <a( of resource 0 6 0
=llocation of resource 0 60
-nter <a( and =llocated resources for P 3 6
-nter the <a( of resource # 6 0
=llocation of resource # 60
-nter the <a( of resource " 6 0
=llocation of resource " 6"
-nter the <a( of resource 0 6 0
=llocation of resource 0 6"
-nter <a( and =llocated resources for P M 6
-nter the <a( of resource # 6 M
=llocation of resource # 6#
-nter the <a( of resource " 6 3
S8B=, =CMDC7
Page *O
SS & OS Lab Manual
=llocation of resource " 6#
-nter the <a( of resource 0 6 3
=llocation of resource 0 60
". >nput
0. ?ew /e*uest
3. 1afe 1tate or ?ot
M. print
H. -(it
-nter ur choice 63
2he sFstem is in safe stateP")P3)PM)P#)P0)
2he 1Fstem is in safe state
". >nput
0. ?ew /e*uest
3. 1afe 1tate or ?ot
M. print
H. -(it
-nter ur choice 60
/e*uesting process id 6"
?umber of /e*uest for resource # 6"
?umber of /e*uest for resource " 6#
?umber of /e*uest for resource 0 60
2he sFstem is in safe stateP")P3)PM)P#)P0)
/e*uest Rommitted
". >nput
0. ?ew /e*uest
3. 1afe 1tate or ?ot
M. print
H. -(it
-nter ur choice 60
S8B=, =CMDC7
Page I'
SS & OS Lab Manual
/e*uesting process id 60
?umber of /e*uest for resource # 60
?umber of /e*uest for resource " 60
?umber of /e*uest for resource 0 63
2he Process -(ceeds ites <a( ?eed6 2erminated
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL B:>
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
S8B=, =CMDC7
Page I1

You might also like