温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unix XSI IPC-消息队列例程

发布时间:2020-05-24 02:47:46 来源:网络 阅读:822 作者:xieyihua 栏目:系统运维

注意事项:

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



client.c

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




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.  
  11. //如果存在队列则打开,没有则创建 
  12. int open_msg(char* path, int id) 
  13.       //获取IPC对象的一个键 
  14.       key_t key = ftok(path, id); 
  15.       if(-1 == key) 
  16.       { 
  17.             perror("ftok\n"); 
  18.             exit(1); 
  19.       } 
  20.       //创建一个队列 
  21.       int queid = msgget(key, IPC_CREAT|0666); 
  22.       if(-1 == queid) 
  23.       { 
  24.             perror("msgget\n"); 
  25.             exit(1); 
  26.       }  
  27.       return queid; 
  28.  
  29. //发送消息到队列 
  30. void msg_send(key_t key,char* text, long msgtype) 
  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.       struct MSG tmp; 
  46.       if(-1 == msgrcv(key, &tmp, TEXTSIZE, msgtype, MSG_NOERROR)) 
  47.       { 
  48.             perror("msgrcv\n"); 
  49.             exit(1); 
  50.       } 
  51.       printf("receive content: %s\n",tmp.mytext); 
  52.  
  53. //删除队列,即使队列里面还有消息也一起删除 
  54. void del_que(key_t key) 
  55.       if(msgctl(key,IPC_RMID,NULL)) 
  56.       { 
  57.             perror("msgsnd\n"); 
  58.             exit(1);       
  59.       } 



msg.h


  1. #ifndef MSG_H 
  2. #define MSG_H 
  3.  
  4. #include <sys/types.h> 
  5.  
  6. #define TEXTSIZE 100 
  7. #define ARRYSIZE 2 
  8. #define MYTYPE_ONE 1 
  9. #define MYTYPE_TWO 2 
  10.  
  11. struct MSG 
  12.       long mytype; 
  13.       char mytext[TEXTSIZE]; 
  14. }; 
  15.  
  16. int open_msg(char*,int); 
  17. void msg_send(key_t,char*,long); 
  18.  
  19.  
  20. #endif // end MSG_H 

 

附件:http://down.51cto.com/data/2362206
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI