Thanks VERY MORE sehe and VJo,
Here is a modified version of the program, taking into account all your comments.
Hope this will be helpful to someone else.
Bert
#include <sys/types.h> #include <sys/msg.h> #include <sys/ipc.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <syslog.h> #define MQMICKEY "/tmp/MQMickey" #define MQDONALD "/tmp/MQDonald" pthread_t t1,t2,t3,t4; FILE *fMickeypointer; FILE *fDonaldpointer; // Mickey send key_t ipcMickey; int mqMickeyid; char helloMickeymsg[] = {"Hello, Minnie!"}; struct { long type; char text[100]; } myMickeymsg; // Minnie get int mqMinnieid; struct { long type; char text[100]; } myMinniemsg; // Donald send key_t ipcDonald; int mqDonaldid; char helloDonaldmsg[] = {"Sit, Pluto!"}; struct { long type; char text[100]; } myDonaldmsg; // Pluto get int mqPlutoid; struct { long type; char text[100]; } myPlutomsg; static void * DONALDthreadFunc(void *arg) { printf("Donald thread successfully started.\n"); char *s = (char *) arg; while(1) { //send memset(myDonaldmsg.text, 0, 100); strncpy(myDonaldmsg.text, helloDonaldmsg, strlen(helloDonaldmsg)); myDonaldmsg.type = 1; msgsnd(mqDonaldid, &myDonaldmsg, sizeof(myDonaldmsg), 0); printf("Donald sent a message to Pluto.\r\n"); } /* just a formality */ return (void *) strlen(s); } static void * PLUTOthreadFunc(void *arg) { printf("Pluto thread successfully started.\n"); char *s = (char *) arg; while(1) { //receive mqPlutoid = msgget(ipcDonald, 0); msgrcv(mqPlutoid, &myPlutomsg, sizeof(myPlutomsg), 0, 0); printf("Pluto received: %s\r\n", myPlutomsg.text); } /* just a formality */ return (void *) strlen(s); } static void * MICKEYthreadFunc(void *arg) { printf("Mickey thread successfully started.\n"); char *s = (char *) arg; while(1) { //send memset(myMickeymsg.text, 0, 100); strncpy(myMickeymsg.text, helloMickeymsg, strlen(helloMickeymsg)); myMickeymsg.type = 1; msgsnd(mqMickeyid, &myMickeymsg, sizeof(myMickeymsg), 0); printf("Mickey sent a message to Minnie.\r\n"); } /* just a formality */ return (void *) strlen(s); } static void * MINNIEthreadFunc(void *arg) { printf("Minnie thread successfully started.\n"); char *s = (char *) arg; while(1) { //receive mqMinnieid = msgget(ipcMickey, 0); msgrcv(mqMinnieid, &myMinniemsg, sizeof(myMinniemsg), 0, 0); printf(" Minnie received: %s\r\n", myMinniemsg.text); } return (void *) strlen(s); } int main (void) { /* MUST create the target files first. The return of ftok is -1 if file is missing. If BOTH files are missing, both ipc keys will be -1, meaning all traffic shares the same port. Result: one queue will acquire messages not intended for it! */ fMickeypointer = fopen(MQMICKEY, "a"); if(fMickeypointer == NULL) { printf("Failed to create Mickey queue file.\n"); /* system logging */ setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("MickeyDonald", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog (LOG_NOTICE, "Failed to create Mickey queue file." ); closelog (); exit(91); } fDonaldpointer = fopen(MQDONALD, "a"); if(fDonaldpointer == NULL) { printf("Failed to create Donald queue file.\r\n"); /* system logging */ setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("MickeyDonald", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog (LOG_NOTICE, "Failed to create Donald queue file." ); closelog (); exit(92); } ipcMickey = 0; ipcDonald = 0; /* make sure we assign DIFFERENT ipc values */ while(ipcMickey == ipcDonald) { ipcMickey = ftok(MQMICKEY, 63); if (-1==ipcMickey) { printf("ipcMickey does not exist.\r\n"); /* system logging */ setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("MickeyDonald", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog (LOG_NOTICE, "Failed to ftok Mickey file." ); closelog (); exit(93); } ipcDonald = ftok(MQDONALD, 69); if (-1==ipcDonald) { printf("ipcDonald does not exist.\r\n"); /* system logging */ setlogmask (LOG_UPTO (LOG_NOTICE)); openlog ("MickeyDonald", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog (LOG_NOTICE, "Failed to ftok Donald file." ); closelog (); exit(94); } } mqMickeyid = msgget(ipcMickey, IPC_CREAT | 0666); mqDonaldid = msgget(ipcDonald, IPC_CREAT | 0666); pthread_create(&t1, NULL, MICKEYthreadFunc, "Mickey sends\n"); pthread_create(&t2, NULL, MINNIEthreadFunc, "Minnie replies\n"); pthread_create(&t3, NULL, DONALDthreadFunc, "Donald sends\n"); pthread_create(&t4, NULL, PLUTOthreadFunc, "Pluto replies\n"); long exitcounter = 0; while(exitcounter < 100000) { exitcounter +=1; usleep(100); } exit(0); }