歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux -進程通信

Linux -進程通信

日期:2017/3/1 11:43:16   编辑:關於Linux

一.概念

消息隊列提供了一種從一個進程向另一個進程發送一個數據塊的方法。 每個數據塊都被認為是有一個類型,接收者進程接收的數據塊可以有不同的類型值。我們可以通過發送消息 來避免命名管道的同步和阻塞問題。消息隊列與管道不同的是,消息隊列是基於消息的, 而管道是基於字節流的,且消息隊列的讀取不?定是先入先出。消息隊列與命名管道有一樣的不足,就是每個消息的最大長度是有上限的(MSGMAX),每個消息隊列的總的字節數是有上限的(MSGMNB),系統上消息隊列的總數也有?個上限(MSGMNI)。

查看機器的三個上限值:

\

二. IPC對象數據結構

內核為每個IPC對象維護?一個數據結構(/usr/include/linux/ipc.h)

  1. structipc_perm
  2. {
  3. key_t__key;/*Keysuppliedtoxxxget(2)*/
  4. uid_tuid;/*EffectiveUIDofowner*/
  5. gid_tgid;/*EffectiveGIDofowner*/
  6. uid_tcuid;/*EffectiveUIDofcreator*/
  7. gid_tcgid;/*EffectiveGIDofcreator*/
  8. unsignedshortmode;/*Permissions*/
  9. unsignedshort__seq;/*Sequencenumber*/};

消息隊列,共享內存和信號量都有這樣?一個共同的數據結構。

三、消息隊列結構(/usr/include/linux/msg.h)

  1. /*Obsolete,usedonlyforbackwardscompatibilityandlibc5compiles*/
  2. structmsqid_ds{
  3. structipc_permmsg_perm;
  4. structmsg*msg_first;/*firstmessageonqueue,unused*/
  5. structmsg*msg_last;/*lastmessageinqueue,unused*/
  6. __kernel_time_tmsg_stime;/*lastmsgsndtime*/
  7. __kernel_time_tmsg_rtime;/*lastmsgrcvtime*/
  8. __kernel_time_tmsg_ctime;/*lastchangetime*/
  9. unsignedlongmsg_lcbytes;/*Reusejunkfieldsfor32bit*/
  10. unsignedlongmsg_lqbytes;/*ditto*/
  11. unsignedshortmsg_cbytes;/*currentnumberofbytesonqueue*/
  12. unsignedshortmsg_qnum;/*numberofmessagesinqueue*/
  13. unsignedshortmsg_qbytes;/*maxnumberofbytesonqueue*/
  14. __kernel_ipc_pid_tmsg_lspid;/*pidoflastmsgsnd*/
  15. __kernel_ipc_pid_tmsg_lrpid;/*lastreceivepid*/
  16. };
可以看到第?個紅色條目就是IPC結構體,即是共有的,後面的都是消息隊列所私有的成員。消息隊列是用鏈表實現的。

四.消息隊列的函數

實現消息隊列的相關函數:

\

1.生成新消息隊列或者取得已存在的消息隊列

intmsgget(ket_tkey,intmsgflg); key:可以認為是一個端口號,也可以由函數ftok生成。

參數: msg?g

IPC_CREAT:如果IPC不存在,則創建一個IPC資源,否則打開操作。

IPC_EXCL:只有在共享內存不存在的時候,新的共享內存才建立,否則就產生錯誤。 如果單獨使用IPC_CREAT XXXget()函數要麼返回一個已經存在的共享內存的操作符,要 麼返回一個新建的共享內存的標識符。 如果將IPC_CREAT和IPC_EXCL標志一起使用,XXXget()將返回一個新建的IPC標識符 ;如果該IPC資源已存在,或者返回-1。 IPC_EXEL標志本身並沒有太大的意義,但是和IPC_CREAT標志一起使用可以用來保證所得的對象是新建的,而不是打開已有的對象。

返回值:返回msgid。

2.向隊列讀/寫消息

msgrcv取消息:

ssize_tmsgrcv(intmsqid,void*msgp,size_tmsgsz,longmsgtyp,intmsg?g);

msgsnd讀消息: intmsgsnd(intmsqid,constvoid*msgp,size_tmsgsz,intmsg?g);

msqid:消息隊列的標識碼。

msgp:指向消息緩沖區的指針,此位置用來暫時存儲發送和接收的消息,是一個用戶可定義的通用結構,形態如下:

  1. structmsgstru
  2. {
  3. longmtype;//大於0
  4. charmtext[_SIZE_];//_SIZE_用戶指定大小
  5. }
msgsz:消息的大小。

msgtyp:從消息隊列內讀取的消息形態。如果值為零,則表示消息隊列中的所有消息都會被讀取。   

msg?g:用來指明核心程序在隊列沒有數據的情況下所應采取的行動。如果msg?g和常數IPC_NOWAIT合用,則在msgsnd()執行時若是消息隊列已滿,則msgsnd()將不會阻塞,而會立即返回-1,如果執行的是msgrcv(),則在消息隊列呈空時,不做等待馬上返回-1,並設定 錯誤碼為ENOMSG。當msg?g為0時,msgsnd()及msgrcv()在隊列呈滿或呈空的情形時,采取 阻塞等待的處理模式。


.應用

———使用消息隊列實現一個簡單的客戶端--服務端的通信

實現代碼如下給出:

