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
background: The site is separated from the front ...
Table of contents Preface Check Constraints Creat...
Achieve results Code html <div class="css...
1|0 Compile the kernel (1) Run the uname -r comma...
When I was in the securities company, because the ...
JDK Installation I won't go into too much det...
Core SQL statements MySQL query statement that do...
Table of contents 1. Create a stored function 2. ...
Preface smb is the name of a protocol that can be...
The skills that front-end developers need to mast...
This article mainly introduces the installation/st...
The following three methods are commonly used to d...
Table of contents 1. Global level 2. Database lev...
Preface: In the daily use of the database, it is ...
Learning objectives: Learn to use Windows system ...