clone() <-> getpid() bug in 2.6?

From: Russell Leighton
Date: Sat Jun 05 2004 - 10:26:56 EST



I have a test program (see attached) that shows what looks like a bug in 2.6.5-1.358 (FedoraCore2)...and breaks my program :(

In summary, I am doing:

clone(run_thread, stack + sizeof(stack) -1,
CLONE_FS|CLONE_FILES|CLONE_VM|SIGCHLD, NULL))

According to the man page the child process should have its own pid as returned by getpid()...much like fork().

In 2.6 the child receives the parent's pid from getpid(), while 2.4 works as documented:

In 2.4 the test program does:
parent pid: 26647
clone returned pid: 26648
thread reported pid: 26648

In 2.6 the test program does:
parent pid: 16665
thread reported pid: 16665
clone returned pid: 16666

Is this fixed in later kernels?

Thx

Russ
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <signal.h>
#include <sys/wait.h>

/* to compile:
gcc -Wall clone-pid-test.c -o clone-pid-test
*/

static int run_thread(void *arg)
{
printf("thread reported pid: %d\n", getpid());

return 0;
}

static char stack[4096];

static int create_thread()
{
int
pid;

/* create thread */
if ( (pid = clone(run_thread, stack + sizeof(stack) -1,
CLONE_FS|CLONE_FILES|CLONE_VM|SIGCHLD, NULL)) == -1 ) {
perror("clone:");
exit(-1);
}/* end if */

return pid ;
}

int main(int argc, char *argv[])
{
printf("parent pid: %d\n", getpid());
printf("clone returned pid: %d\n", create_thread());
wait(NULL);
return 0;
}