Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7556283
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-06-17 21:09:21

  1. /*
  2.  * 创建消息队列
  3.  * Lzy    2011-6-15
  4.  */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/ipc.h>
  9. #include <sys/msg.h>

  10. int main(void)
  11. {
  12.     int qid;        //队列标识符
  13.     key_t key;        //消息队列键值
  14.     
  15.     key = ftok("/work/msg", 'a');    //生成队列键值(文件必须存在)
  16.     if(key < 0)
  17.     {
  18.         perror("ftok");
  19.         exit(0);
  20.     }

  21.     qid = msgget(key,IPC_CREAT | 0666);    //打开或创建队列
  22.     if(qid < 0)
  23.     {
  24.         perror("msgget");
  25.         exit(0);
  26.     }
  27.     else
  28.     {
  29.         printf("Done!\n");
  30.     }

  31.     return 0;
  32. }
  1. /*
  2.  * 消息队列控制
  3.  * 消息队列的删除
  4.  * Lzy    2011-6-15
  5.  */

  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/ipc.h>
  10. #include <sys/msg.h>

  11. int main(int argc, char *argv[])
  12. {
  13.     int qid;
  14.     int ret;

  15.     qid = atoi(argv[1]);        //将传进来的消息队列键值,赋给qid
  16.     ret = msgctl(qid, IPC_RMID, NULL);//删除消息队列
  17.     if(ret < 0)
  18.     {
  19.         perror("msgctl");
  20.         exit(0);
  21.     }
  22.     else
  23.     {
  24.         printf("Done!\n");
  25.     }

  26.     return 0;
  27. }
  1. /*
  2.  * 消息队列的读写
  3.  * 实现两个进程之间的消息传递
  4.  * 消息发送程序
  5.  *Lzy    2011-6-15
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/types.h>
  11. #include <sys/ipc.h>
  12. #include <sys/msg.h>

  13. struct msgbuf
  14. {
  15.     long mtype;        //消息类型
  16.     char mtext[1024];    //消息内容
  17. };

  18. int main(void)
  19. {
  20.     int qid;            //消息队列标识符    
  21.     key_t key;            //消息队列键值
  22.     struct msgbuf buf;        
  23.     int ret;

  24.     key = ftok("msg", 'a');    //获取键值
  25.     if(key < 0)
  26.     {
  27.         perror("ftok");
  28.         exit(0);
  29.     }

  30.     qid = msgget(key, IPC_CREAT | 0666);    //打开消息队列
  31.     if(qid < 0)
  32.     {
  33.         perror("msgget");
  34.         exit(0);
  35.     }
  36.     
  37.     while(1)
  38.     {
  39.         buf.mtype = getpid();        //消息类型为自己的ID
  40.         gets(buf.mtext);
  41.         if(strncmp(buf.mtext, "quit", 4) == 0)    //循环退出
  42.             break;
  43.         
  44.         ret = msgsnd(qid, &buf, sizeof(buf)-4*sizeof(long), 0);    //发送消息
  45.         if(ret < 0)
  46.         {
  47.             perror("msgsnd");
  48.             exit(0);
  49.         }
  50.     }

  51.     return 0;
  52. }
  1. /*
  2.  * 消息队列的读写
  3.  * 实现两个进程之间的消息传递
  4.  * 消息接收程序
  5.  * Lzy    2011-6-15
  6.  */

  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <sys/ipc.h>
  11. #include <sys/msg.h>
  12. #include <string.h>

  13. struct msgbuf
  14. {
  15.     long mtype;        //消息类型
  16.     char mtext[1024];    //消息内容
  17. };

  18. int main(void)
  19. {
  20.     int qid;            //消息队列标识符    
  21.     key_t key;            //消息队列键值
  22.     struct msgbuf buf;    
  23.     int ret;

  24.     key = ftok("msg", 'a');    //获取键值
  25.     if(key < 0)
  26.     {
  27.         perror("ftok");
  28.         exit(0);
  29.     }

  30.     qid = msgget(key, IPC_CREAT | 0666);    //打开消息队列
  31.     if(qid < 0)
  32.     {
  33.         perror("msgget");
  34.         exit(0);
  35.     }
  36.     
  37.     while(1)
  38.     {
  39.         bzero(&buf, sizeof(buf));
  40.         ret = msgrcv(qid, &buf, sizeof(buf)-4*sizeof(long), 0, 0);    //接收消息
  41.         if(ret < 0)
  42.         {
  43.             perror("msgsnd");
  44.             exit(0);
  45.         }
  46.         else
  47.             printf("%s\n",buf.mtext);        
  48.     }

  49.     return 0;
  50. }
阅读(1562) | 评论(0) | 转发(3) |
0

上一篇:信号处理

下一篇:共享内存

给主人留下些什么吧!~~