comm.h

  1. #pragmaonce
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #define_PATH_NAME_"/tmp"
  9. #define_PROJ_ID_0x6666
  10. #define_SIZE_1024
  11. externintserver_type;
  12. externintclient_type;
  13. structmsgbuf
  14. {
  15. longmtype;
  16. charmtext[_SIZE_];
  17. };
  18. intcreat_msg_queue();
  19. intget_msg_queue();
  20. //intcreat_msg_queue(intmsg_id);
  21. intsend_msg(intmsg_id,intsend_type,constchar*msg);
  22. intrecv_msg(intmsg_id,intrecv_type,char*msg_out);
  23. intdestroy_queue(intmsg_id);

comm.c:
  1. #include"comm.h"
  2. intserver_type=1;
  3. intclient_type=2;
  4. staticintcomm_msg_queue(intflags)
  5. {
  6. key_t_key=ftok(_PATH_NAME_,_PROJ_ID_);
  7. if(_key<0)
  8. {
  9. printf("%d:%s",errno,strerror(errno));
  10. return-1;
  11. }
  12. intmsg_id=msgget(_key,flags);
  13. //intmsg_id=msgget(_key,IPC_CREAT|IPC_EXCL|0666);
  14. returnmsg_id;
  15. }
  16. intcreat_msg_queue()
  17. {
  18. intflags=IPC_CREAT|IPC_EXCL|0666;
  19. returncomm_msg_queue(flags);
  20. }
  21. intget_msg_queue()
  22. {
  23. intflags=IPC_CREAT;
  24. returncomm_msg_queue(flags);
  25. }
  26. intdestroy_queue(intmsg_id)
  27. {
  28. if(msgctl(msg_id,IPC_RMID,NULL)!=0)
  29. {
  30. printf("%d:%s",errno,strerror(errno));
  31. return-1;
  32. }
  33. return0;
  34. }
  35. intsend_msg(intmsg_id,intsend_type,constchar*msg)
  36. {
  37. structmsgbuf_buf;
  38. _buf.mtype=send_type;
  39. strncpy(_buf.mtext,msg,strlen(msg)+1);
  40. if(msgsnd(msg_id,&_buf,sizeof(_buf.mtext),0)<0)
  41. {
  42. printf("%d:%s",errno,strerror(errno));
  43. return-1;
  44. }
  45. return0;
  46. }
  47. intrecv_msg(intmsg_id,intrecv_type,char*msg_out)
  48. {
  49. structmsgbuf_buf;
  50. _buf.mtype=0;
  51. memset(_buf.mtext,'\0',sizeof(_buf.mtext));
  52. if(msgrcv(msg_id,&_buf,sizeof(_buf.mtext),recv_type,0)<0)
  53. {
  54. printf("%d:%s",errno,strerror(errno));
  55. return-1;
  56. }
  57. strcpy(msg_out,_buf.mtext);
  58. return0;
  59. }

server.c:
  1. #include"comm.h"
  2. intmain()
  3. {
  4. intmsg_id=creat_msg_queue();
  5. if(msg_id<0)
  6. {
  7. printf("%d:%s\n",errno,strerror(errno));
  8. return1;
  9. }
  10. charbuf[_SIZE_];
  11. while(1)
  12. {
  13. memset(buf,'\0',sizeof(buf));
  14. recv_msg(msg_id,client_type,buf);
  15. printf("client:%s\n",buf);
  16. if(strcasecmp(buf,"quit")==0)
  17. {
  18. break;
  19. }
  20. printf("clientsaydone,PleaseEnter#");
  21. fflush(stdout);
  22. ssize_t_s=read(0,buf,sizeof(buf)-1);
  23. if(_s>0)
  24. {
  25. buf[_s-1]='\0';
  26. }
  27. send_msg(msg_id,server_type,buf);
  28. }
  29. destroy_queue(msg_id);
  30. return0;
  31. }

client.c:
  1. #include"comm.h"
  2. intmain()
  3. {
  4. //intmsg_id=creat_msg_queue();
  5. //if(msg_id<0)
  6. //{
  7. //printf("%d:%s\n",errno,strerror(errno));
  8. //return1;
  9. //}
  10. //
  11. //
  12. intmsg_id=get_msg_queue();
  13. charbuf[_SIZE_];
  14. while(1)
  15. {
  16. printf("PleaseEnter:");
  17. fflush(stdout);
  18. ssize_t_s=read(0,buf,sizeof(buf)-1);
  19. if(_s>0)
  20. {
  21. buf[_s-1]='\0';
  22. }
  23. send_msg(msg_id,client_type,buf);
  24. if(strcasecmp(buf,"quit")==0)
  25. {
  26. break;
  27. }
  28. memset(buf,'\0',sizeof(buf));
  29. recv_msg(msg_id,server_type,buf);
  30. printf("server#%s\n",buf);
  31. }
  32. return0;
  33. }
六.測試過程及結果

頭文件:

\

各個接口:

  1. intcreat_msg_queue();//創建一個消息隊列
  2. intget_msg_queue();//獲得消息隊列的msg_id
\ intsend_msg(intmsg_id,intsend_type,constchar*msg);//發送消息\
intrecv_msg(intmsg_id,intrecv_type,char*msg_out);//接收消息\
intdestroy_queue(intmsg_id);//銷毀隊列\
Copyright © Linux教程網 All Rights Reserved