-
Notifications
You must be signed in to change notification settings - Fork 0
/
d.cpp
64 lines (50 loc) · 1.06 KB
/
d.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <unistd.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <future>
using namespace std;
sem_t *sem;
void second(){
int counter = 0;
sem = sem_open("/aos", O_CREAT, 0777, 1);
if (sem == SEM_FAILED) {
perror("sem_open");
exit(1);
}
while (counter <= 10) {
sem_wait(sem);
printf("Pid = pid2, counter = %d\n", counter);
counter++;
sem_post(sem);
sleep(1);
}
sem_close(sem);
sem_unlink("/aos");
}
void first(){
int counter = 0;
sem = sem_open("/aos", O_CREAT, 0777, 1);
if (sem == SEM_FAILED) {
perror("sem_open");
exit(1);
}
while (counter <= 10) {
sem_wait(sem);
printf("Pid = pid1, counter = %d\n", counter);
counter++;
sem_post(sem);
sleep(1);
}
sem_close(sem);
sem_unlink("/aos");
}
int main(){
auto a=async(launch::async, first);
auto b=async(launch::async, second);
a.get();
b.get();
return 0;
}