0

In the code below, there is no memory leak in parent and child as far as I checked with valgrind.

Child   total heap usage: 1 allocs, 1 frees, 272 bytes allocated

Parent  total heap usage: 2 allocs, 2 frees, 1,296 bytes allocated

I have two questions here. First, didn't process allocated memory for create a thread? I didn't join it in the child process. Shouldn't there be memory leak?

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

void* foo(void* arg){


    puts("hello, world!");
    sleep(3);

    return NULL;
}

int main(){

    pthread_t thread;

    pthread_create(&thread, NULL, foo, NULL);

    switch(fork()){

        case -1:
            fprintf(stderr, "error\n");
            break;
        case 0:
            fprintf(stderr, "%d child finished\n", getpid());
            break;
        default:
            pthread_join(thread, NULL); 
            fprintf(stderr, "%d parent finished\n", getpid());
    }   

    return 0;
}

Second question is, thread isn't existed in the child. But when I changed code like below pthread_join is return 0(success). Shouldn't it return error and set errno in child process?

int main(){

    pthread_t thread;

    pthread_create(&thread, NULL, foo, NULL);

    switch(fork()){

        case -1:
            fprintf(stderr, "error\n");
            break;
        case 0:
            fprintf(stderr, "%d child finished\n", getpid());
            break;
        default:
            fprintf(stderr, "%d parent finished\n", getpid());
    }   

    printf("%d\n", pthread_join(thread, NULL));

    puts(strerror(errno));

    return 0;
}

1 Answers1

0

the fork() causes the whole process to be 'duplicated' (actually just another line of execution/process through the same code)

So anything in the parent process is also in the child process

So both the parent and the child have thread running.

So both the parent and the child need to have a call to pthread_join()

The error path, after the call to fork() also has the thread running, so it should also have a call to pthread_join()

Note: when calling pthread_create(), should always check the returned value to assure the operation was successful

Note: when calling fork() then the parent process should also be calling wait() or better, waitpid() so the child process does not become a zombie process (modern OSs will attach the child to the init() process if the actual parent exits before the child.)

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 1
    According to this http://pubs.opengroup.org/onlinepubs/000095399/functions/fork.html "A process shall be created with a single thread. If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, " –  Feb 26 '17 at 10:42
  • But when I add wait and join thread in both process, it says total heap usage: 2 allocs, 2 frees, 1,296 bytes allocated for both child and parent. But if I don't join thread in child it says total heap usage: 1 allocs, 1 frees, 272 bytes allocated for child –  Feb 26 '17 at 10:45
  • I guess it's valgrind's mistake and I have to join threads both child and parent. Thanks. –  Feb 26 '17 at 10:46