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

MySQL query data by hour, fill in 0 if there is no data

Demand background A statistical interface, the fr...

Native js implementation of slider interval component

This article example shares the specific code of ...

Discussion on the browsing design method of web page content

<br />For an article on a content page, if t...

Introduction to the use and advantages and disadvantages of MySQL triggers

Table of contents Preface 1. Trigger Overview 2. ...

Introduction to RHCE bridging, password-free login and port number modification

Table of contents 1. Configure bridging and captu...

Issues with using Azure Container Registry to store images

Azure Container Registry is a managed, dedicated ...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Html makes a simple and beautiful login page

Let’s take a look first. HTML source code: XML/HT...

JS implements jQuery's append function

Table of contents Show Me The Code Test the effec...

How to hide the version number in Nginx

Nginx hides version number In a production enviro...

Installation steps of Ubuntu 20.04 double pinyin input method

1. Set up Chinese input method 2. Set the double ...

CocosCreator Getting Started Tutorial: Network Communication

Network Communication Overview When developing an...

Docker builds kubectl image implementation steps

If the program service is deployed using k8s inte...