を送りました。 mq_receiveをブロックして、O_NOBLOCKをオンにしないようにします。エラーmq_receiveメッセージ - メッセージなしは、私がメッセージキューと私の最初の練習をやっている
エラーmq_receive方法は戻って、perrorは()された「とは、長すぎるメッセージ」を印刷しています。これは私がメッセージを送ってくれる前です。
ATMは、メッセージを送信します。
void* run_ATM(void* arg) {
int status;
char accountNumber[15];
cout << "ATM is running" << endl;
cout << "Please input an account number > ";
cin >> accountNumber;
status = mq_send(PIN_MSG, accountNumber, sizeof(accountNumber), 1);
}
データベースは
void* run_DB(void* arg){
cout << "Database server running" << endl;
int status;
char received_acct_number[30];
while(1){
status = mq_receive(PIN_MSG, received_acct_number, 100, NULL);
if (status < 0){
perror("error ");
} else {
cout << "received account number\t" << received_acct_number << endl;
}
}
}
これは単なる予備コードでそれらを受け取る - それは最終的に複数の操作を行います。私はちょうど基本的な実例を得たいと思っていました。
EDIT:これを実行するために取得するために必要なその他のコード:
#define PIN_MSG_NAME "/pin_msg"
#define DB_MSG_NAME "/db_msg"
#define MESSAGE_QUEUE_SIZE 15
pthread_t ATM;
pthread_t DB_server;
pthread_t DB_editor;
void* run_ATM(void* arg);
void* run_DB(void* arg);
static struct mq_attr mq_attribute;
static mqd_t PIN_MSG, DB_MSG;
int main(int argc, char const *argv[])
{
pthread_attr_t attr;
mq_attribute.mq_maxmsg = 10; //mazimum of 10 messages in the queue at the same time
mq_attribute.mq_msgsize = MESSAGE_QUEUE_SIZE;
PIN_MSG = mq_open(PIN_MSG_NAME, O_CREAT | O_RDWR, 0666, &mq_attribute);
DB_MSG = mq_open(DB_MSG_NAME, O_CREAT | O_RDWR, 0666, &mq_attribute);
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1024*1024);
long start_arg = 0; //the start argument is unused right now
pthread_create(&ATM, NULL, run_ATM, (void*) start_arg);
pthread_create(&DB_server, NULL, run_DB, (void*) start_arg);
pthread_join(ATM, NULL);
pthread_join(DB_server, NULL);
}
受信バッファは、メッセージキューのサイズよりも大きいので、右、何の問題があってはなりませんか?
でクリーンアップすることで、あなたは私たちに問題を再現するのに十分なコードを与えることができますか?たとえば、 'MESSAGE_QUEUE_SIZE'とは何ですか? 'PIN_MSG'とは何ですか? –