C Posix - msgsnd() msgrcv

Hey guys, Im doing message passing for the first time on a linux OS. Im new to C programming, so bear with me. I made two .c files : central.c and external.c

I simply wanted to pass a message from the central process to the external process. BUT Whenever each process gets to the msgsnd()/msgrcv() method the processes stall there. In the command line I start both processes with:

./central 60 & ./external 1 60

central.c

#include <stdio.h> 
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

typedef struct mymsg{
 long priority;
 int temp;
 int pid;
 int stable;
}msgp;

int main(int argc, char *argv[])
{
 msgp msgp;
 msgp.priority=2;
 msgp.temp=(int) argv[1];
 msgp.pid=70;
 msgp.stable=0;

//Create central mailbox
 int stat, msqid;
 if ((msqid = msgget(0070, 0666 | IPC_CREAT)) < 0) {
   perror("msgget");
   return 1;
 }
 else 
   (void) fprintf(stderr,"Central msgget: msgget succeeded: msqid = %d\n", msqid);

//Send message to external processes
 stat = msgsnd(msqid, &msgp, sizeof(msgp) - sizeof(long), 0);
 if ( stat < 0) {
    printf ("Insert details of message sent");
    perror("msgsnd");
    return 1;
 }
 else {
   printf("Central successfully sent the Message");
 }

 //temp=(2*temp+exTemps)/6;
 return 0;

}

external.c

#include <stdio.h> 
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>

typedef struct my_msg{
 long priority;
 int temp;
 int pid;
 int stable;
}msgp;

int main(int argc, char *argv[])
{
 msgp msgp;
 msgp.priority=2;
 msgp.temp=(int) argv[1];
 msgp.pid=(int) argv[2];
 msgp.stable=0;

 int msqid, stat;

//Create external mailbox
 if ((msqid = msgget(0070, 0666 | IPC_CREAT)) < 0) {
   perror("msgget");
   return 1;
 }
 else 
   (void) fprintf(stderr,"External msgget: msgget succeeded: msqid = %d\n", msqid);

//Wait to receive message
 stat = msgrcv(msqid, &msgp, sizeof(msgp)-sizeof(long), 2, 0);
 if (stat < 0) {
    printf ("Insert details of attempted received message");
    perror("msgrcv");
    exit(1);
 }
 else {
   printf("External successfully received the Message");
 }

 return 0;

}

Any comments or help would be much appreciated!:slight_smile:

Compiling your receiver program generated these warnings on my platform:

msgrcv.c: In function 'main':
msgrcv.c:17: warning: cast from pointer to integer of different size
msgrcv.c:18: warning: cast from pointer to integer of different size
msgrcv.c:36: warning: incompatible implicit declaration of built-in function 'exit'

These warnings are nothing to sneeze at. The first two in particular are for these lines:

 msgp.temp=(int) argv[1];
 msgp.pid=(int) argv[2];

This does not do what you think they do! Adding the line

printf("msgp.temp=%d msgp.pid=%d\n", msgp.temp, msgp.pid);

shows

msgp.temp=-521312457 msgp.pid=-521312455

Operators in C can't convert anything except atomic types, which strings aren't; as far as C's concerned strings are just arrays and pointers. What you're getting is the memory address the string is stored in. It may not have generated a warning for your platform, if pointers and integers are the same size on it(i.e. 32-bit. I'm running 64-bit.)

You need to use a library function to convert them from strings. A quick-and-dirty fix would be to use atoi(argv[1]) and atoi(argv[2]) instead. These need you to include stdlib.h.

The last warning says the function exit() has no prototype, because it needs stdlib.h too. Best not to ignore those warnings since they can cause crashes in some circumstances.

Also, I don't think your receiver program needs the IPC_CREAT flag. It may be making a brand-new queue overtop your old one.

Thanks for the help!

This thread can be closed. Everything else got figured out.:smiley: