Re: vfork() from userland

kernel@draper.net
Tue, 2 Feb 1999 02:22:39 -0600


On Mon, Feb 01, 1999 at 02:49:23PM -0500, Kev wrote:
> > Here the parent suffers a violent SIGSEGV death:
> >
> > int i=0;
> > if (!syscall(__NR_vfork)) {
> > fprintf(stderr,"%d child %d\n",getpid(),++i);
> > execlp("echo","echo","I love my parent",0);
> > }
> > fprintf(stderr,%d parent %d\n",getpid(),++i);
>
> I assume you typoed when sending this message, as I'm pretty sure the
> compiler would have choked on this fprintf()....

Hi Kev,

Yes, that was a typo made when I sent my original message... sorry.

For the benefit of those following this thread on linux-kernel
(which is still refusing to send me traffic btw, argh)...

The vfork syscall requires that the stack remain unchanged as the
syscall is made (thanks to H. Peter Anvin and Jakub Jelinek). The
vfork syscall is supported in glibc-2.0.112.

The attached code illustrates that the child has access to parent
memory prior to exec, and that the parent is blocked until exec.

Linux vfork behavior now appears consistent with that of Solaris and
HP-UX (verified by me), and other Unix systems having a proper vfork
implementation. Also, fwiw, Linux vfork executes "pretty damn fast".

Thanks to the developers who made this happen.

Reed H. Petty
rhp@draper.net

---------------------------------------------------------------------

#include <linux/unistd.h>
#include <stdio.h>

int main (int argc, char **argv)
{
int i=0, pid;

/* pid = vfork(); glibc-2.0.112 or later, asm for now */
__asm__ ( "movl %1, %%eax\n"
"int $0x80\n"
"movl %%eax, %0\n"
: "=g" (pid)
: "g" (__NR_vfork)
);
if (pid < 0) { /* kernel older than version 2.2.0? */
fprintf(stderr,"%d syscall failed\n",getpid());
exit(1);
}
if (!pid) { /* child */
fprintf(stderr,"%d child %d\n",getpid(),++i);
sleep(3);
execlp("echo","echo","I love my parent",0);
}
/* parent */
fprintf(stderr,"%d parent %d\n",getpid(),++i);
}

Program output:

5622 child 1 <-- 1st increment of i, by child
(sleep 3 seconds) <-- parent and child are blocked
I love my parent <-- child wakes up, exec's echo, unblocks parent
5621 parent 2 <-- 2nd increment of i, by parent
(The last two lines may be reversed)

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/