Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/41.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++ SystemV消息队列故障_C++_Linux_Posix_Ipc_Message Queue - Fatal编程技术网

C++ SystemV消息队列故障

C++ SystemV消息队列故障,c++,linux,posix,ipc,message-queue,C++,Linux,Posix,Ipc,Message Queue,我在获取SystemV消息队列设置和在Linux上正常工作时遇到了一些问题。这个想法是让一个中心节点从其他几个节点提取数据。问题在于,中心节点最终坐在那里等待其他节点发送消息。我已经查看了邮箱的值,它们在所有进程中都是相同的。即,中央邮箱为0,其他进程1为32769,等等。我不知道为什么它会失败。我已尝试将msgrcv中的priority参数更改为0以接受所有传入的消息,但出现了相同的问题。任何帮助都是非常感激的。(很抱歉没有评论。) 以下是中心节点的代码: #include <stdio

我在获取SystemV消息队列设置和在Linux上正常工作时遇到了一些问题。这个想法是让一个中心节点从其他几个节点提取数据。问题在于,中心节点最终坐在那里等待其他节点发送消息。我已经查看了邮箱的值,它们在所有进程中都是相同的。即,中央邮箱为0,其他进程1为32769,等等。我不知道为什么它会失败。我已尝试将msgrcv中的priority参数更改为0以接受所有传入的消息,但出现了相同的问题。任何帮助都是非常感激的。(很抱歉没有评论。)

以下是中心节点的代码:

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

struct{
    long priority;
    int temperature;
    int pid;
    int stable;
} msgp;

const int mainMailID = 8484;

using namespace std;

int main(int argc, char* argv[]){

//declare needed variables
int centralMailID;
int externalMailID[4];
int tempdata;
int externalTempature[4];
int externalTemperatureLast[4];

//set initial values for msgp
msgp.priority = 2;
msgp.temperature = atoi(argv[1]);
msgp.pid = 0;
msgp.stable = 0;

//create the central mailbox
centralMailID = msgget(mainMailID, 0600 | IPC_CREAT);

if(centralMailID == -1){
    cout << "Message Queue Creation Failed" << endl;

}
else{
    cout << "Message Queue Created" << endl;
}

//create the external mailboxes
for(int i = 0; i < 4 ; i++){

    externalMailID[i] = msgget(mainMailID + i+1, 0600 | IPC_CREAT);

    if(externalMailID[i] == -1){
        cout << "Message Queue " << i << " Creation Failed" << endl;
    }
    else{
        cout << "Message Queue " << i << " Created" << endl;
    }
}

printf("%i", externalMailID[0]);

while(msgp.stable == 0){

    int centralTemperature = msgp.temperature;

    //get the tempatures from the external sensors.
    for(int i = 0; i<4; i++){

        tempdata = msgrcv(externalMailID[i], &msgp, sizeof(msgp)-sizeof(long), 2, 0);

        cout << "Recived data from sensor " << msgp.pid << endl;

        externalTempature[i] = msgp.temperature;
    }

    if(externalTempature[0] == externalTempature[1] == externalTempature[2] == externalTempature[3] == centralTemperature){

        msgp.stable = 1;
        continue; //could also use break
    }

    int sum = 0;

    for(int i = 0; i<4; i++){

        sum = sum + externalTempature[i];
    }

    centralTemperature = ((2 * centralTemperature) + sum)/6;
    msgp.temperature = centralTemperature;

    for(int i = 0; i<4; i++){

        tempdata = msgsnd(externalMailID[i], &msgp, sizeof(msgp)-sizeof(long), 0);

        printf("Sent data to external mailbox %i", i);
    }
}
printf("Process ended");
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构{
长期优先权;
内部温度;
int-pid;
int稳定;
}msgp;
const int mainMailID=8484;
使用名称空间std;
int main(int argc,char*argv[]){
//声明所需的变量
int centralMailID;
intexternalmailid[4];
int tempdata;
内部外部温度[4];
内部外部温度弹性[4];
//设置msgp的初始值
msgp.priority=2;
msgp.temperature=atoi(argv[1]);
msgp.pid=0;
msgp.stable=0;
//创建中心邮箱
centralMailID=msgget(mainMailID,0600 | IPC|u CREAT);
如果(centralMailID==-1){

cout您正在使用system V api,这可能不是您想要的。有关更多详细信息,请参阅此处:


msgget、msgctl、msgsnd、msgrcv命令是较旧的system V api的一部分,虽然语义相似,但不是posix队列。几个快速的google搜索system V队列教程/示例可能会解决您的问题


如果您真的希望使用posix队列,请切换到mq_open、mq_close、mq_unlink、mq_send、mq_receive、mq_getattr、mq_setattr api并查找有关mq_open、mq_close、mq_unlink、mq_send、mq_receive、mq_getattr api的文档。

请重申给定链接中的要点。仅链接的答案对此没有用处,因为链接往往会腐烂。为什么您认为System V api不是OP想要的?许多大学仍在教授这些api。他明确表示他使用的是posix队列,但他的代码显示的是system V队列。如果他被教授的是posix队列,那么他使用的api是错误的。如果他被教授的是system V队列,那么他问的问题是错误的。我只是按照我的导师和书中所说的去做。他们使用了api以及我正在使用的函数,并将它们称为PSOIX。我想我对它的名称是不正确的,因为我必须使用SystemV。
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <iostream>

struct{
    long priority;
    int temperature;
    int pid;
    int stable;
} msgp;

const int mainMailID = 8484;

using namespace std;

int main(int argc, char* argv[]){


int centralMailID = msgget(mainMailID, 0600 | IPC_CREAT);
int pid = atoi(argv[2]);
int externalMailID = msgget(mainMailID + pid, 0600 | IPC_CREAT);
int externalTemperature = atoi(argv[1]);
int tempdata;

cout << externalMailID << endl;

msgp.priority = 2;

msgp.pid = pid;
msgp.stable = 0;

while(msgp.stable == 0){

    msgp.temperature = externalTemperature;

    tempdata = msgsnd(centralMailID, &msgp, sizeof(msgp)-sizeof(long), 0);

    tempdata = msgrcv(externalMailID, &msgp, sizeof(msgp)-sizeof(long), 2, 0);

    externalTemperature = ((externalTemperature * 3) + (msgp.temperature * 2))/5;

    if(msgp.stable == 1){

        continue;
    }
}

printf("Child Process Ended");
return 0;
}