What would be the output of the following program? Explain.
i n t main ( )
{
p i d t pid 1 ;
p r i n t f ( ‘ ‘ b e f o r e f o r k ( ) ’ ’ ) ;
i f ( ( pid 1=f o r k ()) >0)
{
w ai t pi d ( pid1 , NULL, 0 ) ;
}
e l s e i f ( pid 1 == 0 ) {
e x e c l ( ‘ ‘ / u s r / bin / bash ’ ’ , ‘ ‘ bash ’ ’ ,NULL ) ;
p r i n t f ( ‘ ‘ done l a u n c hi n g the s h e l l ’ ’ ) ;
}
e l s e {
p e r r o r ( ‘ ‘ f o r k ( ) ’ ’ ) ;
}
}
What to submit:
• Write-up describing an explanation for the output (i.e. why you observe
what you observe).
The program will print a new shell (bash) prompt as it will be loaded in the child's copy of the process by the function execl(). After exiting this shell, there will become available the output of the parent exemplar of the program: "before fork()"
Comments
Leave a comment