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

CSS method of controlling element height from bottom to top and from top to bottom

Let’s start the discussion from a common question...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...

How to implement the builder pattern in Javascript

Overview The builder pattern is a relatively simp...

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution Recently,...

CSS code abbreviation div+css layout code abbreviation specification

Using abbreviations can help reduce the size of yo...

JavaScript array reduce() method syntax and example analysis

Preface The reduce() method receives a function a...

Detailed explanation of HTML document types

Mine is: <!DOCTYPE html> Blog Garden: <!...

Detailed Introduction to the MySQL Keyword Distinct

Introduction to the usage of MySQL keyword Distin...

MySQL's conceptual understanding of various locks

Optimistic Locking Optimistic locking is mostly i...

Summary of HTML horizontal and vertical centering issues

I have encountered many centering problems recent...

MySQL 5.7.18 Green Edition Download and Installation Tutorial

This article records the detailed process of down...

Solution for installing opencv 3.2.0 in Ubuntu 18.04

Download opencv.zip Install the dependencies ahea...

A brief discussion on the use of GROUP BY and HAVING in SQL statements

Before introducing the GROUP BY and HAVING clause...

Detailed explanation of JavaScript array deduplication

Table of contents 1. Array deduplication 2. Dedup...

vue-cli4.5.x quickly builds a project

1. Install vue-cli npm i @vue/cli -g 2. Create a ...