欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++ 中消息隊列函數(shù)實例詳解

 更新時間:2017年06月12日 17:02:57   作者:墨言莫問  
這篇文章主要介紹了C++ 中消息隊列函數(shù)實例詳解的相關(guān)資料,需要的朋友可以參考下

C++ 中消息隊列函數(shù)實例詳解

1.消息隊列結(jié)構(gòu)體的定義

typedef struct{
    uid_t  uid;  /* owner`s user id */
    gid_t  gid;  /* owner`s group id */
    udi_t  cuid;  /* creator`s user id */
    gid_t  cgid;  /* creator`s group id */
    mode_t mode;  /* read-write permissions 0400 MSG_R 0200 MSG_W*/
    ulong_t seq;  /* slot usage sequence number*/
}ipc_perm;

typedef stuct{
    struct ipc_perm msg_perm;   /* read_write perms */
    struct msg   *msg_first;   /* ptr to first message on queue */
    struct msg   *msg_last;   /* ptr to last message on queue */
    msglen_t     msg_cbytes;   /* used bytes current on queue */
    msgqnum_t    msg_qnum;    /* current num of message on queue */
    msglen_t    msg_qbytes;   /* max # of bytes allowed on queue */
    pid_t        msg_lspid;   /* pid of last msgsnd() */
    pid_t        msg_lrpid;   /* pid of last msgrcv() */
    time_t       msg_stime;   /* time of last msgsnd() */
    time_t       msg_rtime;   /* time of last msgrcv() */
    time_t       msg_ctime;   /* time of last msgctl() */
}msqid_ds;

typedef struct
{
    long mtype;
    char mbuf[MSGLEN];
}Message;

2.創(chuàng)建消息隊列:

   /***************************************************
Function:
    int msgget(ket_t key,int oflag);
Explain:
    create or view a message queue
Return :
    a int indetify
Include:
    sys/msg.h
introduction:
    oflag: 0400 msg_r 
        0200 msg_w
        0600 msg_wr
    ipc_creat: NO exist and then creat a queue
          exist : reference a queue
    ipc_creat|ipc_excl: NO exist and then creat a queue
              exist : return error
****************************************************/
#include<stdio.h>
#include<sys/msg.h>
#include<stdlib.h>

int MsgGet(int key)
{
    int ret;
    ret=msgget(key,0600|IPC_CREAT);
//   ret=msgget(key,0600|IPC_CREAT|IPC_EXCL);
    if(ret<0)
        perror("creat msgid error");
    printf("msgid=%d/n",ret);
    system("ipcs -q -i ret");
    return ret;
}
int main(int argc,char *agrv[])
{
    int key;
    printf("pleasse input msgkey:");
    scanf("%d",&key);
    MsgGet(key);
    return 0;
}

3.向消息隊列中發(fā)送消息msgsnd

/***********************************************************************************
Function:
    int msgsnd(int msqid,const void *ptr,size_t length,int flag)
Explain:
    send a message to a queue
Return:
    len: send message len;
Include:
    sys/msg.h
Introduction:
    flag: 0 : if queue full wait:1>具備存放新消息的空間
                   2>由msqid標識的消息隊列從系統(tǒng)中刪除(返回EIDRM錯誤)
                   3>調(diào)用線程被某個捕獲的信號所中斷(返回EINTR錯誤)
       IPC_NOWAIT:如果沒有存放新消息的空間,函數(shù)馬上返回
                   1>指定的隊列中有太多的字節(jié)
                   2>在系統(tǒng)范圍存在太多的消息
*****************************************************************************************/
#include "typemsg.h"
int MsgSnd(int msqid,char *buf,int len,int flag)
{
    int ret;
    ret=msgsnd(msqid,buf,len,flag);
    if(ret<0)
        perror("msgsnd error");
    system("ipcs -q");
    return ret;
}

int main()
{
    int msqid,len,stype;
    Message msgb;
    memset(&msgb,0,sizeof(Message));
    printf("msgsnd:please input msqid:");
    scanf("%d",&msqid);
    printf("please input msgtype:");
    scanf("%d",&stype);
    msgb.mtype=stype;
    strcpy(msgb.mbuf,"zhangweia");
    MsgSnd(msqid,(char *)&msgb,sizeof(Message),0);
    return 0;
}

4.從隊列中獲取消息 msgrcv

