2

I'm trying to experiment with how clone() is implemented for threads in Linux 3.10.0-327.3.1.el7.x86_64

I'm running this piece of code and having occasional segfaults. I know if I use CLONE_THREAD then there's no way to check if the thread finished, but why does printf cause problems? How does the Pthread library handle this issue? Without the printfs there's no segfault.

#define STACK_SIZE (1ULL<<22) //4MB

int tmp = 10;

int threadfunc(void *ptr)
{
    printf("i'm here\n");
    tmp++;
    return 0;
}

int main()
{
    char *child_stack = malloc(STACK_SIZE);
    int child_pid = clone(&threadfunc, child_stack+(STACK_SIZE)-1, CLONE_THREAD|CLONE_SIGHAND|CLONE_VM|CLONE_FS|CLONE_FILES|SIGCHLD, NULL);

    printf("my pid: %d\n", getpid());
    printf("child pid: %d\n", child_pid);
    printf("tmp is: %d\n", val);

    return 0;
}
Mike Lui
  • 1,301
  • 10
  • 23
  • 1
    It's tricky to use `clone()` correctly. 99% of the time you want either `fork()` or `pthread_create()` instead. – John Bollinger Jun 28 '16 at 04:47
  • ...just made this comment at https://stackoverflow.com/questions/58731945/what-did-printf-do-in-the-shared-memory ... the default stdio buffer size is 8192 bytes in my machine ... i had been making a stack less than 4096 ok before i started using printf ... unbuffering it at first had opposite effect that wanted, so then i setvbuf with buffer i allocated of 256 bytes with line buffering ( setvbuf ( stdout , buffer , _IOLBF , 128 ) ) and then all was ok with a small stack for each CLONE_VM and short printf lines ... – sol Dec 16 '22 at 22:45

1 Answers1

3

The printf function doesn't understand that it's being called in a clone task; it is designed around the glibc pthread library.

What you're doing is similar to interrupting a printf with an asynchronous signal, and re-entering printf from the signal.

How the library handles the issue is that it uses clone as part of a more complicated implementation of threads, which "dresses up" the cloned task with a thread identity. This thread identity allows printf to lock a mutex around the stream operation.

What is the thread identity? What it means is that a thread-related function in the C library can inquire, in some way, "what thread is calling me?" to obtain an ID, which maps to a thread descriptor structure. The clone function alone won't set up this identity.

Kaz
  • 55,781
  • 9
  • 100
  • 149