Chinaunix首页 | 论坛 | 博客
  • 博客访问: 364656
  • 博文数量: 105
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 826
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-16 13:58
个人简介

记录有意义的东西

文章分类

全部博文(105)

文章存档

2013年(105)

我的朋友

分类: LINUX

2013-04-22 15:26:47

消息队列就是一些消息的列表,(以下称msg)用户可以在msg中添加消息和读取消息等。它具有一定的fifo特性,但它可以实现消息的随机查询,比fifo更有优势。同时这些消息存在于内核中,由队列id来标识。

基 本操作的函数有:调用msgget函数创建或打开msg,创建的msg数量会受到系统msg数量的限制;调用msgsnd添加消息,它把消息添加到已打开 msg的末尾;调用msgrcv来读取消息,它把消息从msg中移走,与fifo不同的是,这里可以取走指定的某一条消息;控制msg的是msgctl函 数,它可以完成多项功能。
函数原型是:

#include
#include
#include

int msgget(key_t key, int msgflg);
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
int msgctl(int msqid, int cmd, struct msqid_ds *buf);

若两个没有关系的进程通过msg通信,注意在各自进程中用msgget时的key值必须一致,即用ftok生成key时,参数需一致,比如都用key_t key = ftok(".", 'a');

下面看个例子,有两个程序:msgsnd.c 和 msgrcv.c

msgsnd.c

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9
 10 #define N 64
 11
 12 typedef struct {
 13
 14     long mtype;
 15     char mtext[N];
 16 }MSG;
 17
 18 int main()
 19 {
 20     key_t key;
 21     int msgid;
 22     MSG msg;
 23
 24     if((key = ftok(".", 'a')) < 0)
 25     {  perror("ftok error");
 26        exit(-1);
 27     }
 28     else
 29     {  printf("key value is %x\n", key); }
 30
 31     if((msgid = msgget(key, 0666 | IPC_CREAT)) < 0)
 32     {  perror("msgget error\n");
 33        exit(-1);
 34     }
 35     else
 36     {  printf("msgget success, id is %d\n", msgid); }
 37
 38
 39
 40     while(1)
 41     {
 42         printf("input some message:\n");
 43         if(fgets(msg.mtext, N, stdin) == NULL)
 44         {  printf("no message\n");
 45            exit(-1);
 46         }
 47         msg.mtype = getpid();
 48
 49         if(msgsnd(msgid, &msg, strlen(msg.mtext), 0) < 0)
 50         {  perror("msgsnd error");
 51            exit(-1);
 52         }
 53         else
 54         {  printf("message added to msg, is %s\n", msg.mtext); }
 55
 56         if(strncmp(msg.mtext, "quit", 4) == 0)
 57         {   break;  }
 58
 59     }
 60
 61
 62     exit(0);
 63
 64
 65 }      (完)


msgrcv.c

  1 #include
  2 #include
  3 #include
  4 #include
  5 #include
  6 #include
  7 #include
  8 #include
  9
 10 #define N 64
 11
 12 typedef struct {
 13
 14     long mtype;
 15     char mtext[N];
 16 }MSG;
 17
 18
 19 int main()
 20 {
 21     key_t key;
 22     int msgid;
 23     MSG msg;
 24
 25     if((key = ftok(".", 'a')) < 0)
 26     {  perror("ftok error");
 27        exit(-1);
 28     }
 29     else
 30     {  printf("key value is %x\n", key); }
 31
 32     if((msgid = msgget(key, 0666 | IPC_CREAT)) < 0)
 33     {  perror("msgget error\n");
 34        exit(-1);
 35     }
 36     else
 37     {  printf("msgget success, id is %d\n", msgid); }
 38
 39
 40
 41     do {
 42
 43         if(msgrcv(msgid, &msg, N, 0, 0) < 0)
 44         {  perror("msgrcv error");
 45            exit(-1);
 46         }
 47         else
 48         {  printf("read from msg, msgid is %ld, content is %s\n", msg.mtype, msg.mtext );}
 49
 50     }while(strncmp(msg.mtext, "quit",4) != 0);
 51
 52     system("ipcs -q");
 53
 54     if(msgctl(msgid, IPC_RMID, NULL) < 0)
 55     {  perror("remove msg error");
 56        exit(-1);
 57     }
 58     printf("msg remove!\n");
 59     system("ipcs -q");
 60     exit(0);
 61 }                   (完)
阅读(774) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~