首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

Unix XSI IPC 消息佇列完整範例程式

     編輯:關於C語言

注意事項

Linux 2.4.22 限制:

  • 可發送最長訊息位元組數:8192
  • 佇列最大容量位元組數:16384
  • 佇列最大佇列容量數:16

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

  • ftok 建立一個鍵值,是核心中佇列在外部的 ID 號。由於消息佇列處於核心中,只有建立者和核心知道佇列在核心裡的 ID 號,其他進程無法得知,因此需關聯一個外部鍵值進行關聯
  • id 範圍:1-255
  • 回傳核心消息佇列的 ID 號

server.c - 伺服器端程式

  1. #include "msg.h"
  2. #include
  3. #include
  4. #include
  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
  3. #include
  4. #include
  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
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  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
  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
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved