Posixメッセージキューの作成に関する問題


次のコードがあります.
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define MQ_DATA_FILE "/tmp/tmp_mq_ipc_file"

int main(int argc, char **argv)
{
    mqd_t mq_list;
    struct mq_attr qu_attr;
    int flags = O_RDWR | O_NONBLOCK | O_CREAT | O_EXCL;
    mode_t mode  = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

    mq_list = mq_open(MQ_DATA_FILE, flags, mode, NULL);
    if (mq_list == -1)
    {
        printf("create mq failed, errno:%d,%s
", errno, strerror(errno)); exit(EXIT_FAILURE); } printf("MQ creat succeed!
"); memset(&qu_attr, 0, sizeof(struct mq_attr)); if (mq_getattr(mq_list, &qu_attr) == -1) { printf("get mqueue attr failed,errno:%d,%s
", errno, strerror(errno)); //exit(EXIT_FAILURE); } else { printf("mq_attr.mq_flags:%ld
", qu_attr.mq_flags); printf("mq_attr.mq_maxmsg:%ld
", qu_attr.mq_maxmsg); printf("mq_attr.mq_msgsize:%ld
", qu_attr.mq_msgsize); printf("mq_attr.mq_curmsgs:%ld
", qu_attr.mq_curmsgs); } mq_close(mq_list); mq_unlink(MQ_DATA_FILE); return EXIT_SUCCESS; }

まず、gcc-o test testを使うかもしれません.cこのウィジェットをコンパイルする
しかし、おかしいです.関連するmq関数のエラー:undefined reference
man発見したら、赤いフォントに注意してください.
---------------------------------------------------------------------------------------------------------------------------------------------
MQ_OPEN(3)                             Linux Programmer's Manual                             MQ_OPEN(3) NAME        mq_open - open a message queue SYNOPSIS        #include          /* For O_* constants */       #include        /* For mode constants */       #include        mqd_t mq_open(const char *name, int oflag);        mqd_t mq_open(const char *name, int oflag, mode_t mode,                      struct mq_attr *attr);        Link with -lrt. -----------------------------------------------------------------------------------------------------------------------------------------------
はい、コンパイル中に「-lrt」を付けるので、コンパイルのすべてのコマンドはgcc-o test testであるべきです.c -lrt
次に、作成エラーのロゴを印刷しましたね~!
create mq failed, errno:13,Permission denied
どうして?調べてみると(赤いフォントに注意):
-----------------------------------------------------------------------------------------------------------------------------------------------
MQ_OPEN(3)                             Linux Programmer's Manual                             MQ_OPEN(3)NAME       mq_open - open a message queueSYNOPSIS       #include          /* For O_* constants */       #include        /* For mode constants */       #include        mqd_t mq_open(const char *name, int oflag);       mqd_t mq_open(const char *name, int oflag, mode_t mode,                     struct mq_attr *attr);       Link with -lrt.DESCRIPTION       mq_open() creates a new POSIX message queue or opens an existing queue.  The queue is identified       by name.  For details of the construction of name, see mq_overview(7).
---------------------------------------------------------------------------------------------------------------------------------------------------
再び、man mq_overview、これはposixメッセージキューの全体的な紹介であり、その一部を抜粋する必要があります.
DESCRIPTION        POSIX message queues allow processes to exchange data in the form of messages.  This API is dis‐        tinct from that provided by System V message queues (msgget(2), msgsnd(2), msgrcv(2), etc.), but        provides similar functionality.        Message  queues  are  created and opened using mq_open(3); this function returns a message queue        descriptor (mqd_t), which is used to refer to the open message queue in later calls.  Each  mes‐        sage  queue  is identified by a name of the form/somename; that is, a null-terminated string of        up to NAME_MAX (i.e., 255) characters consisting of an initial slash, followed by  one  or  more        characters,  none  of which are slashes.  Two processes can operate on the same queue by passing        the same name to mq_open(3).
以上の赤いタグの文字、このnameパラメータは非現実的なファイルであるべきで、作成する必要はなく、特定のフォーマットを持っています.
nullで終わる文字列からNAME_MAX(255)文字からなる初期スラッシュには、1つ以上の文字が加算され、スラッシュは1つもありません.
上記のプログラムを成功させるには、マクロをdefine MQ_に変更する必要があります.DATA_FILE "/tmp_mq_ipc_file" .