-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.c
72 lines (61 loc) · 1.59 KB
/
Test.c
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
65
66
67
68
69
70
71
72
/* in sem_r */
#include <semaphore.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
void del_sem(int sig){
if(sem_unlink("mysem_r") < 0) { /* 接收SIGINT信号后删除信号灯,否则下次执行失败 */
printf("\nUnlink sem failed\n");
exit(-1);
}
printf("\n");
exit(0);
}
int main(int argc, const char *argv[]) {
int id;
key_t key;
void *addr;
sem_t *sem_w, *sem_r;
struct sigaction act = {
.sa_handler = del_sem,
.sa_flags = 0,
};
if(sigemptyset(&act.sa_mask) < 0) {
printf("Empty signal set failed\n");
return -1;
}
if(sigaction(SIGINT, &act, NULL) < 0) {
printf("Get signal failed\n");
return -1;
}
if((key = ftok("./myFile", 111)) < 0) {
printf("Get IPC key failed\n");
return -1;
}
if((id = shmget(key, 200, IPC_CREAT|0666)) < 0) {
printf("Get memery id failed\n");
return -1;
}
if((addr = shmat(id, NULL, 0)) < 0) {
printf("Mapping failed\n");
return -1;
}
sem_w = sem_open("mysem_w", O_CREAT|O_RDWR, 0666, 1);
sem_r = sem_open("mysem_r", O_CREAT|O_RDWR, 0666, 0);
while(1){
if(sem_wait(sem_r) < 0) {
printf("Apply failed\n");
return -1;
}
printf("%s", (char *)addr);
if(sem_post(sem_w) < 0) {
printf("Release failed\n");
return -1;
}
}
return 0;
}