Search code examples
cmultithreadingmsgrcv

Message Type is different from the one sent - msgsnd/msgrcv C


I'm studying Operative Systems basics for my exam and I've faced a strange problem. I'm currently working on send/receive function. Suppose I have 3 main program Client, which thanks to msgsnd() primitive send a message. The struct is the following:

typedef struct{
    long msg_typ; //Required
    int msg[2]; //2 random numbers
} message;

Then I have 2 threads which should catch the message sent by Clients thanks to msgrcv(). Everything should happen after RTS and OTS messages (OK TO SEND, READY TO SEND). Send should be syncronous. When a Thread receive the RTS Message, it should receive only from the sender Client. This brought me to set message field msg_typ to Client Pid, and call the msgrcv() function with this prototype:

msgrcv(queques[2], &mrts, sizeof(message)-sizeof(long), 0, 0);

Supposing queque is the id of the RTS queque, msg_typ parameter is set 0 in order for receiving the first message of the queque, then thanks to message field msg_typ the Thread recover the PID of the Client.

However, at runtime, message field msg_typ is different from the one sent, even though I'm sure the thread is actually receiving the same message:

Client n. 4761: Seding RTS Message. PID: 4761.
...
Thread n. 1 of Server pid 4763: RTS Message received. PID: 1806. Sending OTS Message. //The pid is different!
Thread n. 0 of Server pid 4763: OTS Message sent. PID: 1806. Receiving Message.

For a better understanding, these are the procedures I've used:

void send_sync(message* m, int* queques, char* who){
    message mots, mrts;
    int pid = getpid();
    mrts.msg_typ = pid;
    printf("%s: Sending of RTS Message. PID: %ld.\n", who, mrts.msg_typ);
    msgsnd(queques[2], &mrts, sizeof(message)-sizeof(long), 0);
    printf("%s: RTS Message sent. PID: %ld. Waiting for OTS Message.\n", who, mrts.msg_typ);
    msgrcv(queques[1], &mots, sizeof(message)-sizeof(long), pid, 0);
    printf("%s: OTS Message received. PID: %ld. Sending Message. \n", who, mots.msg_typ);
    m -> msg_typ = pid;
    msgsnd(queques[0], m, sizeof(message)-sizeof(long), 0);
    printf("%s: Message sent. PID: %d.", who, pid);
}

void rcv_sync_f(message* m, int* queques, char* who){
    message mots, mrts;
    printf("%s: Receiving RTS Message. PID: NOT SET YET.\n", who);
    msgrcv(queques[2], &mrts, sizeof(message)-sizeof(long), 0, 0);
    printf("%d\n", queques[2]);
    printf("%s: RTS Message received. PID: %ld. Sending OTS Message.\n", who, mrts.msg_typ);
    mots.msg_typ = mrts.msg_typ;
    msgsnd(queques[1], &mots, sizeof(message)-sizeof(long), 0);
    printf("%s: OTS Message sent. PID: %ld. Receiving Message.\n", who, mots.msg_typ);
    msgrcv(queques[0], m, sizeof(message)-sizeof(long), mots.msg_typ, 0);
    printf("%s: Message ricevuto. PID: %ld.", who, m -> msg_typ);
}

Where:

  • whois a string which I use to identify the Thread/Client which is calling the function;
  • quequesis an array with queques for OTS , RTS and Message queques.

I have no more ideas. Do you know the reason? Thank you in advance.


Solution

  • Actually, the error was hidden somewhere else. In the makefile of my program, I forgot to add the keyword -pthread for compiling; being on Mac platform, probably GCC compiled the program using different libraries. Thank you anyway for your support <3