Introduction to fork in multithreading under Linux

Introduction to fork in multithreading under Linux

Question:

Recall that when a program has only one main thread and fork is called, the child process created by fork will also have only one thread;

What if we put fork into a multi-threaded program?

Let's try it out:

Case (1) fork before creating a child thread

Code:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* pthread_fun(void* arg)
{
	printf("fun = %d\n", getpid());
	pthread_exit(NULL);
}
int main()
{
	fork();

	pthread_t id;
	pthread_create(&id, NULL, pthread_fun, NULL);
	
	printf("main_pid = %d\n", getpid());
	pthread_join(id, NULL);

	return 0;
}

Result: The forked child process will also create its own child thread (兩個進程:四個線程)

insert image description here

Case (2) fork after creating a child thread

Code:

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

void* pthread_fun(void* arg)
{
	printf("fun = %d\n", getpid());
	pthread_exit(NULL);
}
int main()
{

	pthread_t id;
	pthread_create(&id, NULL, pthread_fun, NULL);
	fork();
	
	printf("main_pid = %d\n", getpid());
	pthread_join(id, NULL);
	return 0;
}

Result: After creating a child thread, a child process is created. At this time, the fork child process will only execute the code after fork (兩個進程:三個線程)

insert image description here

Case (3) fork in child thread

Code:

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

void* pthread_fun(void* arg)
{
	fork();
	printf("fun = %d\n", getpid());
	pthread_exit(NULL);
}
int main()
{

	pthread_t id;
	pthread_create(&id, NULL, pthread_fun, NULL);
	
	printf("main_pid = %d\n", getpid());
	pthread_join(id, NULL);

	return 0;
}

result:

insert image description here

in conclusion:

In which thread is fork, the child process created after fork will use this thread as its main thread and execute the code after this thread

This is the end of this article about fork in multi-threading under Linux. For more relevant Linux multi-threading fork content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Python garbage collection mechanism
  • Python language development garbage collection mechanism principle tutorial
  • Analysis of the principle of Python garbage collection mechanism
  • How is Python garbage collection implemented?
  • Example of fork and mutex lock process in Linux multithreading
  • Python Garbage Collection and Linux Fork

<<:  What are inline elements and block elements?

>>:  Detailed explanation of common usage of pseudo-classes before and after in CSS3

Recommend

Detailed explanation of the basic commands of Firewalld firewall in Centos7

1. Basics of Linux Firewall The Linux firewall sy...

The use of v-model in vue3 components and in-depth explanation

Table of contents Use two-way binding data in v-m...

Detailed explanation of three ways to wrap text in el-table header

Table of contents Problem Description Rendering T...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

Detailed tutorial on installing MySQL 8.0 from source code on CentOS 7.4

Table of contents 1. Environment 2. Preparation 3...

In-depth understanding of Vue-cli4 routing configuration

Table of contents Preface - Vue Routing 1. The mo...

Analysis of the reasons why MySQL field definitions should not use null

Why is NULL so often used? (1) Java's null Nu...

Three ways to communicate between Docker containers

We all know that Docker containers are isolated f...

MySQL EXPLAIN statement usage examples

Table of contents 1. Usage 2. Output results 1.id...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

Detailed explanation of CocosCreator optimization DrawCall

Table of contents Preface What is DrawCall How do...

Mysql keeps the existing content and adds content later

This command modifies the data table ff_vod and a...