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);

}

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!!

3 Answers 3

1

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);
1
  • GUYS FOR GOD SAKE! You are amazing here on this website!! You guys answer in less than 5 minutes! JEZ! Thanks alot mate! You solved my issue! Great community here on stackoverflow! Just saying ! Bye and thanks ;)
    – TiagoM
    Nov 5, 2011 at 19:21
0

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

0
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.