int main(int argc, char **argv) { int c, flags; mqd_t mqd; ssize_t n; uint32_t prio; void *buff; struct mq_attr attr; flags = O_RDONLY; while ( (c = Getopt(argc, argv, "n")) != -1) { switch (c) { case 'n': flags |= O_NONBLOCK; break; } } if (optind != argc - 1) err_quit("usage: mqreceive [ -n ] <name>"); mqd = Mq_open(argv[optind], flags); Mq_getattr(mqd, &attr); // get the max msg size for alloc memory.. buff = Malloc(attr.mq_msgsize); n = Mq_receive(mqd, buff, attr.mq_msgsize, &prio); printf("read %ld bytes, priority = %u\n", (long) n, prio); exit(0); }
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; }
void doit(mqd_t mqsend, mqd_t mqrecv) { char buff[MSGSIZE]; Mq_send(mqsend, buff, 1, 0); if (Mq_receive(mqrecv, buff, MSGSIZE, NULL) != 1) err_quit("mq_receive error"); }
/* * ===================================================================== * Function:MsgQReceive() * Description: receive a message from a message queue * Input: msgQId -- msgQ id * recvBuffer -- buffer to save message * bufLength -- length of recvBuffer * timeTick -- wait time (in ticks) * WAIT_FOREVER, this routine block until message available * NO_WAIT, this routine return immediately with errorno * wait time, this routine block until message available or time up * Output: N/A * Return: OK on success or ERROR otherwise. *====================================================================== */ int MsgQReceive(MSG_QUEUE_ID msgQId, char* recvBuffer, int bufLength, int timeTick) { #ifdef LINUX_OS if (msgQId == AII_NULL || recvBuffer == AII_NULL) { return (-1); } return Mq_receive((mqd_t)(*msgQId), recvBuffer, bufLength, timeTick, AII_NULL); #elif VXWORKS_OS if (msgQId == AII_NULL || recvBuffer == AII_NULL) { return (-1); } return msgQReceive(msgQId, recvBuffer, bufLength, timeTick); #endif }
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 i, nloop; mqd_t mq1, mq2; char buff[MSGSIZE]; pid_t childpid; struct mq_attr attr; if (argc != 2) err_quit("usage: lat_pxmsg <#loops>"); nloop = atoi(argv[1]); attr.mq_maxmsg = MAXMSG; attr.mq_msgsize = MSGSIZE; mq1 = Mq_open(Px_ipc_name(NAME1), O_RDWR | O_CREAT, FILE_MODE, &attr); mq2 = Mq_open(Px_ipc_name(NAME2), O_RDWR | O_CREAT, FILE_MODE, &attr); if ( (childpid = Fork()) == 0) { for ( ; ; ) { /* child */ if (Mq_receive(mq1, buff, MSGSIZE, NULL) != 1) err_quit("mq_receive error"); Mq_send(mq2, buff, 1, 0); } exit(0); } /* 4parent */ doit(mq1, mq2); Start_time(); for (i = 0; i < nloop; i++) doit(mq1, mq2); printf("latency: %.3f usec\n", Stop_time() / nloop); Kill(childpid, SIGTERM); Mq_close(mq1); Mq_close(mq2); Mq_unlink(Px_ipc_name(NAME1)); Mq_unlink(Px_ipc_name(NAME2)); exit(0); }