I have an example here:
int runcmd(char *cmd)
{
char* argv[MAX_ARGS];
pid_t child_pid;
int child_status;
parsecmd(cmd,argv);
child_pid = fork();
if(child_pid == 0) {
/* This is done by the child process. */
execvp(argv[0], argv);
/* If execvp returns, it must have failed. */
printf("Unknown command\n");
exit(0);
}
else {
/* This is run by the parent. Wait for the child
to terminate. */
do {
pid_t tpid = wait(&child_status);
if(tpid != child_pid) process_terminated(tpid);
} while(tpid != child_pid);
return child_status;
}
}
This one is a classic example of fork()
After fork(), the control goes to child process.
How can I keep in parent process, do stuffs. Instead of jumping to child immediately?
Thank you
The child will always be the child. The parent will always be the parent.
fork()creates a new process, and each runs separately. If you want to do something in the parent then do it in the parent.