-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_3_q1.c
80 lines (77 loc) · 1.73 KB
/
experiment_3_q1.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
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <sys/ipc.h>
struct Msg
{
char is_empty;
char number;
};
void handler_kill(int signum)
{
exit(0);
}
void handler_print(int signum)
{
key_t key = ftok(".", 5);
int shmid = shmget(key, 0, 0);//通过ftok函数得到id值存在key中
void *pshm = shmat(shmid, 0, 0);//挂接操作,成功返回指向共享存储段的指针,出错返回-1
if (*(int *)pshm == -1) {//查看挂接是否成功如果出错返回-1,报错
printf("shmat error!\n");
exit(0);
}
struct Msg * msg = (struct Msg*)pshm;
if(msg->is_empty == 0)
{
printf("%d\n", msg->number);
msg->is_empty = 1;
}
}
int main()
{
void *pshm;
key_t key = ftok(".", 5);
int shmid = shmget(key, sizeof(struct Msg), 0666 | IPC_CREAT);
pshm = shmat(shmid, 0, 0);
if (*(int *)pshm == -1) { //查看挂接是否成功如果出错返回-1,报错
printf("shmat error!\n");
exit(0);
}
struct Msg *msg = (struct Msg *)pshm;
msg->is_empty = 1;
pid_t pid;
signal(SIGUSR1, handler_print);
signal(SIGUSR2, handler_kill);
pid = fork();
//parent process
if(pid != 0)
{
int i;
for(i = 1; i <= 100; i++)
{
if(msg->is_empty != 0)
{
msg->number = i;
msg->is_empty = 0;
raise(SIGUSR1);
usleep(100000);
}
if(i == 100)
{
kill(pid, SIGUSR2);
sleep(1);
}
}
}
//subprocess
if(pid == 0)
{
sleep(100);
}
return 0;
}