Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Aug 1999 10:40:29 -0400 (EDT)
From:      Andrew Gallatin <gallatin@cs.duke.edu>
To:        freebsd-hackers@freebsd.org
Subject:   undocumented vfork behaviour?
Message-ID:  <14277.18939.326895.354112@grasshopper.cs.duke.edu>

next in thread | raw e-mail | index | archive | help

I'm working on getting linux/alpha compatability going in
FreeBSD/alpha & I stumbled over some odd fork behaviour that I was
hoping somebody could shed some light on.

Specifically, when a linux/alpha process forks, the child's return
value is the parent's pid rather than zero.  I tracked this behaviour
down to the child's code path in the alpha's cpu_fork():

                /*
                 * Set up return-value registers as fork() libc stub expects.
                 */
                p2tf->tf_regs[FRAME_V0] = p1->p_pid;    /* parent's pid */
                p2tf->tf_regs[FRAME_A3] = 0;            /* no error */
                p2tf->tf_regs[FRAME_A4] = 1;            /* is child */


In the i386 & mips libc Ovfork stubs, I see comments which describe
this behaviour:

/*
 * pid = vfork();
 *
 * %edx == 0 in parent process, %edx == 1 in child process.
 * %eax == pid of child in parent, %eax == pid of parent in child.
 *
 */
Is this comment purely historical?  At least the i386 platform no
longer seems to do this:

If I run the following code on FreeBSD/i386, I see:
parent's pid = 6730
I am the child, result = 0
I must be the parent, result = 6731

Whereas FreeBSD/alpha does this:

parent's pid = 17721
I must be the parent, result = 17721
I must be the parent, result = 17722


Code:

#include <sys/syscall.h>
#include <unistd.h>


main()
{
	int result;
	
	printf("parent's pid = %d\n", getpid());
	result = syscall(SYS_vfork);
	if(result == 0)
		printf("I am the child, result = %d\n", result);
	else
		printf("I must be the parent, result = %d\n", result);
	exit();
}


Before I "fix it" to act like FreeBSD/i386 (I've already tried & it seems to 
work OK for FreeBSD & OSF1 binaries, and fixes my linux/alpha binary's 
problems), I'd like to make sure there aren't any hidden ramifications.

Thanks,

Drew

------------------------------------------------------------------------------
Andrew Gallatin, Sr Systems Programmer	http://www.cs.duke.edu/~gallatin
Duke University				Email: gallatin@cs.duke.edu
Department of Computer Science		Phone: (919) 660-6590


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?14277.18939.326895.354112>