receive.c

来自「这个是学习嵌入式开发的重要例子」· C语言 代码 · 共 157 行

C
157
字号
#include <stdio.h>#include <stdlib.h>#include <mqueue.h>#include <string.h>#include <errno.h>#include <signal.h>#define BUFFER_SIZE	1024#define MQUEUE_NAME	"/mqueue.test.1"int read_message_queue(void);void signal_handler(int s);mqd_t global_mqd;int main(int argc, char **argv){  if (argc < 2)  {    fprintf(stdout, "Usage: %s <name>\n", argv[0]);    exit(1);  }  signal(SIGUSR1, signal_handler);#if 0  type = struct mq_attr  {    long int mq_flags;    long int mq_maxmsg;    long int mq_msgsize;    long int mq_curmsgs;    long int __pad[4];  }#endif  struct mq_attr attr;  memset(&attr, 0, sizeof(attr));  //attr.mq_flags =   attr.mq_maxmsg = 10;  attr.mq_msgsize = 100;  //attr.mq_curmsgs =   //mqd_t mq_open(const char *name, int oflag, ...);  //mqd_t mq_open(char *name, int flags, int mode, struct mq_attr *attr);  if ((global_mqd = mq_open(argv[1], O_RDONLY)) == (mqd_t) - 1)  {    fprintf(stderr, "mq_open() failed: %s\n", strerror(errno));    exit(1);  }  fprintf(stdout, "mq_open() successed.\n");#if 0  struct sigevent  {    sigval_t sigev_value;    int sigev_signo;    int sigev_notify;    union    {      int _pad[13];      __pid_t _tid;      struct      {      ...} _sigev_thread;    } _sigev_un;  }#endif  struct sigevent e;  e.sigev_signo = SIGUSR1;  e.sigev_notify = SIGEV_SIGNAL;  //int mq_notify(mqd_t mqdes, const struct sigevent *notification);  mq_notify(global_mqd, &e);  for (;;)  {  }  mq_close(global_mqd);  //mq_unlink(MQUEUE_NAME);  return 0;}void signal_handler(int s){  fprintf(stdout, "Catched signal %d\n", s);  if (s == SIGUSR1)  {    struct sigevent e;    e.sigev_signo = SIGUSR1;    e.sigev_notify = SIGEV_SIGNAL;    //int mq_notify(mqd_t mqdes, const struct sigevent *notification);    mq_notify(global_mqd, &e);        read_message_queue();  }}int read_message_queue(void){  struct mq_attr attr;  mq_getattr(global_mqd, &attr);#if 0  fprintf(stdout, "attr.mq_flags = %ld\n", attr.mq_flags);  fprintf(stdout, "attr.mq_maxmsg = %ld\n", attr.mq_maxmsg);  fprintf(stdout, "attr.mq_msgsize = %ld\n", attr.mq_msgsize);  fprintf(stdout, "attr.mq_curmsgs = %ld\n", attr.mq_curmsgs);#endif  //char buf[BUFFER_SIZE];  char *buf = NULL;  unsigned int pri;  ssize_t n;  if ((buf = (char *) malloc(attr.mq_msgsize)) == NULL)  {    fprintf(stderr, "Allocate memory failed: %s\n", strerror(errno));    // FIXME: try to allocate again.    exit(1);  }  memset(buf, 0, attr.mq_msgsize);  // Upon successful completion, the mq_receive()    and mq_timedreceive() functions shall return the  length  // of  the selected message in bytes and the message shall be removed from the queue. Otherwise, no message  // shall be removed from the queue, the functions shall return a value of -1, and set errno to indicate the  // error.  //ssize_t mq_receive(mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned *msg_prio);  if ((n = mq_receive(global_mqd, buf, attr.mq_msgsize, &pri)) < 0)  {    fprintf(stderr, "mq_receive() failed: %s\n", strerror(errno));    exit(1);  }  //buf[n] = '\0';  //fprintf(stdout, "Read %d bytes from message queue %s, message: %s, pri: %d\n", n, argv[1], buf, pri);  fprintf(stdout, "Read %d bytes from message queue, message: %s, pri: %d\n", n, buf, pri);  if (buf)  {    free(buf);    buf = NULL;  }  return n;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?