程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> unix XSI IPC-消息隊列例程

unix XSI IPC-消息隊列例程

編輯:關於C語言

注意事項:

linux2.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. { 
  8.       int queid = open_msg("/root",100); 
  9.        
  10.       while(1) 
  11.       { 
  12.             fputs("請輸入要發送的類型:1 or 2\n", stdout); 
  13.             int type; 
  14.             scanf("%d",&type); 
  15.              
  16.             switch(type) 
  17.             { 
  18.                   case MYTYPE_ONE: 
  19.                        { 
  20.                               msg_send(queid,"MYTYPE_ONE", MYTYPE_ONE); 
  21.                               break; 
  22.                        } 
  23.                   case MYTYPE_TWO: 
  24.                        { 
  25.                               msg_send(queid,"MYTYPE_TWO", MYTYPE_TWO); 
  26.                               break; 
  27.                        } 
  28.                   default: 
  29.                         { 
  30.                               fputs("輸入類型錯誤,請重新輸入\n",stdout); 
  31.                               break; 
  32.                         }                   
  33.              
  34.             } 
  35.              
  36.             fputs("輸入:q 為退出,其它表示繼續\n",stdout); 
  37.             if(getchar() == 'q') 
  38.             { 
  39.                   fputs("退出成功!\n",stdout); 
  40.                   break; 
  41.             } 
  42.             else 
  43.             { 
  44.                   fputs("繼續發送消息\n",stdout); 
  45.             }      
  46.       } 
  47. //不發送退出需要獎隊列移除       
  48.       del_que(queid); 
  49.        
  50.       return 0; 
  51. } 



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. { 
  8.       int queid = open_msg("/root",100); 
  9.        
  10.       while(1) 
  11.       { 
  12.             fputs("請接收要發送的類型:1 or 2\n", stdout); 
  13.             int type; 
  14.             scanf("%d",&type); 
  15.              
  16.             switch(type) 
  17.             { 
  18.                   case MYTYPE_ONE: 
  19.                        { 
  20.                               msg_rec(queid,MYTYPE_ONE); 
  21.                               break; 
  22.                        } 
  23.                   case MYTYPE_TWO: 
  24.                        { 
  25.                               msg_rec(queid,MYTYPE_TWO); 
  26.                               break; 
  27.                        } 
  28.                   default: 
  29.                         { 
  30.                               fputs("輸入類型錯誤,請重新輸入\n",stdout); 
  31.                               break; 
  32.                         }                   
  33.              
  34.             } 
  35.              
  36.             fputs("輸入:q 為退出,其它表示繼續\n",stdout); 
  37.             if(getchar() == 'q') 
  38.             { 
  39.                   fputs("退出成功!\n",stdout); 
  40.                   break; 
  41.             } 
  42.             else 
  43.             { 
  44.                   fputs("繼續發送消息\n",stdout); 
  45.             }      
  46.       } 
  47. //隊列移除       
  48.       del_que(queid); 
  49.        
  50.       return 0; 
  51. } 




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. { 
  14.       //獲取IPC對象的一個鍵 
  15.       key_t key = ftok(path, id); 
  16.       if(-1 == key) 
  17.       { 
  18.             perror("ftok\n"); 
  19.             exit(1); 
  20.       } 
  21.       //創建一個隊列 
  22.       int queid = msgget(key, IPC_CREAT|0666); 
  23.       if(-1 == queid) 
  24.       { 
  25.             perror("msgget\n"); 
  26.             exit(1); 
  27.       }  
  28.       return queid; 
  29. } 
  30.  
  31. //發送消息到隊列 
  32. void msg_send(key_t key,char* text, long msgtype) 
  33. { 
  34.       //初始化內容 
  35.       struct MSG tmp; 
  36.       memset(&tmp,sizeof(struct MSG),0); 
  37.       tmp.mytype = msgtype; 
  38.       strcpy(tmp.mytext,text);  
  39.       //發送消息 
  40.       if(msgsnd(key, &tmp, TEXTSIZE, 0)) 
  41.       { 
  42.             perror("msgsnd\n"); 
  43.             exit(1); 
  44.       } 
  45. } 
  46.  
  47. //從消息隊列獲取消息並顯示 
  48. void msg_rec(key_t key,long msgtype) 
  49. { 
  50.       struct MSG tmp; 
  51.       if(-1 == msgrcv(key, &tmp, TEXTSIZE, msgtype, MSG_NOERROR)) 
  52.       { 
  53.             perror("msgrcv\n"); 
  54.             exit(1); 
  55.       } 
  56.       printf("receive content: %s\n",tmp.mytext); 
  57. } 
  58.  
  59. //刪除隊列,即使隊列裡面還有消息也一起刪除 
  60. void del_que(key_t key) 
  61. { 
  62.       if(msgctl(key,IPC_RMID,NULL)) 
  63.       { 
  64.             perror("msgsnd\n"); 
  65.             exit(1);       
  66.       } 
  67. } 



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. { 
  13.       long mytype; 
  14.       char mytext[TEXTSIZE]; 
  15. }; 
  16.  
  17. int open_msg(char*,int); 
  18. void msg_send(key_t,char*,long); 
  19.  
  20.  
  21. #endif // end MSG_H 

 

本文出自 “在路上” 博客,請務必保留此出處http://642364.blog.51cto.com/632364/1125338

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved