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!
