我的程序一直在等待,甚至不显示第一个 printf
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/shm.h>
typedef struct{long type;char resultado[10];} MsgAnswerLoginStruct;
typedef struct{long tipo;int meupid;char login[20];char password[20];}MsgReqLoginStruct;
main(){
printf("i am here");
int msg_id, status;
MsgReqLoginStruct msg;
MsgAnswerLoginStruct msg2;
msg_id = msgget(2000, 0600 | IPC_CREAT);
if(msg_id == -1){
printf("erro\n");
exit(1);
}
status = msgrcv(msg_id, &msg, sizeof(msg) - sizeof(long) , 1, 0);
if(status < 0){
printf("erro2\n");
exit(1);
}
printf("Tentativa de Autenticação de PID = %d\n", msg.meupid);
}
这是我的问题,这是一个从 IPC 接收消息的程序,但即使我尝试从另一个进程发送消息,他仍然在等待。 我把那个 printf 进行调试..它没有出现在我的控制台上?!为什么 ? 当我运行程序时它只是一直等待.. 提前谢谢大家!!
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/shm.h>
typedef struct{long type;char resultado[10];} MsgAnswerLoginStruct;
typedef struct{long tipo;int meupid;char login[20];char password[20];}MsgReqLoginStruct;
main(){
printf("i am here");
int msg_id, status;
MsgReqLoginStruct msg;
MsgAnswerLoginStruct msg2;
msg_id = msgget(2000, 0600 | IPC_CREAT);
if(msg_id == -1){
printf("erro\n");
exit(1);
}
status = msgrcv(msg_id, &msg, sizeof(msg) - sizeof(long) , 1, 0);
if(status < 0){
printf("erro2\n");
exit(1);
}
printf("Tentativa de Autenticação de PID = %d\n", msg.meupid);
}
here is my problem, this is a program that receives messages from the IPC, but he keeps waiting even when I am trying to send from another process..
And I put that printf to debug.. It doesnt appear on my console?! why ?
when I run program it just keeps waiting..
Thanks in advance guys!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为你没有刷新缓冲区。
stdout
可能在您的实现中进行了行缓冲。尝试:
或者
Because you are not flusing the buffer. It's likely
stdout
is line buffered in your implementation.Try:
Or
您还可以使用
strace
来了解程序正在执行哪些系统调用。You could also use
strace
to understand what system calls a program is doing.这与您的具体问题无关,但您确实应该更好地处理 msgget/msgrcv 错误。这两个函数在失败时都会设置 errno,您可以使用它向用户提供有关错误性质的有用信息。就这样写:
perror是从errno获取信息的最简单的方法。如果您想要更复杂的错误消息,可以使用 strerror。例如,
无论哪种情况,错误消息都会发送到 stderr,而不是 stdout。您不希望错误消息进入标准输出。
This is unrelated to your specific question, but you should really handle msgget/msgrcv errors better. Both functions set errno when they fail, and you can use that to provide useful information to the user about the nature of the error. Just write:
perror is the simplest way to get information from errno. If you want a more sophisticated error message, you can use strerror. For example,
In either case, the error message goes to stderr, rather than stdout. You do not want error messages going to stdout.