メッセージキューの仕組みを理解しようとしています。子プロセスが親プロセスにメッセージを送るこの小さなプログラムを作成しました。ほとんどの場合、動作しますが、時々私はエラー:Error parent: No message of desired type
を受け取ります。子プロセスが終了するまで私はwait
にもしようとしましたが、私はまだエラーが発生します。メッセージキューエラー:希望するタイプのメッセージがありません
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
struct msg{
long mtype;
char text[100];
};
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");
if(msgsnd(qid, (void*)&send, strlen(send.text), IPC_NOWAIT)<0){
printf("Error child: ");
}
}
else{
struct msg recieve;
if(msgrcv(qid, (void*)&recieve, 100, 1, IPC_NOWAIT)<0){
perror("Error parent: ");
};
printf("%s\n", recieve.text);
}
return 0;
}
ありがとう。