千家信息网

unix XSI IPC-消息队列例程

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,注意事项:linux(2.4.22)限制:可发送最长消息字节数为8192队列最大容量字节数 16384队列最大队列容量数 16key_t ftok(char* path,int id)使用说明:fto
千家信息网最后更新 2024年09月22日unix XSI IPC-消息队列例程

注意事项:

linux(2.4.22)限制:

  • 可发送最长消息字节数为8192
  • 队列最大容量字节数 16384
  • 队列最大队列容量数 16

key_t ftok(char* path,int id)使用说明:

  • ftok创建一个键,是内核中的队列在外部的ID号,由于消息队列处于内核中,只有创建者和内核知道队列在内核里面的ID号,这样其它的进程就无法知道内核里面队列ID号,所以要关联一个外部键进行关联
  • id (1-255)
  • 返回内核消息队列的ID号

其它的注意就查看一下unix高级环境编程吧,或者有些问题需要讨论就回我吧!!


server.c

  1. #include "msg.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int main(int argc, char** argv)
  6. {
  7. int queid = open_msg("/root",100);
  8. while(1)
  9. {
  10. fputs("请输入要发送的类型:1 or 2\n", stdout);
  11. int type;
  12. scanf("%d",&type);
  13. switch(type)
  14. {
  15. case MYTYPE_ONE:
  16. {
  17. msg_send(queid,"MYTYPE_ONE", MYTYPE_ONE);
  18. break;
  19. }
  20. case MYTYPE_TWO:
  21. {
  22. msg_send(queid,"MYTYPE_TWO", MYTYPE_TWO);
  23. break;
  24. }
  25. default:
  26. {
  27. fputs("输入类型错误,请重新输入\n",stdout);
  28. break;
  29. }
  30. }
  31. fputs("输入:q 为退出,其它表示继续\n",stdout);
  32. if(getchar() == 'q')
  33. {
  34. fputs("退出成功!\n",stdout);
  35. break;
  36. }
  37. else
  38. {
  39. fputs("继续发送消息\n",stdout);
  40. }
  41. }
  42. //不发送退出需要奖队列移除
  43. del_que(queid);
  44. return 0;
  45. }



client.c

  1. #include "msg.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int main(int argc, char** argv)
  6. {
  7. int queid = open_msg("/root",100);
  8. while(1)
  9. {
  10. fputs("请接收要发送的类型:1 or 2\n", stdout);
  11. int type;
  12. scanf("%d",&type);
  13. switch(type)
  14. {
  15. case MYTYPE_ONE:
  16. {
  17. msg_rec(queid,MYTYPE_ONE);
  18. break;
  19. }
  20. case MYTYPE_TWO:
  21. {
  22. msg_rec(queid,MYTYPE_TWO);
  23. break;
  24. }
  25. default:
  26. {
  27. fputs("输入类型错误,请重新输入\n",stdout);
  28. break;
  29. }
  30. }
  31. fputs("输入:q 为退出,其它表示继续\n",stdout);
  32. if(getchar() == 'q')
  33. {
  34. fputs("退出成功!\n",stdout);
  35. break;
  36. }
  37. else
  38. {
  39. fputs("继续发送消息\n",stdout);
  40. }
  41. }
  42. //队列移除
  43. del_que(queid);
  44. return 0;
  45. }




msg.c


  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include<string.h>
  8. #include"msg.h"
  9. //如果存在队列则打开,没有则创建
  10. int open_msg(char* path, int id)
  11. {
  12. //获取IPC对象的一个键
  13. key_t key = ftok(path, id);
  14. if(-1 == key)
  15. {
  16. perror("ftok\n");
  17. exit(1);
  18. }
  19. //创建一个队列
  20. int queid = msgget(key, IPC_CREAT|0666);
  21. if(-1 == queid)
  22. {
  23. perror("msgget\n");
  24. exit(1);
  25. }
  26. return queid;
  27. }
  28. //发送消息到队列
  29. void msg_send(key_t key,char* text, long msgtype)
  30. {
  31. //初始化内容
  32. struct MSG tmp;
  33. memset(&tmp,sizeof(struct MSG),0);
  34. tmp.mytype = msgtype;
  35. strcpy(tmp.mytext,text);
  36. //发送消息
  37. if(msgsnd(key, &tmp, TEXTSIZE, 0))
  38. {
  39. perror("msgsnd\n");
  40. exit(1);
  41. }
  42. }
  43. //从消息队列获取消息并显示
  44. void msg_rec(key_t key,long msgtype)
  45. {
  46. struct MSG tmp;
  47. if(-1 == msgrcv(key, &tmp, TEXTSIZE, msgtype, MSG_NOERROR))
  48. {
  49. perror("msgrcv\n");
  50. exit(1);
  51. }
  52. printf("receive content: %s\n",tmp.mytext);
  53. }
  54. //删除队列,即使队列里面还有消息也一起删除
  55. void del_que(key_t key)
  56. {
  57. if(msgctl(key,IPC_RMID,NULL))
  58. {
  59. perror("msgsnd\n");
  60. exit(1);
  61. }
  62. }



msg.h


  1. #ifndef MSG_H
  2. #define MSG_H
  3. #include <sys/types.h>
  4. #define TEXTSIZE 100
  5. #define ARRYSIZE 2
  6. #define MYTYPE_ONE 1
  7. #define MYTYPE_TWO 2
  8. struct MSG
  9. {
  10. long mytype;
  11. char mytext[TEXTSIZE];
  12. };
  13. int open_msg(char*,int);
  14. void msg_send(key_t,char*,long);
  15. #endif // end MSG_H
附件:http://down.51cto.com/data/2362206
0