创建自定义属性的POSIX消息队列
#include <stdio.h> #include <fcntl.h> #include <errno.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <mqueue.h> struct mq_attr attr; const int maxline=4096; int main(int argc, char **argv) { int c, flags; mqd_t mqd; char errbuff[maxline]; 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) { fprintf(stderr, "mqcreate [-e] [ -m maxmsg -z msgsize ] <name>\n"); exit(-1); } if((attr.mq_maxmsg!=0&&attr.mq_msgsize==0) || (attr.mq_maxmsg==0&&attr.mq_msgsize!=0)) { fprintf(stderr, "must specify both -m maxmsg -z msgsize\n"); exit(-1); } if((mqd=mq_open(argv[optind], flags, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, (attr.mq_maxmsg!=0) ? &attr:NULL))<0) { strerror_r(errno, errbuff, maxline); fprintf(stderr, "mq_open error: %s\n", errbuff); exit(-1); } mq_close(mqd); exit(0); }
指定创建队列的最大消息数和每个消息的最大大小