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

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

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

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

1.消息隊(duì)列結(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)建消息隊(duì)列:

   /***************************************************
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.向消息隊(duì)列中發(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標(biāo)識(shí)的消息隊(duì)列從系統(tǒng)中刪除(返回EIDRM錯(cuò)誤)
                   3>調(diào)用線程被某個(gè)捕獲的信號(hào)所中斷(返回EINTR錯(cuò)誤)
       IPC_NOWAIT:如果沒有存放新消息的空間,函數(shù)馬上返回
                   1>指定的隊(duì)列中有太多的字節(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.從隊(duì)列中獲取消息 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的長(zhǎng)度小于消息體中消息的長(zhǎng)度
Para :
    ptr: point to message struct 
    msglen: 由ptr指向的緩沖區(qū)中數(shù)據(jù)部分的大小,這個(gè)是該函數(shù)能夠返回的最大數(shù)據(jù)量
    type: message type;

              1> 0:返回隊(duì)列中最早的消息
              2> 大于0:返回消息隊(duì)列中類型為type的第一個(gè)消息
              3> 小于0:返回消息隊(duì)列中類型小于或者等于type的絕對(duì)值的消息類型中最小的第一個(gè)消息
    flag: 0<wait> 沒有消息或者消息類型不符合的時(shí)候,線程等待
         響應(yīng): 1>有一個(gè)所請(qǐng)求類型的消息可以獲取
            2>msqid的消息隊(duì)列被系統(tǒng)刪除,返回一個(gè)EIDRM
            3>調(diào)用線程被某個(gè)捕獲的信號(hào)所中斷
       IPC_NOWAIT:在沒有數(shù)據(jù)的情況下,立即返回一個(gè)ENOMSG錯(cuò)誤
       MSGNOERROR:當(dāng)所接受的消息數(shù)據(jù)部分大于msglen長(zhǎng)度時(shí),獲取截短的數(shù)據(jù)部分,否則返回E2BIG錯(cuò)誤
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.消息隊(duì)列的控制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;

}

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

相關(guān)文章

  • C語言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法

    C語言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法

    這篇文章主要介紹了C語言的isatty函數(shù)和ttyname函數(shù)以及sendmsg函數(shù)用法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C++使用初始化列表的方式來初始化字段的方法

    C++使用初始化列表的方式來初始化字段的方法

    今天小編就為大家分享一篇關(guān)于C++使用初始化列表的方式來初始化字段的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C語言簡(jiǎn)明講解類型轉(zhuǎn)換的使用與作用

    C語言簡(jiǎn)明講解類型轉(zhuǎn)換的使用與作用

    類型轉(zhuǎn)換(type?cast),是高級(jí)語言的一個(gè)基本語法。它被實(shí)現(xiàn)為一個(gè)特殊的運(yùn)算符,以小括號(hào)內(nèi)加上類型名來表示,接下來讓我們一起來詳細(xì)了解
    2022-04-04
  • 詳解樹形DP

    詳解樹形DP

    樹形DP是什么?跟其他DP有什么區(qū)別?相信很多初學(xué)者在剛剛接觸一種新思想的時(shí)候都會(huì)有這種問題。沒錯(cuò),樹形DP準(zhǔn)確的說是一種DP的思想,將DP建立在樹狀結(jié)構(gòu)的基礎(chǔ)上。所以我們結(jié)合具體題目進(jìn)行講解,希望大家可以在題目中領(lǐng)悟這種思想。
    2021-05-05
  • C語言實(shí)現(xiàn)2D賽車游戲的示例代碼

    C語言實(shí)現(xiàn)2D賽車游戲的示例代碼

    此游戲是《2D 賽車》的”魔改版“——2.5D 雙人賽車!原作實(shí)現(xiàn)了 2D 視角的賽車游戲,但是我覺得不夠真實(shí)、操縱感不強(qiáng),故擠出數(shù)個(gè)周末完成了這個(gè)”魔改版“,實(shí)現(xiàn)了第一人稱的視角,希望大家喜歡
    2022-12-12
  • 一文帶你了解C語言中的0長(zhǎng)度數(shù)組(可變數(shù)組/柔性數(shù)組)

    一文帶你了解C語言中的0長(zhǎng)度數(shù)組(可變數(shù)組/柔性數(shù)組)

    眾所周知,?GNU/GCC?在標(biāo)準(zhǔn)的?C/C++?基礎(chǔ)上做了有實(shí)用性的擴(kuò)展,?零長(zhǎng)度數(shù)組(Arrays?of?Length?Zero)?就是其中一個(gè)知名的擴(kuò)展,本文就來聊聊零長(zhǎng)度數(shù)組的相關(guān)知識(shí)吧
    2023-03-03
  • C++實(shí)現(xiàn)LeetCode(28.實(shí)現(xiàn)strStr()函數(shù))

    C++實(shí)現(xiàn)LeetCode(28.實(shí)現(xiàn)strStr()函數(shù))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(28.實(shí)現(xiàn)strStr()函數(shù)),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C語言中預(yù)處理命令的使用

    C語言中預(yù)處理命令的使用

    C語言預(yù)處理是編程中非常重要的一個(gè)環(huán)節(jié),通過預(yù)處理指令和預(yù)處理器的一些特性,本文主要介紹了C語言中預(yù)處理命令的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • C++中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方式

    C++中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方式

    使用C++編程發(fā)送HTTP請(qǐng)求通常需要使用第三方的HTTP庫或框架,在C++中,有幾個(gè)受歡迎的HTTP庫可供選擇,例如Curl、Boost.Beast和cpp-httplib,另外,也可以自己實(shí)現(xiàn)socket來發(fā)送http請(qǐng)求,需要的朋友可以參考下
    2024-04-04
  • 詳解如何在C/C++中測(cè)量一個(gè)函數(shù)或功能的運(yùn)行時(shí)間

    詳解如何在C/C++中測(cè)量一個(gè)函數(shù)或功能的運(yùn)行時(shí)間

    本文算是一個(gè)比較完整的關(guān)于在 C/C++ 中測(cè)量一個(gè)函數(shù)或者功能的總結(jié),最后會(huì)演示三種方法的對(duì)比,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12

最新評(píng)論