Листинг 5.24. Функция mq_notify
//my_pxmsg_mmap/mq_notify.с
1 #include "unpipc.h"
2 #include "mqueue.h"
3 int
4 mymq_notify(mymqd_t mqd, const struct sigevent *notification)
5 {
6 int n;
7 pid_t pid;
8 struct mymq_hdr *mqhdr;
9 struct mymq_info *mqinfo;
10 mqinfo = mqd;
11 if (mqinfo->mqi magic != MQI_MAGIC) {
12 errno = EBADF;
13 return(-1);
14 }
15 mqhdr = mqinfo->mqi_hdr;
16 if ((n = pthread_mutex_lock(&mqhdr->mqh_lock)) != 0) {
17 errno = n;
18 return(-1);
19 }
20 pid = getpid();
21 if (notification == NULL) {
22 if (mqhdr->mqh_pid == pid) {
23 mqhdr->mqh_pid = 0; /* снятие вызвавшего процесса с регистрации */
24 } /* если вызвавший процесс не зарегистрирован – 61К */
25 } else {
26 if (mqhdr->mqh_pid != 0) {
27 if (kill(mqhdr->mqh_pid, 0) != –1 || errno != ESRCH) {
28 errno = EBUSY;
29 goto err;
30 }
31 }
32 mqhdr->mqh_pid = pid;
33 mqhdr->mqh_event = *notification;
34 }
35 pthread_mutex_unlock(&mqhdr->mqh_lock);
36 return(0);
37 err:
38 pthread_mutex_unlock(&mqhdr->mqh_lock);
39 return(-1);
40 }