static void sig_usr1(int signo) { ssize_t n; Mq_notify(mqd, &sigev); n = Mq_receive(mqd, buff, attr.mq_msgsize, NULL); printf("SIGUSR1 received, read %ld bytes\n", (long)n); return; }
/* $$.bp$$ */ int main(int argc, char **argv) { int nfds; char c; fd_set rset; mqd_t mqd; void *buff; ssize_t n; struct mq_attr attr; struct sigevent sigev; if (argc != 2) err_quit("usage: mqnotifysig5 <name>"); /* 4open queue, get attributes, allocate read buffer */ mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK); Mq_getattr(mqd, &attr); buff = Malloc(attr.mq_msgsize); Pipe(pipefd); /* 4establish signal handler, enable notification */ Signal(SIGUSR1, sig_usr1); sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; Mq_notify(mqd, &sigev); FD_ZERO(&rset); for ( ; ; ) { FD_SET(pipefd[0], &rset); nfds = Select(pipefd[0] + 1, &rset, NULL, NULL, NULL); if (FD_ISSET(pipefd[0], &rset)) { Read(pipefd[0], &c, 1); Mq_notify(mqd, &sigev); /* reregister first */ while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) { printf("read %ld bytes\n", (long) n); } if (errno != EAGAIN) err_sys("mq_receive error"); } } exit(0); }
int main(int argc, char **argv) { mqd_t mqd; void *buff; ssize_t n; sigset_t zeromask, newmask, oldmask; struct mq_attr attr; struct sigevent sigev; if (argc != 2) err_quit("Usage: nfs3 <name>"); mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK); Mq_getattr(mqd, &attr); buff = (void *)Malloc(attr.mq_msgsize); Sigemptyset(&zeromask); Sigemptyset(&newmask); Sigemptyset(&oldmask); Sigaddset(&newmask, SIGUSR1); Signal(SIGUSR1, sig_usr1); sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; Mq_notify(mqd, &sigev); for ( ; ; ) { Sigprocmask(SIG_BLOCK, &newmask, &oldmask); while (mqflag == 0) sigsuspend(&zeromask); mqflag = 0; Mq_notify(mqd, &sigev); while ((n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) { printf("read %ld bytes\n", (long)n); } if (errno != EAGAIN) err_sys("mq_receive error"); Sigprocmask(SIG_UNBLOCK, &newmask, NULL); } exit(0); }
int main(int argc, char **argv) { mqd_t mqd; void *buff; ssize_t n; sigset_t zeromask, newmask, oldmask; struct mq_attr attr; struct sigevent sigev; if (argc != 2) err_quit("usage: mqnotifysig2 <name>"); /* 4open queue, get attributes, allocate read buffer */ mqd = Mq_open(argv[1], O_RDONLY); Mq_getattr(mqd, &attr); buff = Malloc(attr.mq_msgsize); Sigemptyset(&zeromask); /* no signals blocked */ Sigemptyset(&newmask); Sigemptyset(&oldmask); Sigaddset(&newmask, SIGUSR1); /* 4establish signal handler, enable notification */ Signal(SIGUSR1, sig_usr1); sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; Mq_notify(mqd, &sigev); for ( ; ; ) { Sigprocmask(SIG_BLOCK, &newmask, &oldmask); /* block SIGUSR1 */ while (mqflag == 0) sigsuspend(&zeromask); mqflag = 0; /* reset flag */ Mq_notify(mqd, &sigev); /* reregister first */ n = Mq_receive(mqd, buff, attr.mq_msgsize, NULL); printf("read %ld bytes\n", (long) n); Sigprocmask(SIG_UNBLOCK, &newmask, NULL); /* unblock SIGUSR1 */ } exit(0); }
int main(int argc, char **argv) { int signo; mqd_t mqd; void *buff; ssize_t n; sigset_t newmask; struct mq_attr attr; struct sigevent sigev; if (argc != 2) err_quit("usage: mqnotifysig4 <name>"); /* 4open queue, get attributes, allocate read buffer */ mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK); Mq_getattr(mqd, &attr); buff = Malloc(attr.mq_msgsize); Sigemptyset(&newmask); Sigaddset(&newmask, SIGUSR1); Sigprocmask(SIG_BLOCK, &newmask, NULL); /* block SIGUSR1 */ /* 4establish signal handler, enable notification */ sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; Mq_notify(mqd, &sigev); for ( ; ; ) { Sigwait(&newmask, &signo); if (signo == SIGUSR1) { Mq_notify(mqd, &sigev); /* reregister first */ while ( (n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) { printf("read %ld bytes\n", (long) n); } if (errno != EAGAIN) err_sys("mq_receive error"); } } exit(0); }
int main(int argc, char **argv) { if (argc != 2) err_quit("Usage nft <name>"); mqd = Mq_open(argv[1], O_RDONLY | O_NONBLOCK); Mq_getattr(mqd, &attr); sigev.sigev_notify = SIGEV_THREAD; sigev.sigev_value.sival_ptr = NULL; sigev.sigev_notify_function = notify_thread; sigev.sigev_notify_attributes = NULL; Mq_notify(mqd, &sigev); for ( ; ; ) pause(); exit(0); }
static void notify_thread(union sigval arg) { ssize_t n; void *buff; printf("notify_thread started\n"); buff = (void *)Malloc(attr.mq_msgsize); Mq_notify(mqd, &sigev); while ((n = mq_receive(mqd, buff, attr.mq_msgsize, NULL)) >= 0) printf("read %ld bytes\n", (long)n); if (errno != EAGAIN) err_sys("mq_receive error"); free(buff); pthread_exit(NULL); }
int main(int argc, char **argv) { if (argc != 2) err_quit("Usage: nts1 <name>"); mqd = Mq_open(argv[1], O_RDONLY); Mq_getattr(mqd, &attr); buff = (void *)Malloc(attr.mq_msgsize); Signal(SIGUSR1, sig_usr1); sigev.sigev_notify = SIGEV_SIGNAL; sigev.sigev_signo = SIGUSR1; Mq_notify(mqd, &sigev); for ( ; ; ) pause(); exit(0); }