3,481 views
0 votes
0 votes
Explain the circumstances under which which the line of code marked printf("LINE J") in following program will be reached.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
pid t pid;
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0) { /* child process */
execlp("/bin/ls","ls",NULL);
printf("LINE J");
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}

1 Answer

0 votes
0 votes
When child process executes execlp() system call then if the execution is successful then the memory space assigned to the child process  gets replaced by the process which is given as the parameter to the execlp() system call. In that case printf("LINE J") line would never be reached because it is over written by the new process. But if the execution of execlp() function is unsuccessful then there will be no replacement and printf("LINE J")  Will be executed.

Related questions

0 votes
0 votes
1 answer
2
akash.dinkar12 asked Mar 19, 2019
1,751 views
Including the initial parent process, how many processes are created by the program shown below- #include <stdio.h>#include <unistd.h>int main(){int i;for (i = 0; i < 4; ...
0 votes
0 votes
0 answers
3
akash.dinkar12 asked Mar 19, 2019
373 views
Explain the role of the init process on UNIX and Linux systems in regard to process termination.