Ramon <rentmeester.r@stu.hsbrabant.nl> wrote:
Hi, I tried having two processes communicate with eachother using channels /
MsgSend / MsgReceive. Sending a message in QNX4 was quite easy when the pid
of the server/target was known (using Send/Receive). What I tried in
neutrino was passing a channel-id and/or a connection-id to a forked/execed
child process and have it send a message to its parent, no succes though.
My question is: How can IPC be realised using channels / MsgSend /
MsgReceive?
Here is a simple example to demonstrate how you can
do it:
/* Simple Send/Receive/Reply example w/ fork */
#include <stdio.h>
#include <sys/neutrino.h>
#define MAX_BUF_SIZE 100
int main(int argc, char **argv) {
int chid, ret;
char buffer[MAX_BUF_SIZE];
if((chid = ChannelCreate(0)) == -1) {
perror(“Can’t create channel”);
return 1;
}
if((ret = fork()) == -1) {
perror(“Can’t fork”);
return 1;
}
if(ret == 0) { /* Child, sender */
int coid;
if((coid = ConnectAttach(0, getppid(), chid, 0, 0)) == -1) {
perror(“Can’t ConnectAttach”);
return 1;
}
strcpy(buffer, “Hello Parent”);
printf(“Child send: %s \n”, buffer);
ret = MsgSend(coid, buffer, strlen(buffer) + 1, buffer, MAX_BUF_SIZE);
if(ret == -1) {
perror(“Can’t MsgSend”);
return 1;
}
printf(“Child received: %s \n”, buffer);
return 1;
} else { /* Parent, receiver */
int rcvid;
rcvid = MsgReceive(chid, buffer, MAX_BUF_SIZE, NULL);
if(rcvid == -1) {
perror(“Can’t MsgReceive”);
return 1;
} else if (rcvid == 0) {
printf(“Odd, received a pulse”);
} else {
printf(“Parent received: %s \n”, buffer);
strcpy(buffer, “Hello Child”);
printf(“Parent reply: %s \n”, buffer);
ret = MsgReply(rcvid, 0, buffer, strlen(buffer) + 1);
if(ret == -1) {
perror(“Can’t MsgReply”);
}
}
sleep(1); /* Let the client run and print a message */
}
/* Just let the system clean up after us … lazy */
return 0;
}
\
Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com Core OS Technology Group
(613)-591-0931 http://www.qnx.com/