The Gateway to Computer Science Excellence
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 */
printf("LINE J");
else { /* parent process */
/* parent will wait for the child to complete */
printf("Child Complete");
return 0;
in Operating System by Boss (41.8k points) | 30 views

1 Answer

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.
by (113 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,650 questions
56,228 answers
95,688 users