国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩


linux 消息隊(duì)列

分享到:
           

    本文關(guān)鍵字: 消息隊(duì)列,linux 消息隊(duì)列

    顧名思義,消息隊(duì)列就是一些消息的列表,用戶可以在消息隊(duì)列中添加消息和讀取消息等。從這點(diǎn)上看,消息隊(duì)列具有一定的FIFO特性,但是它可以實(shí)現(xiàn)消息的隨機(jī)查詢,比FIFO具有更大的優(yōu)勢(shì)。同時(shí),這些消息又是存在于內(nèi)核中的,由“隊(duì)列ID”來(lái)標(biāo)識(shí)。

    消息隊(duì)列的實(shí)現(xiàn)包括創(chuàng)建或打開(kāi)消息隊(duì)列、添加消息、讀取消息和控制消息隊(duì)列4種操作,其中創(chuàng)建或打開(kāi)消息隊(duì)列使用的函數(shù)是msgget(),這里創(chuàng)建的消息隊(duì)列的數(shù)量會(huì)受到系統(tǒng)消息隊(duì)列數(shù)量的限制;添加消息使用的函數(shù)是msgsnd(),它把消息添加到已打開(kāi)的消息隊(duì)列末尾;讀取消息使用的函數(shù)是msgrcv(),它把消息從消息隊(duì)列中取走,與FIFO不同的是,這里可以取走指定的某一條消息;控制消息隊(duì)列使用的函數(shù)是msgctl(),它可以完成多項(xiàng)功能。

    表1列舉了msgget()函數(shù)的語(yǔ)法要點(diǎn)。

表1 msgget()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函數(shù)原型 int msgget(key_t key, int msgflg)
函數(shù)傳入值 key:消息隊(duì)列的鍵值,多個(gè)進(jìn)程可以通過(guò)它訪問(wèn)同一個(gè)消息隊(duì)列,其中有個(gè)特殊值IPC_PRIVATE,用于創(chuàng)建當(dāng)前進(jìn)程的私有消息隊(duì)列
msgflg:權(quán)限標(biāo)志位
函數(shù)返回值 成功:消息隊(duì)列ID
出錯(cuò):-1

    表2列舉了msgsnd()函數(shù)的語(yǔ)法要點(diǎn)。

表2 msgsnd()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函數(shù)原型 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)
函數(shù)傳入值 msqid:消息隊(duì)列的隊(duì)列ID
msgp:指向消息結(jié)構(gòu)的指針,該消息結(jié)構(gòu)msgbuf通常如下。
struct msgbuf
{
  long mtype; /* 消息類型,該結(jié)構(gòu)必須從這個(gè)域開(kāi)始 */
  char mtext[1]; /* 消息正文 */
}
msgsz:消息正文的字節(jié)數(shù)(不包括消息類型指針變量)
msgflg IPC_NOWAIT:若消息無(wú)法立即發(fā)送(如當(dāng)前消息隊(duì)列已滿),函數(shù)會(huì)立即返回
0:msgsnd調(diào)用阻塞直到發(fā)送成功為止
函數(shù)返回值 成功:0
出錯(cuò):-1

    表3列舉了msgrcv()函數(shù)的語(yǔ)法要點(diǎn)。

表3 msgrcv()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函數(shù)原型 int msgrcv(int msgid, void *msgp, size_t msgsz, long int msgtyp, int msgflg)
函數(shù)傳入值 msqid:消息隊(duì)列的隊(duì)列ID
msgp:消息緩沖區(qū),同msgsnd()函數(shù)的msgp
msgsz:消息正文的字節(jié)數(shù)(不包括消息類型指針變量)
msgtyp 0:接收消息隊(duì)列中第一個(gè)消息
大于0:接收消息隊(duì)列中第一個(gè)類型為msgtyp的消息
函數(shù)傳入值 小于0:接收消息隊(duì)列中第一個(gè)類型值不小于msgtyp絕對(duì)值且類型值小的消息
msgflg MSG_NOERROR:若返回的消息比msgsz字節(jié)多,則消息就會(huì)截短到msgsz字節(jié),且不通知消息發(fā)送進(jìn)程
IPC_NOWAIT:若在消息隊(duì)列中并沒(méi)有相應(yīng)類型的消息可以接收,則函數(shù)立即返回
0:msgsnd()調(diào)用阻塞直到接收一條相應(yīng)類型的消息為止
函數(shù)返回值 成功:0
出錯(cuò):-1

    表4列舉了msgctl()函數(shù)的語(yǔ)法要點(diǎn)。

表4 msgctl()函數(shù)語(yǔ)法要點(diǎn)

