我的程序一直在等待,甚至不显示第一个 printf

发布于 2024-12-14 03:59:37 字数 915 浏览 1 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-12-21 03:59:37

因为你没有刷新缓冲区。 stdout 可能在您的实现中进行了行缓冲。

尝试:

printf("i am here\n");
                  ^^

或者

printf("i am here");
fflush(stdout);

Because you are not flusing the buffer. It's likely stdout is line buffered in your implementation.

Try:

printf("i am here\n");
                  ^^

Or

printf("i am here");
fflush(stdout);
单挑你×的.吻 2024-12-21 03:59:37

您还可以使用 strace 来了解程序正在执行哪些系统调用。

You could also use strace to understand what system calls a program is doing.

等待圉鍢 2024-12-21 03:59:37

这与您的具体问题无关,但您确实应该更好地处理 msgget/msgrcv 错误。这两个函数在失败时都会设置 errno,您可以使用它向用户提供有关错误性质的有用信息。就这样写:

if( status < 0 ) {
    perror( "msgrcv" );
    exit( EXIT_FAILURE );
}

perror是从errno获取信息的最简单的方法。如果您想要更复杂的错误消息,可以使用 strerror。例如,

fprintf( stderr, "Some error message: %s\n", strerror( errno ));

无论哪种情况,错误消息都会发送到 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:

if( status < 0 ) {
    perror( "msgrcv" );
    exit( EXIT_FAILURE );
}

perror is the simplest way to get information from errno. If you want a more sophisticated error message, you can use strerror. For example,

fprintf( stderr, "Some error message: %s\n", strerror( errno ));

In either case, the error message goes to stderr, rather than stdout. You do not want error messages going to stdout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文