-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode14_3.cpp
More file actions
49 lines (47 loc) · 1.28 KB
/
Copy pathcode14_3.cpp
File metadata and controls
49 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
pthread_mutex_t mutex;
/*子线程运行的函数,它首先获得互斥锁mutex,然后暂停5s,在释放该互斥锁*/
void *another(void *arg)
{
printf("in child thread,lock the mutex\n");
pthread_mutex_lock(&mutex);
sleep(5);
pthread_mutex_unlock(&mutex);
return nullptr;
}
int main()
{
pthread_mutex_init(&mutex,nullptr);
pthread_t id;
pthread_create(&id,NULL,another,NULL);
/*父进程中的主线程暂停1s,以确保在执行fork操作之前
* 子进程已经开始运行并获得了互斥锁变量mutex*/
sleep(1);
int pid = fork();
if(pid < 0)
{
pthread_join(id,NULL);
pthread_mutex_destroy(&mutex);
return 1;
}
else if(pid == 0)
{
printf("i am in the child,want to get the lock\n");
/*子进程从父进程继承了互斥锁mutex的状态,该互斥锁处于锁住的状态,这是由父进程中的子进程
* 执行pthread_mutex_lock引起的因此,下面这句加锁操作会一直阻塞,尽管从逻辑上说它是不应该
* 阻塞的*/
pthread_mutex_lock(&mutex);
printf("i can not run to here.opp...\n");
pthread_mutex_unlock(&mutex);
exit(0);
}
else
wait(NULL);
pthread_join(id,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}