【发布时间】:2016-12-24 13:46:16
【问题描述】:
我的代码有问题。它应该创建一个消息队列并发送一条消息,而不是等待一段时间让另一个程序接收该消息并回答。问题是,当我运行它时,我在 msgsnd 和 msgrcv 上都得到了一个无效的参数。
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/msg.h>
typedef struct my_msg{
long type;
char text[100];
char sqdr;
}message;
static void score(int messagge_id, char* A_B){
message send;
send.type=1;
strcpy(send.text, "Try to score");
send.sqdr = *A_B;
if((msgsnd(messagge_id, &send, sizeof(send), 0))<0)perror("Error msgsnd\n");
sleep(3);
if((msgrcv(messagge_id, &send, sizeof(send), 4, 0))==-1)perror("Error msgrcv 1\n");
int test=atoi(send.text);
printf("%d\n", test);
}
int main(){
int caso, key;
char team= 'A';
key=1234;
int msg_id=msgget(key, S_IRUSR|S_IWUSR);
printf("Try function score\n");
score(msg_id, &team);
printf("After score\n");
return 0;
}
【问题讨论】:
-
为什么不对
msgget()调用进行错误检查?它成功了吗?是否有其他进程创建了消息队列? -
我会尝试检查 msgget,但我认为问题不在于消息队列的创建,因为我还没有编写第二个程序。会不会是使用的密钥的问题?
标签: c