Beginner question: IPC using channels in QNX6

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?

Gracias.

In article <9u06q1$8sl$1@inn.qnx.com>,
“Ramon” <rentmeester.r@stu.hsbrabant.nl> writes:

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?

The channel id’s hold across processes, but the call to ConnectAttach requires
a process id too. So need the child to know the pid of the parent as well. I’ve
passed this information using the stdin of the child, using a particular file on
the disk and using the following procedure.

A convenient cheat is to use name_attach and name_open:

name_attach_t* mynameattach = name_attach(NULL, “MyName”, 0);

The channel id for use in MsgReceive is simply:

int chid = mynameattach->chid;

and don’t forget the name_detach at the end.

Now your child has to do the very simple:

int coid = name_open(“MyName”, 0);

coid can be used in MsgSend.

Perhaps someone else has a better technique - the resource manager and
dispatcher are excessive for many applications, and don’t work with C++
exceptions.

Tom

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/

thomasf@qnx.com wrote:

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;
}

Really this should be ConnectAttach(0, getppid(), chid, _NTO_SIDE_CHANNEL, 0),
refer to the documentation for a full explanation, but the short of it is that
if you spawn/fork etc then this coid will attempt to be DUP’ed which in this
case will fail because you are doing straight MsgSend/Receive/Reply’s.

Tsk, tsk, tsk … naughty me.

Thomas

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/

Thomas (toe-mah) Fletcher QNX Software Systems
thomasf@qnx.com Core OS Technology Group
(613)-591-0931 http://www.qnx.com/