Листинг Г.7. Использование select с очередями System V
//shm/svmsgread.c
1 #include "unpipc.h"
2 #define MAXMSG (8192 + sizeof(long))
3 int
4 main(int argc, char **argv)
5 {
6 int pipe1[2], pipe2[2], mqid;
7 char c;
8 pid_t childpid;
9 fd_set rset;
10 ssize_t n, nread;
11 struct msgbuf *buff;
12 if (argc != 2)
13 err_quit("usage: svmsgread
14 Pipe(pipe1); /* двусторонняя связь */
15 Pipe(pipe2);
16 buff = My_shm(MAXMSG); /* неименованная разделяемая память */
17 if ((childpid = Fork()) == 0) {
18 Close(pipe1[1]); /* child */
19 Close(pipe2[0]);
20 mqid = Msgget(Ftok(argv[1], 0), MSG_R);
21 for(;;) {
22 /* блокируется в ожидании, извещает родительский процесс */
23 nread = Msgrcv(mqid, buff, MAXMSG, 0, 0);
24 Write(pipe2[1], &nread, sizeof(ssize_t));
25 /* ожидает разрешения родительского процесса */
26 if ((n = Read(pipe1[0], &c, 1)) != 1)
27 err_quit("child: read on pipe returned %d", n);
28 }
29 exit(0);
30 } /* $$.bp$$ */
31 /* parent */
32 Close(pipe1[0]);
33 Close(pipe2[1]);
34 FD_ZERO(&rset);
35 FD_SET(pipe2[0], &rset);
36 for(;;) {
37 if ((n = select(pipe2[0] + 1, &rset, NULL, NULL, NULL)) != 1)
38 err_sys("select returned %d", n);
39 if (FD_ISSET(pipe2[0], &rset)) {
40 n = Read(pipe2[0], &nread, sizeof(ssize_t)); /* *INDENT-OFF* */
41 if (n != sizeof(ssize_t))
42 err_quit("parent: read on pipe returned %d", n); /* *INDENT-ON* */
43 printf("read %d bytes, type = %ld\n", nread, buff->mtype);
44 Write(pipe1[1], &c, 1);
45 } else
47 }
46 err_quit("pipe2[0] not ready");
48 Kill(childpid, SIGTERM);
49 exit(0);
50 }