Question: We have such a problem: create a child process in a multi-threaded program and let 1. First attemptCode: #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <stdlib.h> #include <sys/wait.h> pthread_mutex_t mutex; void* fun(void* arg) { pthread_mutex_lock(&mutex); printf("fun get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("fun unlock\n"); } int main() { pthread_mutex_init(&mutex, NULL); pthread_t id; pthread_create(&id, NULL, fun, NULL); sleep(1); pid_t pid = fork(); if(pid == -1) { perror("fork err"); return -1; } if(pid == 0) { pthread_mutex_lock(&mutex); printf("child get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("child unlock\n"); exit(0); } wait(NULL); pthread_mutex_destroy(&mutex); printf("main over\n"); return 0; } Conjecture results:
Create a global lock and initialize it The main thread creates a child thread to acquire the lock. The main thread sleeps for one second, and the child thread gets the lock and locks it. The child thread sleeps for 3 seconds, outputs unlock fun, and the child thread exits The main thread starts forking, the child process gets the lock, and outputs child lock Child process unlock output child unlock The main thread of the parent process waits for the child process to exit, and finally destroys the lock and outputs main over So… get straight to the correct code! ! ! 2. Rational AnalysisUnfortunately, the answer is wrong! ! !
Analyze again: block? ? Could it be that the child process is blocked when acquiring the lock? Or is the parent process blocked waiting for the child process? In other words: both places are blocked, the child process is blocked when acquiring the lock, causing the parent process to be blocked. Verify it! ! So the program is blocked in two places. The child process is blocked when acquiring the lock, causing the parent process to be blocked. 3. Problem Solving
(1) Using pthread_join()Use pthread_join() before fork. Before the child thread ends, the main thread will be blocked and wait for the child thread to end before forking. At this time, the lock obtained by the child process is unlocked. Code: #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <stdlib.h> #include <sys/wait.h> pthread_mutex_t mutex; void* fun(void* arg) { pthread_mutex_lock(&mutex); printf("fun get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("fun unlock\n"); } int main() { pthread_mutex_init(&mutex, NULL); pthread_t id; pthread_create(&id, NULL, fun, NULL); pthread_join(id, NULL); sleep(1); pid_t pid = fork(); if(pid == -1) { perror("fork err"); return -1; } if(pid == 0) { pthread_mutex_lock(&mutex); printf("child get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("child unlock\n"); exit(0); } wait(NULL); pthread_mutex_destroy(&mutex); printf("main over\n"); return 0; } result: (2) Use phread_atfork() to register a pre-fork judgment Header file: prepare: This function is called before fork is executed parent: The parent process calls this function after fork is executed child: The child process calls this function after fork is executed Return value: 0 if successful, error code if failed Code: #include <stdio.h> #include <unistd.h> #include <pthread.h> #include <stdlib.h> #include <sys/wait.h> pthread_mutex_t mutex; void prepare_fun(void) { pthread_mutex_lock(&mutex); } void parent_fun(void) { pthread_mutex_unlock(&mutex); } void child_fun() { pthread_mutex_unlock(&mutex); } void* fun(void* arg) { pthread_mutex_lock(&mutex); printf("fun get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("fun unlock\n"); } int main() { pthread_mutex_init(&mutex, NULL); pthread_t id; pthread_atfork(prepare_fun, parent_fun, child_fun); pthread_create(&id, NULL, fun, NULL); sleep(1); pid_t pid = fork(); if(pid == -1) { perror("fork err"); return -1; } if(pid == 0) { pthread_mutex_lock(&mutex); printf("child get lock\n"); sleep(3); pthread_mutex_unlock(&mutex); printf("child unlock\n"); exit(0); } wait(NULL); pthread_mutex_destroy(&mutex); printf("main over\n"); return 0; } result: This is the end of this article about the fork and mutex process examples in Linux multithreading. For more relevant Linux multithreading fork and mutex 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:
|
<<: Implementation of CSS linear gradient concave rectangle transition effect
>>: How to get the dynamic number of remaining words in textarea
Docker Compose Docker Compose is a tool for defin...
Table of contents 1. Introduction 2. GitHub 3. Ba...
1. Download First of all, I would like to recomme...
Table of contents 1. The writing order of a compl...
Method 1: Use script method: Create a common head...
What is text wrapping around images? This is the ...
one: 1. Semantic tags are just HTML, there is no ...
This article example shares the specific code of ...
Today I recommend such an open source tool for ex...
Table of contents Update the image from an existi...
This article introduces a detailed explanation of...
How to modify the style of the el-select componen...
After Ubuntu 20.04 is installed, there is no root...
In MySQL 8.0.18, a new Hash Join function was add...
Copy code The code is as follows: <div style=&...