patterncMinor
Return from the parent process code or common code
Viewed 0 times
theprocessreturnparentcodefromcommon
Problem
In this program, the parent process creates a child process, then the child executes
Although above code works correctly, I am not sure where to place the
But in this program, the child process calls
Is there any advantage or disadvantage to putting the
ls -l. The parent process waits for the child process to complete and then returns using return 0;. /*child's output redirected to file*/
/*sps, 6262015*/
/*Headers*/
#include
#include
#include
#include
#include
/*Start*/
int main(void)
{
//Create a child process
int ret = fork();
if(ret0)
{
int status;
int w = waitpid(ret, &status, WUNTRACED | WCONTINUED);
}
/*Where to place this ???*/
return 0;
}Although above code works correctly, I am not sure where to place the
return 0; statement. For instance, in the above code it is at the end so it is common to both the parent and child processes.But in this program, the child process calls
execvp, so it will never reach the last line of the code which has return 0;. So, taking this into account, can I place the return 0; inside the parent code as below?/*Parent code*/
else if(ret>0)
{
int status;
int w = waitpid(ret, &status, WUNTRACED | WCONTINUED);
return 0;
}Is there any advantage or disadvantage to putting the
return 0; statement inside the parent process code?Solution
As for all these functions: Take a look at the
that
value
Since you are already handling such errors with
well do it here and either
man page. It statesthat
execvp (respectively execve) will return on error with returnvalue
-1 and with errno set.Since you are already handling such errors with
fork you might aswell do it here and either
exit(1), or fall through to the end.Context
StackExchange Code Review Q#94869, answer score: 2
Revisions (0)
No revisions yet.