-3

Just 1 question, when I fork(), I have created parent and child process. It is possible to terminate my parent while my child is still running?

basic on this diagram? Differences between fork and exec

if(!fork())
{
  //child
  while(1);
}
else
{
  //parent
  exit();
}

Another scenario is if my child terminated. my parent is able to received the return value from main? if yes, how do i retrieve the value?

What is the different between exec and execlp?

lastly, I understand that when you exec you are overwrite the memory (heap, stack, text, data). Basically, call new .exe . But under the PCB? only the PID, PPID remain while the state and sp all overwrite?

Community
  • 1
  • 1
user2306421
  • 83
  • 1
  • 2
  • 10
  • 1
    Consider doing some preliminary searching. And, assuming you are on a unix based system, consider reading the `man` pages – rliu Jun 23 '13 at 11:13

1 Answers1

1

When you fork, you create a completly new and separate process. The child inherits certain aspects, like open filedescriptors and other things. So you can exit the parent and the child will continue running.

In order to retrieve the exit code and status of the child you can use pid_t waitpid(pid_t pid, int *status, int options); and interpret the status accordingly. In fact, to avoid Zombiprocesses, you should wait on the child anyway.

The exec family creates a new executable inside the currently running process, so the PID remains, but the executation state is initialized.

Devolus
  • 21,661
  • 13
  • 66
  • 113