Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 消息队列就是这样工作的吗?_C_Unix_Message Queue - Fatal编程技术网

C 消息队列就是这样工作的吗?

C 消息队列就是这样工作的吗?,c,unix,message-queue,C,Unix,Message Queue,我试图理解消息队列。在我看到的示例中,msgstrunct除了第一个属性(类型)必须是long之外,只有一个属性。因此,它类似于structmsg{long mtype;char text[100]} 我试图添加一个新的int属性,x,看看我是否同时收到了文本和数字,并且效果良好 消息队列就是这样工作的吗?在我的struct中可以有我想要的任意多个属性吗 另外,调用长度参数设置为sizeof(send)-sizeof(send.x)的msgrcv和msgsnd函数是否可以,因为我知道结构的大小并

我试图理解消息队列。在我看到的示例中,
msg
strunct除了第一个属性(类型)必须是
long
之外,只有一个属性。因此,它类似于
structmsg{long mtype;char text[100]}

我试图添加一个新的
int
属性,
x
,看看我是否同时收到了文本和数字,并且效果良好

消息队列就是这样工作的吗?在我的
struct
中可以有我想要的任意多个属性吗

另外,调用长度参数设置为
sizeof(send)-sizeof(send.x)
msgrcv
msgsnd
函数是否可以,因为我知道结构的大小并不总是与每个属性的
sizeof
之和相同? 多谢各位

