linux msg queue 基礎操作

來源:互聯網
上載者:User
#include <sys/types.h>#include <sys/msg.h>#include <unistd.h>#include <string.h>#include <stdio.h>void msg_stat(int,struct msqid_ds );int main(){    int gflags,sflags,rflags;    key_t key;    int msgid;    int reval;    struct msgsbuf{        int mtype;        char mtext[1];    }msg_sbuf;    struct msgmbuf    {        int mtype;        char mtext[10];    }msg_rbuf;    struct msqid_ds msg_ginfo,msg_sinfo;    char* msgpath="/unix/msgqueue";    key=ftok(msgpath,'a');    gflags=IPC_CREAT|IPC_EXCL;    msgid=msgget(key,gflags|00666);    if(msgid==-1)    {        printf("msg create error\n");        return;    }    /*建立一個訊息佇列後,輸出訊息佇列預設屬性*/    msg_stat(msgid,msg_ginfo);    sflags=IPC_NOWAIT;    msg_sbuf.mtype=10;    msg_sbuf.mtext[0]='a';    reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);    if(reval==-1)    {        printf("message send error\n");    }    /*發送一個訊息後,輸出訊息佇列屬性*/    msg_stat(msgid,msg_ginfo);    rflags=IPC_NOWAIT|MSG_NOERROR;    reval=msgrcv(msgid,&msg_rbuf,4,10,rflags);    if(reval==-1)        printf("read msg error\n");    else        printf("read from msg queue %d bytes\n",reval);    /*從訊息佇列中讀出訊息後,輸出訊息佇列屬性*/    msg_stat(msgid,msg_ginfo);    msg_sinfo.msg_perm.uid=8;/*just a try                               msg_sinfo.msg_perm.gid=8;/*                               msg_sinfo.msg_qbytes=16388;                               /*此處驗證超級使用者可以更改訊息佇列的預設msg_qbytes*/    /*注意這裡設定的值大於預設值*/    reval=msgctl(msgid,IPC_SET,&msg_sinfo);    if(reval==-1)    {        printf("msg set info error\n");        return;    }    msg_stat(msgid,msg_ginfo);    /*驗證設定訊息佇列屬性*/    reval=msgctl(msgid,IPC_RMID,NULL);/*刪除訊息佇列*/    if(reval==-1)    {        printf("unlink msg queue error\n");        return;    }}void msg_stat(int msgid,struct msqid_ds msg_info){    int reval;    sleep(1);/*只是為了後面輸出時間的方便*/    reval=msgctl(msgid,IPC_STAT,&msg_info);    if(reval==-1)    {        printf("get msg info error\n");        return;    }    printf("\n");    printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);    printf("number of messages in queue is %d\n",msg_info.msg_qnum);    printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);    /*每個訊息佇列的容量(位元組數)都有限制MSGMNB,值的大小因系統而異。在建立新的訊息佇列時,msg_qbytes的預設值就是MSGMNB*/    printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);    printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);    printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime)));    printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime)));    printf("last change time is %s", ctime(&(msg_info.msg_ctime)));    printf("msg uid is %d\n",msg_info.msg_perm.uid);    printf("msg gid is %d\n",msg_info.msg_perm.gid);}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.