Channels !!

bridged with qdn.public.qnxrtp.os
Post Reply
Alain Bonnefoy

Channels !!

Post by Alain Bonnefoy » Wed Oct 04, 2000 3:12 pm

Hi,

Is it a problem to create more than one channel for one thread?
The reason is that I think that it's sometimes easier to have a channel with its own MsgReceive() in some functions to just receive pulses triggered by timers, for example. Just because local variables without any signification outside the dedicated function have to be global in other hand.

This question because it seems to be a problem in fact!

In case, just do it!

testChannel.c:

#include <stdio.h>
#include <stdlib.h>
#include <sys/neutrino.h>
#include <sys/dispatch.h>

int main(void)
{
name_attach_t *attach;
int chid, rcvid;

attach = name_attach(NULL, "testChannel", 0);

while (1)
{
rcvid = MsgReceive(attach->chid, NULL, 0, NULL);
chid = ChannelCreate(0);
printf("chid %d\n", chid);
ChannelDestroy(chid);

MsgReply(rcvid, 0, NULL, 0);
}
}



clientChannel.c:
#include <stdlib.h>
#include <sys/neutrino.h>


int main(void)
{
int fd;

fd = name_open("testChannel", 0);
MsgSend(fd, NULL, 0, NULL, 0);
name_close(fd);
}

../testChannel &

if you launch clientChannel many times, you will see chid increasing until fatal value!!

Thanks, Alain.

Steven Dufresne

Re: Channels !!

Post by Steven Dufresne » Wed Oct 04, 2000 3:12 pm

Alain Bonnefoy <alain.bonnefoy@icbt.com> wrote:
Is it a problem to create more than one channel for one thread?
No problem doing this. Your problem is something else. Sneaky
though, I almost missed it :-).

name_attach() creates a channel with the _NTO_CHF_DISCONNECT flag.
This flag causes a pulse to be sent to you whenever all connections
from a process are detached. name_open() creates a connection and
when your client dies, the kernel automatically detaches its connections
(sort of). So far so good...

When the above mentioned pulse is received, there is something
important you must do. From the docs for ChannelCreate()...

"the server must call ConnectDetach( scoid ) where scoid is the server
connection ID in the pulse message. Failure to do so leaves an invalid
server connection ID which can't be reused. Over time, the server may
run out of available IDs. If this flag isn't set, the kernel removes
the server connection ID automatically, making it available for reuse."

Last thing, scoids, chids and _NTO_SIDE_CHANNEL coids are all taken
from the same space. So you are destroying the channels that you
are creating, its just that every time your client dies, a new
scoid is being left dangling.

See the library reference page for name_attach() for more on how
to properly handle messages from a channel created by name_attach().

See the modifications to your code below...

The reason is that I think that it's sometimes easier to have a channel with its own MsgReceive() in some functions to just receive pulses triggered by timers, for example. Just because local variables without any signification outside the dedicated function have to be global in other hand.
This question because it seems to be a problem in fact!
In case, just do it!

testChannel.c:

#include <stdio.h
#include <stdlib.h
#include <sys/neutrino.h
#include <sys/dispatch.h

int main(void)
{
name_attach_t *attach;
int chid, rcvid;
struct _pulse pulse;
attach = name_attach(NULL, "testChannel", 0);

while (1)
{
rcvid = MsgReceive(attach->chid, NULL, 0, NULL);
becomes:
rcvid = MsgReceive(attach->chid, &pulse, sizeof(pulse), NULL);
chid = ChannelCreate(0);
printf("chid %d\n", chid);
ChannelDestroy(chid);

if (rcvid == 0) { // if a pulse message
switch (pulse.code) {
case _PULSE_CODE_DISCONNECT:
ConnectDetach(pulse.scoid);
break;
}
} else
MsgReply(rcvid, 0, NULL, 0);
}
}
clientChannel.c:
#include <stdlib.h
#include <sys/neutrino.h
int main(void)
{
int fd;

fd = name_open("testChannel", 0);
MsgSend(fd, NULL, 0, NULL, 0);
name_close(fd);
}

./testChannel &

if you launch clientChannel many times, you will see chid increasing until fatal value!!

Thanks, Alain.

Post Reply

Return to “qdn.public.qnxrtp.os”