#include <stdio.h> #include <stdlib.h> #include <mqueue.h> int main(int argc, char **argv) { mqd_t mqd; void *ptr; size_t len, prio; if(argc!=4) { fprintf(stderr, "usage: ./a.out <name> <#bytes> <priority>\n"); exit(-1); } len=atoi(argv[2]); prio=atoi(argv[3]); mqd=mq_open(argv[1], O_WRONLY); ptr=calloc(len, sizeof(char)); mq_send(mqd, ptr, len, prio); exit(0); }
往队列中增加消息运行示例:
$ ./mqsend /test 100 30 $ ./mqgetattr /test max #msgs=10, max #bytes/msg=8192, #currently on queue=1
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <mqueue.h> int main(int argc, char **argv) { int c, flags; mqd_t mqd; ssize_t n; unsigned int 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) { fprintf(stderr, "usage: ./a.out [-n] <name>\n"); exit(-1); } mqd=mq_open(argv[optind], flags); mq_getattr(mqd, &attr); 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); }
从队列中读入消息:
$ ./mqreceive -n /test read 100 bytes, priority=30 $ ./mqgetattr /test max #msgs=10, max #bytes/msg=8192, #currently on queue=0标签:include,mqd,prio,int,argv,发送,mq,POSIX,接收 From: https://www.cnblogs.com/donggongdechen/p/16778813.html