CLONE_VM doesn't appear to work as documented

Hal Duston hald at kc.rr.com
Thu Aug 24 17:29:50 CDT 2006


Ugh.  Something is seriously broken somewhere.

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

[hald at iolo ~]$ strace -o /dev/null ./clone
0
[hald at iolo ~]$ strace -f -o /dev/null ./clone
4

kernel: 2.6.12-2.3.legacy_FC3
gcc: 3.4.4 20050721 (Red Hat 3.4.4-2)
libc: 2.3.6-0.fc3.1

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

[hald at zeus ~]$ strace -o /dev/null ./clone
0
[hald at zeus ~]$ strace -o /dev/null -f ./clone
4

kernel: 2.6.17-1.2157_FC5smp
gcc: 4.1.1 20060525 (Red Hat 4.1.1-1)
libc: 2.4.8

On Thu, Aug 24, 2006 at 03:58:49PM -0500, David Nicol wrote:
> anyone familiar with the nuances of clone(2) care to comment?  I am running
> the attached program on a quad machine with a Linux version
> 2.6.11-1.1369_FC4smp (bhcompile at decompose.build.redhat.com) and gcc
> version 4.0.2 20051125 (Red Hat 4.0.2-8)
>
> and I do not get a single counter increased by multiple threads, which
> is what I want.
>
> I don't even seem to get four different counters incremented by multiple threads
> that are on the same CPU.  It appears that all the threads are running their own
> counter, and all starting from 0.
>
> Advice?
>
>
> --
> David L Nicol
> Dickenson on the flag
> http://cronos.advenge.com/pc/EmilyDickenson/SecondBook/p39.html
> _______________________________________________
> Kclug mailing list
> Kclug at kclug.org
> http://kclug.org/mailman/listinfo/kclug
-------------- next part --------------
#include <signal.h>
#include <stdlib.h>
#include <sched.h>
#include <pthread.h>

int main(int argc, char *argv);
static int child_thread(void *data);
static sig_atomic_t counter;
static pthread_mutex_t clone_mutex;

int main(int argc, char *argv)
{
   char *stack[4];
   pid_t child[4];
   int status;

   pthread_mutex_init(&clone_mutex, NULL);
   stack[0] = malloc(4096);
   stack[1] = malloc(4096);
   stack[2] = malloc(4096);
   stack[3] = malloc(4096);
   child[0] = clone(child_thread, &stack[0][4096], CLONE_VM, NULL);
   child[1] = clone(child_thread, &stack[1][4096], CLONE_VM, NULL);
   child[2] = clone(child_thread, &stack[2][4096], CLONE_VM, NULL);
   child[3] = clone(child_thread, &stack[3][4096], CLONE_VM, NULL);
   waitpid(child[0], &status, 0);
   waitpid(child[1], &status, 0);
   waitpid(child[2], &status, 0);
   waitpid(child[3], &status, 0);
   pthread_mutex_destroy(&clone_mutex);
   printf("%d\n", counter);
   exit(0);
   return 0;
}

static int child_thread(void *data)
{
   pthread_mutex_lock(&clone_mutex);
   ++counter;
   pthread_mutex_unlock(&clone_mutex);
   return 0;
}


More information about the Kclug mailing list