簡體   English   中英

System V消息隊列故障

[英]System V Message Queue Trouble

我在獲取System V Message Queue設置並在Linux上正常工作時遇到了一些麻煩。 這個想法是讓一個中央節點從其他幾個節點提取數據。 問題在於中央節點最終坐在那里等待其他節點發送消息。 我查看了郵箱的值,它們在所有進程中都是相同的。 IE 0(用於中央郵箱),32769(用於其他進程1,等等)。 我不知道為什么它看起來會失敗。 我試圖將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;
}

這是其他節點的代碼:

#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;
}

您正在使用系統V api,這可能不是您想要的。 請參閱此處以獲取更多詳細信息:

http://mij.oltrelinux.com/devel/unixprg/#ipc__posix_msgqs


msgget,msgctl,msgsnd和msgrcv命令是較舊的系統V api的一部分,盡管語義相似,但它們不是posix隊列。 在Google上快速搜索系統V隊列教程/示例,可能會解決您的問題。

如果您確實希望使用posix隊列,請切換至並在mq_open,mq_close,mq_unlink,mq_send,mq_receive,mq_getattr,mq_setattr api上查找文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM