int main(int argc, char *argv[]) { int c, flags; mqd_t mqd; flags = O_RDWR | O_CREAT; while( (c = Getopt(argc, argv, "em:z:")) !=-1){/*Getopt命令行参数解析*/ switch(c){ case 'e': flags |= O_EXCL; break; case 'm': attr.mq_maxmsg = atol(optarg); break; case 'z': attr.mq_msgsize = atol(optarg); break; } } if(optind != argc -1) err_quit("usage; mqcreate [-e] [-m maxmsg -z msgsize] <name>"); if((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) || (attr.mq_maxmsg == 0 && attr.mq_msgsize != 0)) err_quit("must specify both -m maxmsg and -z msgszie"); mqd = Mq_open(argv[optind], flags, FILE_MODE, (attr.mq_maxmsg != 0)? &attr:NULL); Mq_close(mqd); 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); }
int main(int argc, char **argv) { mqd_t mqd; struct mq_attr attr; if (argc != 2) err_quit("usage: mqgetattr <name>"); mqd = Mq_open(argv[1], O_RDONLY); Mq_getattr(mqd, &attr); printf("max #msgs = %ld, max #bytes/msg = %ld, " "#currently on queue = %ld\n", attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs); Mq_close(mqd); exit(0); }
int main(int argc, char **argv) { int c, flags; mqd_t mqd; flags = O_RDWR | O_CREAT; while ( (c = Getopt(argc, argv, "em:z:")) != -1) { switch (c) { case 'e': flags |= O_EXCL; break; case 'm': attr.mq_maxmsg = atol(optarg); break; case 'z': attr.mq_msgsize = atol(optarg); break; } } if (optind != argc - 1) err_quit("usage: mqcreate [ -e ] [ -m maxmsg -z msgsize ] <name>"); if ((attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) || (attr.mq_maxmsg == 0 && attr.mq_msgsize != 0)) err_quit("must specify both -m maxmsg and -z msgsize"); printf("about to sleep for 30 seconds before mq_open\n"); sleep(30); mqd = Mq_open(argv[optind], flags, FILE_MODE, (attr.mq_maxmsg != 0) ? &attr : NULL); printf("mq_open OK, about to sleep for 30 more seconds\n"); sleep(30); Mq_close(mqd); exit(0); }