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

Russell Leighton
2004-06-05T15:24:59Z

[-- Attachment #1: Type: text/plain, Size: 716 bytes --]

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

[-- Attachment #2: clone-pid-test.c --]
[-- Type: text/plain, Size: 733 bytes --]

#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;
}