Here is a code sample where the fork library call is used to create a child process which shares the parent's address space. The child process executes its code without using the exec system call. My question is: is the exec system call not required in the case that both the parent and child processes are executing the same program?
#include <stdio.h>
int main()
{
int count;
count = fork();
if (count == 0)
printf("\nHi I'm child process and count =%d\n", count);
else
printf("\nHi I'm parent process and count =%d\n", count);
return 0;
}
The answer to this question may be different depending on the operating system. The man page for fork on OS X contains this ominous warning (bold portion is a paraphrase of the original):
There are limits to what you can do in the child process. To be totally safe you should restrict yourself to only executing async-signal safe operations until such time as one of the exec functions is called. All APIs, including global data symbols, in any framework or library should be assumed to be unsafe after a fork() unless explicitly documented to be safe or async-signal safe. If you need to use these frameworks in the child process, you must exec. In this situation it's reasonable to exec another copy of the same executable.
The list of async-signal safe functions can be found in the man page for sigaction(2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With