现在的位置: 首页 > 综合 > 正文

Linux消息队列应用

2017年12月19日 ⁄ 综合 ⁄ 共 3270字 ⁄ 字号 评论关闭

 

  1. #include"sys/types.h"  
  2. #include "sys/msg.h"  
  3. #include "unistd.h"  
  4. #include"stdio.h"  
  5. void msg_stat(int,struct msqid_ds);  
  6.  
  7. int main()  
  8. {  
  9.     int gflags,sflags,rflags;  
  10.     key_t key;  
  11.     int msgid;  
  12.     int reval;  
  13.     struct msgbuf{  
  14.         int mtype;  
  15.         char mtext[1];  
  16.     }msg_sbuf;  
  17.     struct msgmbuf{  
  18.         int mtype;char mtext[10];  
  19.     }msg_rbuf;  
  20.     struct msqid_ds msg_ginfo,msg_sinfo;  
  21.     char* msgpath="/UNIX/msgqueue";  
  22.     key=ftok(msgpath,'a');  
  23.     gflags=IPC_CREAT|IPC_EXCL;  
  24.     msgid=msgget(key,gflags|00666);  
  25.     if(msgid==-1){  
  26.         printf("msg create error!\n");  
  27.         return;  
  28.     }  
  29.     /*创建一个消息队列后,输出消息队列默认属性*/ 
  30.     msg_stat(msgid,msg_ginfo);  
  31.     sflags=IPC_NOWAIT;  
  32.     msg_sbuf.mtype=10;  
  33.     msg_sbuf.mtext[0]='a';  
  34.     reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);  
  35.     if (reval==-1)  
  36.     {  
  37.         printf("message send error!\n");  
  38.     }  
  39.     /*发送一个消息后,输出消息队列属性*/ 
  40.     msg_stat(msgid,msg_ginfo);  
  41.     rflags=IPC_NOWAIT|MSG_NOERROR;  
  42.     reval=msgrcv(msgid,&msg_rbuf,4,10,rflags);  
  43.     if (reval==-1)  
  44.     {  
  45.         printf("Read msg error!\n");  
  46.     }  
  47.     else 
  48.         printf("Read from msg queue %d bytes\n",reval);  
  49.     /*从消息队列中读出消息后,输出消息队列属性*/ 
  50.     msg_stat(msgid,msg_ginfo);  
  51.     msg_sinfo.msg_perm.uid=8;  
  52.     msg_sinfo.msg_perm.gid=8;  
  53.     msg_sinfo.msg_qbytes=16388;  
  54.     /************************************************************************/ 
  55.     /* 此处验证超级用户可以更改消息队列的默认msg_qbytes                     */ 
  56.     //注意这里设置的值大于最大默认值  
  57.     /************************************************************************/ 
  58.     reval=msgctl(msgid,IPC_SET,&msg_sinfo);  
  59.     if(reval==-1){  
  60.         printf("msg set info error!\n");  
  61.         return;  
  62.     }  
  63.     msg_stat(msgid,msg_ginfo);  
  64.     /************************************************************************/ 
  65.     /* 验证设置消息队列属性                                                 */ 
  66.     /************************************************************************/ 
  67.     reval=msgctl(msgid,IPC_RMID,NULL);//删除消息队列  
  68.     if (reval==-1)  
  69.     {  
  70.         printf("unlink msg queue error!\n");  
  71.         return;  
  72.     }  
  73. }  
  74. void msg_stat(int msgid,struct msqid_ds msg_info)  
  75. {  
  76.     int reval;  
  77.     sleep(1);  
  78.     reval=msgctl(msgid,IPC_STAT,&msg_info);  
  79.     if (reval==-1)  
  80.     {  
  81.         printf("get msg info error!\n");  
  82.         return;  
  83.     }  
  84.     printf("\n");  
  85.     printf("current number of bytes on queue is %ld \n",msg_info.msg_cbytes);  
  86.     printf("number of messages in the queue is %ld \n",msg_info.msg_qnum);  
  87.     printf("max number of bytes on queue id %ld \n",msg_info.msg_qbytes);  
  88.     /************************************************************************/ 
  89.     /* 每个消息队列是容量(字节数)都有限制MSGMNB,值的大小因系统而异  
  90.     在创建新的消息队列时,msg_qtytes的默认值就是MSHMNB  
  91.     /************************************************************************/ 
  92.     printf("pid of last msgsnd is %ld\n",msg_info.msg_lspid);  
  93.     printf("pid of last msgrcv is %ld \n",msg_info.msg_lrpid);  
  94.     printf("last msgcnd time is %s \n",ctime(&(msg_info.msg_stime)));  
  95.     printf("last msgrcv time is %s \n",ctime(&(msg_info.msg_rtime)));  
  96.     printf("last change time is %s \n",ctime(&(msg_info.msg_ctime)));  
  97.     printf("msg uid is %ld \n",msg_info.msg_perm.uid);  
  98.     printf("msg gid is %ld \n",msg_info.msg_perm.gid);  
  99. }  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702496

抱歉!评论已关闭.