int main(){

    struct msg{
        long mtype;
        char text[100];
        int x;
    };

    int key = ftok(".", 10);
    int qid = msgget(key, 0666|IPC_CREAT);

    int pid = fork();

    if(pid == 0){
        struct msg send;
        send.mtype = 1;
        strcpy(send.text, "hello");
        send.x = 99;
        if(msgsnd(qid, (void*)&send, sizeof(send) - sizeof(send.x), 0)<0){
             printf("Error child: ");
        }
    }
    else{
        struct msg recieve;
        if(msgrcv(qid, (void*)&recieve, sizeof(recieve) - sizeof(recieve.x), 1, 0)<0){
             perror("Error parent: ");
        };
        printf("text: %s\nnumber: %d", recieve.text, recieve.x);
    }

    return 0;
}
intmain(){
结构味精{
长型;
字符文本[100];
int x;
};
int key=ftok(“.”,10);
int qid=msgget(键,0666 | IPC|U CREAT);
int-pid=fork();
如果(pid==0){
结构消息发送;
send.mtype=1;
strcpy(send.text,“hello”);
send.x=99;
if(msgsnd(qid,(void*)和send,sizeof(send)-sizeof(send.x),0)
由于
msgp
参数被声明为
const void*
,因此您可以使用所需的任何数据类型。没有任何规定它必须是一个
struct
,只有
long
char[]
。这意味着您可以执行
sizeof(send)
。您不需要为要发送的额外结构成员进行调整。事实上,这样做会导致问题,因为整个结构将无法处理。唯一重要的是
msgrcv()
使用与前面的
msgsnd()
相同的结构。请参阅

由于
msgp
参数被声明为
const void*
,因此您可以使用所需的任何数据类型。没有任何规定它必须是一个
struct
,只有
long
char[]
。这意味着您可以执行
sizeof(send)
。您不需要为正在发送的额外结构成员进行调整。事实上,这样做会导致问题,因为整个结构将无法处理。唯一重要的是
msgrcv()
使用与前面的
msgsnd()
相同的结构。请参阅。

中的,在:

msgp
定义为:

msgp参数是指向调用程序定义的结构的指针 一般形式如下:

      struct msgbuf {
           long mtype;       /* message type, must be > 0 */
           char mtext[1];    /* message data */
       };
粗体的是我的

这里主要的一点是结构是调用方定义的。因此,只要输入结构(由
msgsnd
发送)和输出结构(由
msgrcv
接收)是相同的,那么
mtype
后面的数据可以是您想要的任何数据(只要您正确指定大小)。对于您的情况,您只需要:

msgsnd(qid, (void*)&send, sizeof(send) - sizeof(send.mtype), 0)

从,在:

msgp
定义为:

msgp参数是指向调用程序定义的结构的指针 一般形式如下:

      struct msgbuf {
           long mtype;       /* message type, must be > 0 */
           char mtext[1];    /* message data */
       };
粗体的是我的

这里主要的一点是结构是调用方定义的。因此,只要输入结构(由
msgsnd
发送)和输出结构(由
msgrcv
接收)是相同的,那么
mtype
后面的数据可以是您想要的任何数据(只要您正确指定大小)。对于您的情况,您只需要:

msgsnd(qid, (void*)&send, sizeof(send) - sizeof(send.mtype), 0)


char[]只是一个占位符,在所需的长mtype字段之后,可以在结构中包含任何内容。msgsnd()调用的大小不包括mtype

你几乎说对了

以下是一个工作版本:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int main(void){

    struct msg {
        long mtype;
        char text[100];
        int x;
    };

    size_t sz = sizeof(struct msg) - sizeof(long);  <=== /* SIZE */
    int key = ftok(".", 10);
    int qid = msgget(key, 0666|IPC_CREAT);

    int pid = fork();

    if (pid == 0){
        struct msg send;
        send.mtype = 1;
        strcpy(send.text, "hello");
        send.x = 99;
        if (msgsnd(qid, (void*)&send, sz, 0)<0){
             perror("Error child: ");
        }
    } else {
        struct msg recieve;
        if(msgrcv(qid, (void*)&recieve, sz, 1, 0)<0){
             perror("Error parent: ");
        };
        printf("text: %s\nnumber: %d\n", recieve.text, recieve.x);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
内部主(空){
结构味精{
长型;
字符文本[100];
int x;
};

size_t sz=sizeof(struct msg)-sizeof(long);字符[]只是一个占位符,在所需的long mtype字段之后,可以在结构中拥有任何需要的内容。msgsnd()调用的大小不包括mtype

你几乎说对了

以下是一个工作版本:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int main(void){

    struct msg {
        long mtype;
        char text[100];
        int x;
    };

    size_t sz = sizeof(struct msg) - sizeof(long);  <=== /* SIZE */
    int key = ftok(".", 10);
    int qid = msgget(key, 0666|IPC_CREAT);

    int pid = fork();

    if (pid == 0){
        struct msg send;
        send.mtype = 1;
        strcpy(send.text, "hello");
        send.x = 99;
        if (msgsnd(qid, (void*)&send, sz, 0)<0){
             perror("Error child: ");
        }
    } else {
        struct msg recieve;
        if(msgrcv(qid, (void*)&recieve, sz, 1, 0)<0){
             perror("Error parent: ");
        };
        printf("text: %s\nnumber: %d\n", recieve.text, recieve.x);
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
内部主(空){
结构味精{
长型;
字符文本[100];
int x;
};

size_t sz=sizeof(struct msg)-sizeof(long);char[]
只是一个占位符,您可以在所需的
long mtype
字段之后的结构中拥有任何您想要的内容
调用不包括
mtype
@JohnHascall是否需要
mtype
?是的,对于消息类型,结构必须以
long
开头。您不必调用它
mtype
,但这是正常做法。
char[]
只是一个占位符,在所需的
long mtype
字段之后,您可以在结构中拥有所需的任何内容
call不包括
mtype
@JohnHascall是否需要
mtype
?是的,对于消息类型,结构必须以
long
开头。您不必调用它
mtype
,但这是正常的做法。谢谢您的回答,但我看不出您的版本和我的版本之间的区别。您的大小of(struct msg)-sizeof(long)相当于我的sizeof(recieve)-sizeof(recieve.x)。我缺少什么吗?它只是数字上的“等效”如果
int
long
大小相同。从语义上讲,减去
mtype
的大小是正确的。从要发送的某个字段中减去(如
x
)是不正确的。谢谢你的回答,但我看不出你的版本和我的版本之间的区别。你的大小\u t sz=sizeof(struct msg)-sizeof(long)相当于我的sizeof(reci