zoukankan      html  css  js  c++  java
  • Linux消息队列应用

    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! ");  
    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! ");  
    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! ");  
    46.     }  
    47.     else 
    48.         printf("Read from msg queue %d bytes ",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! ");  
    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! ");  
    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! ");  
    82.         return;  
    83.     }  
    84.     printf(" ");  
    85.     printf("current number of bytes on queue is %ld  ",msg_info.msg_cbytes);  
    86.     printf("number of messages in the queue is %ld  ",msg_info.msg_qnum);  
    87.     printf("max number of bytes on queue id %ld  ",msg_info.msg_qbytes);  
    88.     /************************************************************************/ 
    89.     /* 每个消息队列是容量(字节数)都有限制MSGMNB,值的大小因系统而异  
    90.     在创建新的消息队列时,msg_qtytes的默认值就是MSHMNB  
    91.     /************************************************************************/ 
    92.     printf("pid of last msgsnd is %ld ",msg_info.msg_lspid);  
    93.     printf("pid of last msgrcv is %ld  ",msg_info.msg_lrpid);  
    94.     printf("last msgcnd time is %s  ",ctime(&(msg_info.msg_stime)));  
    95.     printf("last msgrcv time is %s  ",ctime(&(msg_info.msg_rtime)));  
    96.     printf("last change time is %s  ",ctime(&(msg_info.msg_ctime)));  
    97.     printf("msg uid is %ld  ",msg_info.msg_perm.uid);  
    98.     printf("msg gid is %ld  ",msg_info.msg_perm.gid);  
    99. }  

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

    积跬步以致千里,积小流以成江海。
    2016年5月之前的博文发布于51cto,链接地址:shamrock.blog.51cto.com
    2016年5月之后博文发布与cnblogs上。
    Github地址 https://github.com/umgsai
    Keep moving~!!!
  • 相关阅读:
    sync.Once.Do(f func())
    协程
    Qt 线程基础(QThread、QtConcurrent、QThreadPool等)
    linux下valgrind的使用概述
    QT--QSocketNotifier类介绍
    QThreadPool类和QtConcurrent命名空间
    联想电池维修
    asm
    tapset::iosched(3)
    systemtap --diskio
  • 原文地址:https://www.cnblogs.com/umgsai/p/3908253.html
Copyright © 2011-2022 走看看