所需頭文件 #include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
函數(shù)原型 int msgctl (int msgqid, int cmd, struct msqid_ds *buf )
函數(shù)傳入值 msqid:消息隊(duì)列的隊(duì)列ID
cmd:命令參數(shù) IPC_STAT:讀取消息隊(duì)列的數(shù)據(jù)結(jié)構(gòu)msqid_ds,并將其存儲(chǔ)在buf指定的地址中
IPC_SET:設(shè)置消息隊(duì)列的數(shù)據(jù)結(jié)構(gòu)msqid_ds中的ipc_perm域(IPC操作權(quán)限描述結(jié)構(gòu))值,這個(gè)值取自buf參數(shù)
IPC_RMID:從系統(tǒng)內(nèi)核中刪除消息隊(duì)列
buf:描述消息隊(duì)列的msqid_ds結(jié)構(gòu)類型變量
函數(shù)返回值 成功:0
出錯(cuò):-1

    下面的實(shí)例體現(xiàn)了如何使用消息隊(duì)列進(jìn)行兩個(gè)進(jìn)程(發(fā)送端和接收端)之間的通信,包括消息隊(duì)列的創(chuàng)建、消息發(fā)送與讀取、消息隊(duì)列的撤銷和刪除等多種操作。

    消息發(fā)送端進(jìn)程和消息接收端進(jìn)程間不需要額外實(shí)現(xiàn)進(jìn)程間的同步。在該實(shí)例中,發(fā)送端發(fā)送的消息類型設(shè)置為該進(jìn)程的進(jìn)程號(hào)(可以取其他值),因此接收端根據(jù)消息類型來(lái)確定消息發(fā)送者的進(jìn)程號(hào)。注意這里使用了fotk()函數(shù),它可以根據(jù)不同的路徑和關(guān)鍵字產(chǎn)生標(biāo)準(zhǔn)的key。消息隊(duì)列發(fā)送端的代碼如下:

    /* msgsnd.c */
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define BUFFER_SIZE 512


    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;

        /* 根據(jù)不同的路徑和關(guān)鍵字產(chǎn)生標(biāo)準(zhǔn)的key */
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /* 創(chuàng)建消息隊(duì)列 */
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d\n",qid);
        while(1)
        {
            printf("Enter some message to the queue:");
            if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
            {
                puts("no message");
                exit(1);
            }

            msg.msg_type = getpid();
            /* 添加消息到消息隊(duì)列 */
            if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
            {
                perror("message posted");
                exit(1);
            }
            if (strncmp(msg.msg_text, "quit", 4) == 0)
            {
                break;
            }
        }
        exit(0);
    }

    消息隊(duì)列接收端的代碼如下:

    /* msgrcv.c */
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/msg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define BUFFER_SIZE 512

    struct message
    {
        long msg_type;
        char msg_text[BUFFER_SIZE];
    };
    int main()
    {
        int qid;
        key_t key;
        struct message msg;

        /* 根據(jù)不同的路徑和關(guān)鍵字產(chǎn)生標(biāo)準(zhǔn)的key */
        if ((key = ftok(".", 'a')) == -1)
        {
            perror("ftok");
            exit(1);
        }
        /* 創(chuàng)建消息隊(duì)列 */
        if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
        {
            perror("msgget");
            exit(1);
        }
        printf("Open queue %d\n", qid);
        do
        {
            /* 讀取消息隊(duì)列 */
            memset(msg.msg_text, 0, BUFFER_SIZE);
            if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0)
            {
                perror("msgrcv");
                exit(1);
            }
            printf("The message from process %d : %s", msg.msg_type, msg.msg_text);

        } while(strncmp(msg.msg_text, "quit", 4));
        /* 從系統(tǒng)內(nèi)核中移走消息隊(duì)列 */
        if ((msgctl(qid, IPC_RMID, NULL)) < 0)
        {
            perror("msgctl");
            exit(1);
        }
        exit(0);
    }

    以下是程序的運(yùn)行結(jié)果,輸入“quit”則兩個(gè)進(jìn)程都將結(jié)束。

    $ ./msgsnd
    Open queue 327680
    Enter some message to the queue:first message
    Enter some message to the queue:second message
    Enter some message to the queue:quit
    $ ./msgrcv
    Open queue 327680
    The message from process 6072 : first message
    The message from process 6072 : second message
    The message from process 6072 : quit

    本文選自華清遠(yuǎn)見(jiàn)嵌入式培訓(xùn)教材《從實(shí)踐中學(xué)嵌入式Linux應(yīng)用程序開(kāi)發(fā)》

   熱點(diǎn)鏈接:

   1、linux 共享內(nèi)存
   2、linux下的信號(hào)量
   3、linux下的信號(hào)處理實(shí)例
   4、信號(hào)處理函數(shù)signal()和信號(hào)集函數(shù)組
   5、信號(hào)捕捉函數(shù)alarm()和pause()

更多新聞>>