/*********************************************************************
Function:
    int msgrcv(int msqid,const void *ptr,size_t msglen,long type,int flag)
Explain:
    recv message order by type 
    msgrcv error: Argument list too long --> msglen的長度小于消息體中消息的長度
Para :
    ptr: point to message struct 
    msglen: 由ptr指向的緩沖區(qū)中數(shù)據(jù)部分的大小,這個是該函數(shù)能夠返回的最大數(shù)據(jù)量
    type: message type;

              1> 0:返回隊列中最早的消息
              2> 大于0:返回消息隊列中類型為type的第一個消息
              3> 小于0:返回消息隊列中類型小于或者等于type的絕對值的消息類型中最小的第一個消息
    flag: 0<wait> 沒有消息或者消息類型不符合的時候,線程等待
         響應(yīng): 1>有一個所請求類型的消息可以獲取
            2>msqid的消息隊列被系統(tǒng)刪除,返回一個EIDRM
            3>調(diào)用線程被某個捕獲的信號所中斷
       IPC_NOWAIT:在沒有數(shù)據(jù)的情況下,立即返回一個ENOMSG錯誤
       MSGNOERROR:當(dāng)所接受的消息數(shù)據(jù)部分大于msglen長度時,獲取截短的數(shù)據(jù)部分,否則返回E2BIG錯誤
Return:
    message len
*********************************************************************/
#include "typemsg.h"
int MsgRcv(int msqid,char *buf,int msglen,long type,int flag)
{
    int ret;
    ret=msgrcv(msqid,buf,msglen,type,flag);
    if(ret<0)
    perror("msgrcv error");
    system("ipcs -q");
    return ret;

}

int main()
{
    int msqid,len;
    long ttype;
    Message mbuf;
    printf("msgrcv:please input recv msqid:");
    scanf("%d",&msqid);
    MsgRcv(msqid,(char *)&mbuf,8900,0,IPC_NOWAIT);
    printf("recv message=%s/n",mbuf.mbuf);
    Put_String((unsigned char *)&mbuf,sizeof(Message));
    return 0;
}

6.消息隊列的控制msgctl

/**********************************************************
Function:
    int msgctl(int msqid,int cmd,struct msqid_ds *buff)
Explain:
    cdm: IPC_RMID; delete msqid 
       IPC_SET: 
       IPC_STAT: return msqid stat

*********************************************************/
#include "typemsg.h"
int MsgCtl(int msqid,int cmd,struct msqid_ds *buff)
{
    int ret;
    ret=msgctl(msqid,cmd,buff);
    if(ret<0)
    {
        perror("msgctl error");
        return -1;
    }
    return 0;
}

int main()
{
    int msqid,type;
    struct msqid_ds info;
    printf("please input msqid /nand type(1:icp_rmid;2:ipc_stat)");
    scanf("%d%d",&msqid,&type);
    if(type==1)
    {
        MsgCtl(msqid,IPC_RMID,NULL);
        printf("delete queue success:%d/n",msqid);
    }else if(type==2)
    {
        MsgCtl(msqid,IPC_STAT,&info);
        printf("get queue stat:%d/n",msqid);
    }
    return 0;

}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • C/C++字符串函數(shù)之復(fù)制函數(shù)詳解

    C/C++字符串函數(shù)之復(fù)制函數(shù)詳解

    下面小編就為大家?guī)硪黄狢/C++字符串函數(shù)之復(fù)制函數(shù)詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • 帶你了解C++中vector的用法

    帶你了解C++中vector的用法

    大家好,本篇文章主要講的是帶你了解C++中vector的用法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Qt定時器和隨機數(shù)詳解

    Qt定時器和隨機數(shù)詳解

    在前一篇中我們介紹了鍵盤和鼠標事件,其實還有一個非常常用的事件,就是定時器事件,如果要對程序?qū)崿F(xiàn)時間上的控制,那么就要使用到定時器。而隨機數(shù)也是很常用的一個功能,在我們要想產(chǎn)生一個隨機的結(jié)果時就要使用到隨機數(shù)。本文我們就來簡單介紹一下定時器和隨機數(shù)。
    2015-06-06
  • C++實現(xiàn)涂色游戲(博弈)

    C++實現(xiàn)涂色游戲(博弈)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)涂色游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++迭代器和顯示類型轉(zhuǎn)換方式

    C++迭代器和顯示類型轉(zhuǎn)換方式

    這篇文章主要介紹了C++迭代器和顯示類型轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 帶你了解C++this指針的用法及其深究

    帶你了解C++this指針的用法及其深究

    這篇文章主要介紹了C++中this指針的用法,對初學(xué)者而言是非常重要的概念,必須加以熟練掌握,需要的朋友可以參考下,希望能給你帶來幫助
    2021-08-08
  • C++遍歷文件夾下的所有文件

    C++遍歷文件夾下的所有文件

    數(shù)據(jù)分多個文件存儲,讀取數(shù)據(jù)就需要對多個文件進行操作。下面通過實例代碼給大家講解C++遍歷文件夾下的所有文件,感興趣的的朋友一起看看吧
    2017-08-08
  • C++實現(xiàn)日期類的方法詳解

    C++實現(xiàn)日期類的方法詳解

    這篇文章主要給大家介紹了C++實現(xiàn)日期類的方法,文中通過代碼示例給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01
  • 關(guān)于C語言和命令行之間的交互問題

    關(guān)于C語言和命令行之間的交互問題

    這篇文章主要介紹了C語言和命令行之間的交互,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • C++ string 字符串查找匹配實例代碼

    C++ string 字符串查找匹配實例代碼

    下面小編就為大家?guī)硪黄狢++ string 字符串查找匹配實例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10

最新評論