> It is also very common for a program to store the result of fork() in
> a variable, because you have to do two checks against the value:
> check for error and check for parent/child status. The only time I
> have ever seen a program that used an "if (fork()) {....} else
> {....}" construction (thus not using a variable) it caused incorrect
> program behaviour: the fork did fail occasionally and the program
> could not detect it.... So, I think it is safe to say that
> copy-on-write for the data/stack segment is a nice idea but in
> practice not very useful
Storing in a variable is not the same thing as storing in memory. With
a good compiler and optimization enabled it would be stored in a
register:
int run_script()
{
pid_t pid;
if ((pid = fork()) < 0)
return -1;
if (!pid)
{
execl("/bin/sh", "myscript.sh", NULL);
exit(1);
}
return 0;
}
This should produce something like this:
_run_script:
call _fork
test eax, eax
jns 0f
mov eax, 0xffffffff
ret
0: jnz 1f
push 0
push param2
push param1
call _execl
add esp, 12
push 1
call _exit
add esp, 4
1: xor eax, eax
ret
The only time memory writes are needed is to push the parameters and to
perform the call.
This suggests, by the way, that it might work if the data and stack
segments are separate. There may be a good chance that the data segment
does not get written to by the child.
--
With kind regards,
Erik van der Kouwe