BGS Health & Education City, Kengeri, Bangalore-60.
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
A MANUAL FOR
VI SEMESTER
UNIX SYSTEM PROGRAMMING & COMPILER DESIGN LAB MANUAL
Subject Code: 10CSL68
Prepared By: Darshan K.R Lecturer, Dept.of CS&E
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 2
Syllabus
List of Experiments for USP: Design, develop, and execute the following programs
1. Write a C/C++POSIX compliant program to check the following limits: (i) No. of clock ticks (ii) Max. no. of child processes (iii) Max. path length (iv) Max. no. of characters in a file name (v) Max. no. of open files/ process
2. Write a C/C++POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.
3. Consider the last 100 bytes as a region. Write a C/C++program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. If the region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region.
4. Write a C/C++program which demonstrates interprocess communication between a reader process and a writer process. Use mkfifo, open, read, write and close APIs in your program.
5. a) Write a C/C++program that outputs the contents of its Environment list b) Write a C / C++program to emulate the unix ln command
6. Write a C/C++program to illustrate the race condition.
7. Write a C/C++program that creates a zombie and then calls system to execute the ps command to verify that the process is zombie.
8. Write a C/C++program to avoid zombie process by forking twice.
9. Write a C/C++program to implement the system function.
10. Write a C/C++program to set up a real-time clock interval timer using the alarm API.
List of Experiments for Compiler Design:
11. Write a C program to implement the syntax-directed definition of if E then S1 and if E then S1 else S2. (Refer Fig. 8.23 in the text book prescribed for 06CS62 Compiler Design, Alfred V Aho, Ravi Sethi, and J effrey D Ullman: Compilers- Principles, Techniques and Tools, Addison-Wesley, 2007).
12. Write a yacc program that accepts a regular expression as input and produce its parse tree as output.
Note: In the examination each student picks one question from the lot of all 12 questions.
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 3
1. Write a C/C++ POSIX compliant program to check the following limits: (i) No. of clock ticks (ii) Max. no. of child processes (iii) Max. path length (iv) Max. no. of characters in a file name (v) Max. no. of open files/ process
To Open the Editor with filename
[root@localhost /]# gedit limit.cpp
#def i ne _POSI X_SOURCE #def i ne _POSI X_C_SOURCE 199309L #i ncl ude<i ost r eam> #i ncl ude<uni st d. h> i nt mai n( ) { usi ng namespace st d; i nt r es; i f ( ( r es=sysconf ( _SC_CLK_TCK) ) ==- 1) cout <<" Syst emdoesnot suppor t \ n" ; el se cout <<" Number of Cl ock Ti ck: " <<r es<<endl ;
i f ( ( r es=sysconf ( _SC_CHI LD_MAX) ) ==- 1) cout <<" Syst emdoesnot suppor t \ n" ; el se cout <<" Maxi mumNumber of Chi l d Pr ocess t hat pr ocess can cr eat e: " <<r es<<endl ;
i f ( ( r es=pat hconf ( / , _PC_PATH_MAX) ) ==- 1) cout <<" Syst emdoesnot suppor t \ n" ; el se cout <<" Maxi mumPat h Lengt h: " <<r es<<endl ;
i f ( ( r es=pat hconf ( / , _PC_NAME_MAX) ) ==- 1) cout <<" Syst emdoesnot suppor t \ n" ; el se cout <<" Maxi mumNo. of Char act er i n a f i l ename: " <<r es<<endl ;
i f ( ( r es=sysconf ( _SC_OPEN_MAX) ) ==- 1) cout <<" Syst emdoesnot suppor t \ n" ; el se cout <<" Maxi mumNumber of opened f i l es per pr ocess: " <<r es<<endl ;
r et ur n 0; } Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 4
To Run the Program
[ r oot @l ocal host / ] # g++ l i mi t . cpp [ r oot @l ocal host / ] # . / a. out
OUTPUT
Number of Cl ock Ti ck: 100 Maxi mumNumber of Chi l d Pr ocess t hat pr ocess can cr eat e: 999 Maxi mumPat h Lengt h: 4096 Maxi mumNo. of Char act er i n a f i l ename: 255 Maxi mumNumber of opened f i l es per pr ocess: 1024
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 5
2. Write a C/C++ POSIX compliant program that prints the POSIX defined configuration options supported on any given system using feature test macros.
To Open the Editor with filename
[ r oot @l ocal host / ] # gedi t t est macr o. cpp
#def i ne _POSI X_SOURCE #def i ne _POSI X_C_SOURCE 199309L #i ncl ude<i ost r eam> #i ncl ude<uni st d. h> i nt mai n( ) { usi ng namespace st d; #i f def _POSI X_J OB_CONTROL cout <<" Syst emSuppor t s J ob Cont r ol f eat ur e" <<endl ; #el se cout <<" Syst emdoesnot suppor t j ob cont r ol \ n" ; #endi f
#i f def _POSI X_SAVED_I DS cout <<" Syst emSuppor t s saved set - UI D and saved set - GI D" <<endl ; #el se cout <<" Syst emdoesnot suppor t saved set - UI D\ n" ; #endi f
#i f def _POSI X_CHOWN_RESTRI CTED cout <<" Syst emSuppor t s Change Owner shi p f eat ur e: " <<endl ; #el se cout <<" Syst emdoesnot suppor t change Owner shi p f eat ur e\ n" ; #endi f
#i f def _POSI X_NO_TRUNC cout <<" Syst emSuppor t s Pat h t r uncat i on opt i on: " <<endl ; #el se cout <<" Syst emdoesnot suppor t Pat h t r uncat i on \ n" ; #endi f
#i f def _POSI X_VDI SABLE cout <<" Syst emSuppor t s Di sabl e Char act er f or f i l es: " <<endl ; #el se cout <<" Syst emdoesnot suppor t Di sabl e Char act er s \ n" ; #endi f
r et ur n 0;
} Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 6
To Run the Program
[ r oot @l ocal host / ] # g++ t est macr o. cpp [ r oot @l ocal host / ] # . / a. out
OUTPUT
Syst emSuppor t s J ob Cont r ol f eat ur e Syst emSuppor t s saved set - UI D and saved set - GI D Syst emSuppor t s Change Owner shi p f eat ur e Syst emSuppor t s Pat h t r uncat i on opt i on Syst emSuppor t s Di sabl e Char act er f or f i l es
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 7
3. Consider the last 100 bytes as a region. Write a C/C++ program to check whether the region is locked or not. If the region is locked, print pid of the process which has locked. If the region is not locked, lock the region with an exclusive lock, read the last 50 bytes and unlock the region.
[ r oot @l ocal host / ] # gedi t l ock. c
#i ncl ude <st di o. h> #i ncl ude <st dl i b. h> #i ncl ude <er r no. h> #i ncl ude <f cnt l . h> #i ncl ude <uni st d. h>
i nt mai n( i nt ar gc, char *ar gv[ ] ) { / * l _t ype l _whence l _st ar t l _l en l _pi d*/ st r uct f l ock f l = {F_UNLCK, SEEK_SET, 0, 100, 0 }; i nt f d; i nt f si ze, of f set ; char buf [ 50] ; i f ( ( f d = open( ar gv[ 1] , O_RDWR) ) == - 1) { per r or ( " Can' t open f i l e" ) ; exi t ( 1) ; } pr i nt f ( " Fi l e i s Not Locked by any Pr ocess\ n" ) ; pr i nt f ( " Pr ess Ent er t o Lock t he Fi l e\ n" ) ; pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ; get char ( ) ; f l . l _t ype = F_WRLCK; f l . l _pi d = get pi d( ) ; i f ( f cnt l ( f d, F_SETLK, &f l ) == - 1) { per r or ( " Can' t set Excul si ve Lock" ) ; exi t ( 1) ; }
el se i f ( f l . l _t ype! =F_UNLCK) { pr i nt f ( " Fi l e has been Excul si vel y Locked by pr ocess: %d\ n" , f l . l _pi d) ; } el se { pr i nt f ( " Fi l e i s not Locked\ n" ) ; }
pr i nt f ( " Pr ess ENTER t o Rel ease l ock: \ n" ) ; get char ( ) ; f l . l _t ype = F_UNLCK; pr i nt f ( " Fi l e has been Unl ocked\ n" ) ; f si ze=l seek( f d, 0, SEEK_END) ; of f set =f si ze- 50;
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 8
l seek( f d, of f set , SEEK_SET) ; r ead( f d, buf , 50) ; pr i nt f ( " Last 50 Byt e Cont ent i n t he f i l e i s\ n" ) ; pr i nt f ( " ====================================\ n" ) ; pr i nt f ( " %s\ n" , buf ) ; r et ur n 0; }
To Run Program
Cr eat e a f i l e, her e we ar e cr eat i ng a f i l e wi t h name demo wi t h t he f ol l owi ng Cont ent :
Consi der t he l ast 100 byt es as a r egi on. Wr i t e a C/ C++ pr ogr amt o check whet her t he r egi on i s l ocked.
[ r oot @l ocal host / ] # cc l ock. c [ r oot @l ocal host / ] # . / a. out demo
OUTPUT
Fi l e i s Not Locked by any Pr ocess Pr ess Ent er t o Lock t he Fi l e - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Fi l e has been Excl usi vel y Locked by pr ocess: 4087 Pr ess Any Key t o r el ease l ock:
Fi l e has been Unl ocked Last 50 Byt e Cont ent i n t he f i l e i s ==================================== / C++ pr ogr amt o check whet her t he r egi on i s l ocked
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 9
4. Write a C/C++ program which demonstrates interprocess communication between a reader process and a writer process. Use mkfifo, open, read, write and close APIs in your program.
[ r oot @l ocal host / ] # gedi t wr i t er . c
Writer Process (writer.c)
#i ncl ude <f cnt l . h> #i ncl ude <sys/ st at . h> #i ncl ude <sys/ t ypes. h> #i ncl ude <uni st d. h>
i nt mai n( ) { i nt f d; char * myf i f o = " / t mp/ myf i f o" ; / * cr eat e t he FI FO ( named pi pe) */ mkf i f o( myf i f o, 0666) ; pr i nt f ( " Run Reader pr ocess t o r ead t he FI FO Fi l e\ n" ) ; f d = open( myf i f o, O_WRONLY) ; wr i t e( f d, " Hi " , si zeof ( " Hi " ) ) ; / * wr i t e " Hi " t o t he FI FO */ cl ose( f d) ;
unl i nk( myf i f o) ; / * r emove t he FI FO */ r et ur n 0; }
[ r oot @l ocal host / ] # gedi t r eader . c
Reader Process (reader.c)
#i ncl ude <f cnt l . h> #i ncl ude <sys/ st at . h> #i ncl ude <sys/ t ypes. h> #i ncl ude <uni st d. h> #def i ne MAX_BUF 1024 i nt mai n( ) { i nt f d; char *myf i f o = " / t mp/ myf i f o" ; char buf [ MAX_BUF] ;
/ * open, r ead, and di spl ay t he message f r omt he FI FO */ f d = open( myf i f o, O_RDONLY) ; r ead( f d, buf , MAX_BUF) ; pr i nt f ( " Recei ved: %s\ n" , buf ) ; cl ose( f d) ;
r et ur n 0; }
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 10
To Run the Program
[ r oot @l ocal host / ] # cc wr i t er . c [ r oot @l ocal host / ] # . / a. out
Run Reader pr ocess t o r ead t he FI FO Fi l e
Af t er t hi s Open New Ter mi nal by pr essi ng shi f t +ct r l +N or Go t o Fi l e- >Open Ter mi nal
[ r oot @l ocal host / ] # cc r eader . c [ r oot @l ocal host / ] # . / a. out
OUTPUT
Recei ved: Hi
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 11
5a) Write a C/C++ program that outputs the contents of its Environment list
[ r oot @l ocal host / ] # gedi t env. c
#i ncl ude<st di o. h> #i ncl ude<st dl i b. h>
i nt mai n( ) { i nt i ; char **pt r ; ext er n char **envi r on; pr i nt f ( " Li st of Envi r onment al Var i abl e\ n" ) ; pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ; f or ( pt r = envi r on; *pt r ! = 0; pt r ++) pr i nt f ( " %s\ n" , *pt r ) ;
exi t ( 0) ; }
To Run the Program
[ r oot @l ocal host / ] # cc env. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
Li st of Envi r onment al Var i abl e - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SSH_AGENT_PI D=3008 HOSTNAME=l ocal host . l ocal domai n DESKTOP_STARTUP_I D= SHELL=/ bi n/ bash TERM=xt er m HI STSI ZE=1000 KDE_NO_I PV6=1 GTK_RC_FI LES=/ et c/ gt k/ gt kr c: / r oot / . gt kr c- 1. 2- gnome2 WI NDOWI D=50331729 QTDI R=/ usr / l i b/ qt - 3. 3 QTI NC=/ usr / l i b/ qt - 3. 3/ i ncl ude USER=r oot HOME=/ r oot SHLVL=2 GNOME_DESKTOP_SESSI ON_I D=Def aul t LOGNAME=r oot QTLI B=/ usr / l i b/ qt - 3. 3/ l i b CVS_RSH=ssh
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 12
5b. Write a C / C++ program to emulate the unix ln command
[ r oot @l ocal host / ] # gedi t l n. cpp
#i ncl ude<i ost r eam> #i ncl ude<uni st d. h> i nt mai n( i nt ar gc, char * ar gv[ ] ) { usi ng namespace st d; i f ( ar gc! =3) { cout <<" Usage . / a. out sour cef i l e dest i nat i on f i l e\ n" ; r et ur n 0; }
i f ( l i nk( ar gv[ 1] , ar gv[ 2] ) ==- 1) { cout <<" Can' t Li nk\ n" ; r et ur n 1; } el se { cout <<" Fi l es have been Li nked\ n" ; } r et ur n 0; }
To Run the Program
[ r oot @l ocal host / ] # g++ l n. cpp [ r oot @l ocal host / ] # . / a. out l n. cpp newf i l ename
OUTPUT
Fi l es have been Li nked [ r oot @l ocal host / ] # gedi t newf i l ename ( The Cont ent of Sour ce f i l e Wi l l be Copi ed t o newf i l ename)
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 13
6. Write a C/C++ program to illustrate the race condition.
[ r oot @l ocal host / ] # gedi t r ace. c
#i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #i ncl ude<er r or . h>
st at i c voi d char at at i me( char *) ;
i nt mai n( voi d) { pi d_t pi d;
i f ( ( pi d = f or k( ) ) < 0) { pr i nt f ( " f or k er r or " ) ; } el se i f ( pi d == 0) { char at at i me( " out put f r omchi l d\ n" ) ; } el se { char at at i me( " out put f r ompar ent \ n" ) ; } exi t ( 0) ; }
st at i c voi d char at at i me( char *st r ) { char *pt r ; i nt c;
set buf ( st dout , NULL) ; / * set unbuf f er ed */ f or ( pt r = st r ; ( c = *pt r ++) ! = 0; ) put c( c, st dout ) ; }
To Run the Program
[ r oot @l ocal host / ] # cc r ace. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
out put f r omchi l d out put f r ompar ent [ r oot @l ocal host / ] # . / a. out out put f r omcohui t l pdu t f r ompar ent [ r oot @l ocal host / ] # . / a. out oouut t ppuut t f f r r oomm pcahr i el dnt Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 14
7. Write a C/C++ program that creates a zombie and then calls system to execute the ps command to verify that the process is zombie.
[ r oot @l ocal host / ] # gedi t zombi e. c
#i ncl ude <st dl i b. h> #i ncl ude <sys/ t ypes. h> #i ncl ude <uni st d. h>
i nt mai n ( ) { pi d_t chi l d_pi d; / * Cr eat e a chi l d pr ocess. */ chi l d_pi d = f or k ( ) ; i f ( chi l d_pi d == 0) { exi t ( 0) ; / * Thi s i s t he chi l d pr ocess. Exi t i mmedi at el y. */ } el se { sl eep( 3) ; / * Thi s i s t he par ent pr ocess. Sl eep f or a mi nut e. */ syst em( " ps - e - o pi d, ppi d, st at , cmd" ) ; } r et ur n 0; }
To Run the Program
[ r oot @l ocal host / ] # cc zombi e. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
PI D PPI D STAT CMD 1 0 Ss i ni t [ 5] 2 1 S [ mi gr at i on/ 0] 3 1 SN [ ksof t i r qd/ 0] 4 1 S [ wat chdog/ 0] 5 1 S [ mi gr at i on/ 1] 6 1 SN [ ksof t i r qd/ 1] 7 1 S [ wat chdog/ 1] 8 1 S [ mi gr at i on/ 2] 9 1 SN [ ksof t i r qd/ 2] 10 1 S [ wat chdog/ 2] 3087 3084 S gnome- pt y- hel per 3088 3084 Ss bash 3166 3088 S+ ./a.out 3167 3166 Z+ [a.out] <defunct> //Zombie Process 3168 3166 R+ ps -e -o pid,ppid,stat,cmd
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 15
8. Write a C/C++ program to avoid zombie process by forking twice.
[ r oot @l ocal host / ] # gedi t avoi dzombi e. c
#i ncl ude<st di o. h> #i ncl ude <sys/ wai t . h> #i ncl ude<er r no. h> #i ncl ude<st dl i b. h>
i nt mai n( ) { pi d_t pi d;
i f ( ( pi d = f or k( ) ) < 0) { pr i nt f ( " f or k er r or " ) ; } el se i f ( pi d == 0) { / * f i r st chi l d */ i f ( ( pi d = f or k( ) ) < 0) pr i nt f ( " f or k er r or " ) ; el se i f ( pi d > 0) exi t ( 0) ; sl eep( 2) ; pr i nt f ( " second chi l d, par ent pi d = %d\ n" , get ppi d( ) ) ; exi t ( 0) ; }
i f ( wai t pi d( pi d, NULL, 0) ! = pi d) / * wai t f or f i r st chi l d */ pr i nt f ( " wai t pi d er r or " ) ; exi t ( 0) ; }
To Run the Program
[ r oot @l ocal host / ] # cc avoi dzombi e. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
Second Chi l d, par ent pi d=1
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 16
9. Write a C/C++ program to implement the system function.
[ r oot @l ocal host / ] # gedi t syst em. c
#i ncl ude<sys/ wai t . h> #i ncl ude<er r no. h> #i ncl ude<uni st d. h> #i ncl ude<st di o. h> #i ncl ude<st dl i b. h>
i nt syst em1( const char *cmdst r i ng) { pi d_t pi d; i nt st at us;
i f ( cmdst r i ng == NULL) r et ur n( 1) ;
i f ( ( pi d = f or k( ) ) < 0) { st at us = - 1; } el se i f ( pi d == 0) { / * chi l d */ execl ( " / bi n/ sh" , " sh" , " - c" , cmdst r i ng, ( char *) 0) ; _exi t ( 127) ; / * execl er r or */ } el se / * par ent */ whi l e ( wai t pi d( pi d, &st at us, 0) < 0) { i f ( er r no ! = EI NTR) st at us = - 1; / * er r or ot her t han EI NTR f r omwai t pi d( ) */ br eak; }
r et ur n( st at us) ; }
i nt mai n( ) { i nt st at us;
i f ( ( st at us = syst em1( " dat e" ) ) < 0) pr i nt f ( " syst em( ) er r or " ) ;
i f ( ( st at us = syst em1( " who" ) ) < 0) pr i nt f ( " syst em( ) er r or " ) ;
exi t ( 0) ; }
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 17
To Run The Program
[ r oot @l ocal host / ] # cc syst em. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
Sun Dec 30 08: 38: 10 I ST 2012 r oot pt s/ 0 2012- 12- 30 08: 34 ( : 0. 0)
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 18
10. Write a C/C++ program to set up a real-time clock interval timer using the alarm API.
[ r oot @l ocal host / ] # gedi t al ar m. c
#i ncl ude<si gnal . h> #i ncl ude<st di o. h> #i ncl ude<uni st d. h> #i ncl ude<er r no. h>
voi d wakeup( ) { pr i nt f ( " Hel l o\ n" ) ; pr i nt f ( " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ n" ) ; }
i nt mai n( ) { i nt i ; st r uct si gact i on act i on; act i on. sa_handl er =wakeup; act i on. sa_f l ags=SA_RESTART; si gempt yset ( &act i on. sa_mask) ;
i f ( si gact i on( SI GALRM, &act i on, 0) ==- 1) { per r or ( " si gact i on" ) ; } whi l e( 1) { al ar m( 5) ; pause( ) ; pr i nt f ( " Wai t i ng For Al ar m\ n" ) ; } r et ur n 0; }
To Run the Program
[ r oot @l ocal host / ] # cc al ar m. c [ r oot @l ocal host / ] # . / a. out
OUTPUT
Hel l o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Wai t i ng For Al ar m Hel l o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( Af t er 5 CPU Cl ockcycl e) Wai t i ng For Al ar m Hel l o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ( Af t er 5 CPU Cl ockcycl e) Wai t i ng For Al ar m Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 19
11. Write a C program to implement the syntax-directed definition of if E then S1 and if E then S1 else S2.
[ r oot @l ocal host / ] # gedi t sdd. c
#i ncl ude<st di o. h> #i ncl ude<st dl i b. h> #i ncl ude<st r i ng. h> i nt par secondi t i on( char [ ] , i nt , char *, i nt ) ; voi d gen( char [ ] , char [ ] , char [ ] , i nt ) ;
i nt mai n( ) { i nt count er =0, st l en=0, el sef l ag=0; char st mt [ 60] ; char st r B[ 54] ; char st r S1[ 50] ; char st r S2[ 45] ;
pr i nt f ( " f or mat of i f st at ement \ n exampl e. . . . . . . . . . . . \ n" ) ; pr i nt f ( " i f ( a<b) t hen( s, a) ; \ n" ) ; pr i nt f ( " i f ( a<b) t hen( s, a) el se ( s, b) ; \ n\ n" ) ; pr i nt f ( " ent er t he st at ement \ n" ) ; scanf ( " %s" , &st mt ) ;
st l en=st r l en( st mt ) ; count er =count er +2; count er =par secondi t i on( st mt , count er , st r B, st l en) ; i f ( st mt [ count er ] ==' ) ' ) count er ++; count er =count er +3; count er =par secondi t i on( st mt , count er , st r S1, st l en) ; i f ( st mt [ count er +1] ==' ; ' ) { pr i nt f ( " \ n par si ng t he i nput st at ement . . . \ n" ) ; gen( st r B, st r S1, st r S2, el sef l ag) ; r et ur n 0; } i f ( st mt [ count er ] ==' ) ' ) count er ++; count er =count er +3; count er =par secondi t i on( st mt , count er , st r S2, st l en) ; count er =count er +2; i f ( count er ==st l en) { el sef l ag=1; pr i nt f ( " \ n par si ng t he i nput st at ement " ) ; gen( st r B, st r S1, st r S2, el sef l ag) ; r et ur n 0; } r et ur n 0; } Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 20
i nt par secondi t i on( char i nput [ ] , i nt cnt r , char *dest , i nt t ot al l en) { i nt i ndex=0, pos=0; whi l e( i nput [ cnt r ] ! =' ( ' && cnt r <=t ot al l en) cnt r ++; i f ( cnt r >=t ot al l en) r et ur n 0; i ndex=cnt r ; whi l e( i nput [ cnt r ] ! =' ) ' ) cnt r ++; i f ( cnt r >=t ot al l en) r et ur n 0; whi l e( i ndex<=cnt r ) dest [ pos++] =i nput [ i ndex++] ; dest [ pos] =' \ 0' ; r et ur n cnt r ; } voi d gen( char B[ ] , char S1[ ] , char S2[ ] , i nt el separ t ) { i nt Bt =101, Bf =102, Sn=103; pr i nt f ( " \ n\ t i f %s got o %d" , B, Bt ) ; pr i nt f ( " \ n\ t got o %d" , Bf ) ; pr i nt f ( " \ n %d: " , Bt ) ; pr i nt f ( " %s" , S1) ; i f ( ! el separ t ) pr i nt f ( " \ n %d: " , Bf ) ; el se { pr i nt f ( " \ n\ t got o %d" , Sn) ; pr i nt f ( " \ n %d : %s" , Bf , S2) ; pr i nt f ( " \ n %d: " , Sn) ; } }
To Run the Program
[ r oot @l ocal host / ] # cc sdd. c [ r oot @l ocal host / ] # . / a. out
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 21
OUTPUT
f or mat of i f st at ement exampl e. . . . . . . . . . . . i f ( a<b) t hen( s, a) ; i f ( a<b) t hen( s, a) el se ( s, b) ;
ent er t he st at ement i f ( a<b) t hen( s, a) ;
par si ng t he i nput st at ement . . .
i f ( a<b) got o 101 got o 102 101: ( s, a) 102:
[ r oot @l ocal host / ] # . / a. out
f or mat of i f st at ement exampl e. . . . . . . . . . . . i f ( a<b) t hen( s, a) ; i f ( a<b) t hen( s, a) el se ( s, b) ;
ent er t he st at ement i f ( a<b) t hen( s, a) el se( s, b) ;
par si ng t he i nput st at ement i f ( a<b) got o 101 got o 102 101: ( s, a) got o 103 102 : ( s, b) 103:
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 22
12. Write a yacc program that accepts a regular expression as input and produce its parse tree as output
[ r oot @l ocal host / ] # gedi t par se. y
%{ #i ncl ude<ct ype. h> char st r [ 20] ; i nt i =0; %} %t oken i d %l ef t ' +' ' / ' ' *' ' - ' %% E: S {i nf i x_post f i x( st r ) ; } S: S' +' T| S' - ' T | T T: T' *' F| T' / ' F | F F: i d| ' ( ' S' ) ' ; %% #i ncl ude<st di o. h> mai n( ) { pr i nt f ( " \ n Ent er t he gr ammar : " ) ; yypar se( ) ; } yyer r or ( ) { pr i nt f ( " i nval i d" ) ; } yyl ex( ) { char ch=' ' ; whi l e( ch! =' \ n' ) { ch=get char ( ) ; st r [ i ++] =ch; i f ( i sal pha( ch) ) r et ur n i d; i f ( ch==' +' | | ch==' *' | | ch==' - ' | | ch==' / ' ) r et ur n ch; } st r [ - - i ] =' \ 0' ; r et ur n( 0) ; exi t ( 0) ; }
voi d push( char st ack[ ] , i nt *t op, char ch) { st ack[ ++( *t op) ] =ch; }
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 23
char pop( char st ack[ ] , i nt *t op) { r et ur n( st ack[ ( *t op) - - ] ) ; } i nt pr ec( char ch) { swi t ch( ch) { case ' / ' : case ' *' : r et ur n 2; case ' +' : case ' - ' : r et ur n 1; case ' ( ' : r et ur n 0; def aul t : r et ur n - 1; } } voi d i nf i x_post f i x( char i nf i x[ ] ) { i nt t op=- 1, i pt r =- 1, ppt r =- 1; char post f i x[ 20] , st ack[ 20] , st ksymb, cur symb; push( st ack, &t op, ' \ 0' ) ; whi l e( ( cur symb=i nf i x[ ++i pt r ] ) ! =' \ 0' ) { swi t ch( cur symb) { case ' ( ' : push( st ack, &t op, cur symb) ; br eak; case ' ) ' : st ksymb=pop( st ack, &t op) ; whi l e( st ksymb! =' ( ' ) { post f i x[ ++ppt r ] =st ksymb; st ksymb=pop( st ack, &t op) ; } br eak; case ' *' : case ' / ' : case ' +' : case ' - ' : whi l e ( pr ec( st ack[ t op] ) >=pr ec( cur symb) ) post f i x[ ++ppt r ] =pop( st ack, &t op) ; push( st ack, &t op, cur symb) ; br eak; def aul t : i f ( i sal num( cur symb) ==0) { pr i nt f ( " er r or i n i nput : " ) ; exi t ( 0) ; } post f i x[ ++ppt r ] =cur symb; } } whi l e( t op! =- 1) post f i x[ ++ppt r ] =pop( st ack, &t op) ; pr i nt f ( " %s\ n" , post f i x) ; } Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 24
To Run the Program
[ r oot @l ocal host / ] # yacc d par se. y [ r oot @l ocal host / ] # cc y. t ab. c l l [ r oot @l ocal host / ] #. / a. out
OUTPUT
Ent er t he gr ammar : ( a*b) +c- ( d*e) ab*c+de*-
Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 25
VIVA Question
1. What are the differences between ANSI C and K & R C? 2. Explain feature test macros. Write a C/C++POSIX compliant program that is supported on any given system using feature test macros. 3. Explain the common characteristics of API and describe the error status codes. 4. Describe the characteristics of POSIX.1 FIPS standard and X/Open standard. 5. Differentiate between ANSI C and C++standards 6. Explain the different file types available in UNIX and POSIX systems 7. Differentiate between hard links and symbolic links with an example. 8. Describe Inode table entries 9. Write a C/C++program to emulate the UNIX mv command 10. Explain how fcntl API is used for file and record locking. 11. Write the code segment in C that reads utmost 100 bytes into a variable but from standard input. 12. List and explain the access mode flags and access modifier flags. Also explain how the permission value specified in an open call is modified by its calling process umask value. 13. Explain the process of changing user and group ID of files 14. What are named pipes? Explain with an example the use of lseek, link, access with their prototypes and argument values. 15. Describe the structure of stat system call along with declarations. Also write struct stat structure. 16. Write a C program to implement the UNIX chown functions 17. Explain Open, Creat, read and fcntl APIs with example 18. With an example program, explain the use of setjmp and longjmp functions. 19. Explain how a C program can be started and various ways of termination. 20. Briefly explain the memory layout of a C program 21. What is fork and vfork? Explain with an example program for each. 22. What is a zombie process? 23. How does UNIX operating system keep process accounting? Write a program to generate accounting data and give its process structure. 24. Write a C/C++program to obtain process attributes. 25. Explain wait, waitpid, wait3 and wait4 functions with their prototypes and uses Unix Systems Programming and Compiler Design Manual
Prepared By: Darshan.K.R, Dept.of CS&E Page 26
26. Explain different exec functions? Describe how their functioning differs from each other. 27. Write short notes on Race condition 28. Write a program that execs an interpreter file. 29. What is process Identifier? Mention the commands for getting different IDs of calling process