fork multithreaded processes

problem

In linux system, we all know that fork will produce a copy of the calling process, create a new process, so if the parent process has multiple threads will not copy the parent process multiple threads of it?

answer

Use man forkthe instruction manual to see The answer, in fact, the key period of the following

The child process is created with a single thread—the one that
called fork(). The entire virtual address space of the parent is
replicated in the child, including the states of mutexes, condition
variables, and other pthreads objects; the use of pthread_atfork(3)
may be helpful for dealing with problems that this can cause.

In which the first sentence translates to only one thread is created when the child out, is to call fork () function that thread.

verification

We can use the following code to test:

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

void * func1(void * args){
    while(1){
        printf("func1\n");
    }
}

int main(){
    pthread_t tid;
    pthread_create(&tid, NULL, func1, NULL);
    pid_t pid = fork();
    // 子进程
    if(pid==0){
        int t = 5;
        while(t-->0){
            sleep(1);
            printf("child process\n");
        }
    }
    return 0;
}

Code execution results are as follows:

We can see while waiting for the child process and output the "child process" when the function of the parent process to create threads and no output.

So if we were not in the main thread fork, but to create a thread fork it? code show as below:

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

void * func1(void * args){
    // fork()出一个子进程
    pid_t pid = fork();
    // 父进程退出
    if(pid > 0) return NULL;
    while(1){
        sleep(1);
        printf("func1:my pid = %d\n", getpid());
    }
}

int main(){
    pthread_t tid;
    pthread_create(&tid, NULL, func1, NULL);
    // 等待fork()完成
    sleep(1);
    while(1){
        sleep(1);
        printf("main :my pid = %d\n", getpid());
    }
    return 0;
}

Execution results are as follows:

Func1 function where you can see the results when the process is only performed neutron fork (), but did not continue with the main function of the instruction, also confirmed the child only had a fork in the thread where the fork, and no other threads .

Title outside

We note that this statement in the manual

The entire virtual address space of the parent is replicated in the child, including the states of mutexes, condition variables, and other pthreads objects;

That is, when the fork is copied lock state, that happens at this time if a lock is locked, then the child is also produced in the fork lock state.

Use the following code to verify:

#include<pthread.h>
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void * func1(void * args){
    pthread_mutex_lock(&mutex);
    printf("func1\n");
    pthread_mutex_unlock(&mutex);
}

int main(){
    pthread_t tid;
    pthread_create(&tid, NULL, func1, NULL);
    // sleep保证线程加锁完成才fork
    sleep(1);
    pid_t pid = fork();
    // 子进程
    if(pid==0){
        int t = 5;
        sleep(1);
        while(t--){
            // 子进程请求锁
            pthread_mutex_lock(&mutex);
            printf("child process\n");
            sleep(1);
            pthread_mutex_unlock(&mutex);
        }
    }
    return 0;
}

Program execution results:

The child wants to lock into the state, but no subsequent output, the reason is fork when the mutex is locked, and the child process to mutex locked state copied, but no other thread to release the child lock, it has been requested less than lock in pthread_mutex_lock blocking function.

Guess you like

Origin www.cnblogs.com/wenxuanh/p/12573966.html