qnx_name_locate() fails?

Hi folks,

Could you explain such a case please?

I’ve got a piece of code for testing qnx_name_locate(). Here it is:

-----------------------8<---------------------------

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

#define PNAME “swd/test”

pid_t pid;
int nameid;

int main ()
{
if ( nameid = qnx_name_attach( 0, PNAME ) == -1 )
{
fprintf( stderr, "cannot attach name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

puts( “Name attached; strike a key…” );
getch();

if ( pid = qnx_name_locate( 0, PNAME, 0, NULL ) == -1 )
{
fprintf( stderr, "cannot locate name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

printf( “located %s at PID %d\n”, PNAME, pid );
qnx_name_detach( 0, nameid );
return( EXIT_SUCCESS );
}

-----------------------8<---------------------------

It registers the name OK (I can see it via “sin na”), but then qnx_name_locate() returns PID 0 instead of the real PID, so that the program reports that it has “located swd/test at PID 0”.

What may be wrong? I’m using WCC 10.6B under QNX 4.25C.

Thanks,

  • Nick

Hi Nick,

I don’t have a QNX system handy but I have an idea.
‘pid==0’ means the current process, doesn’t it?

It’s just my 0.02 CAD :slight_smile:
– Dmitri


Nikolai Gorbunov wrote:

Hi folks,

Could you explain such a case please?

I’ve got a piece of code for testing qnx_name_locate(). Here it is:

-----------------------8<---------------------------

#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <errno.h

#define PNAME “swd/test”

pid_t pid;
int nameid;

int main ()
{
if ( nameid = qnx_name_attach( 0, PNAME ) == -1 )
{
fprintf( stderr, "cannot attach name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

puts( “Name attached; strike a key…” );
getch();

if ( pid = qnx_name_locate( 0, PNAME, 0, NULL ) == -1 )
{
fprintf( stderr, "cannot locate name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

printf( “located %s at PID %d\n”, PNAME, pid );
qnx_name_detach( 0, nameid );
return( EXIT_SUCCESS );
}

-----------------------8<---------------------------

It registers the name OK (I can see it via “sin na”), but then qnx_name_locate() returns PID 0 instead of the real PID, so that the program reports that it has “located swd/test at PID 0”.

What may be wrong? I’m using WCC 10.6B under QNX 4.25C.

Thanks,

  • Nick

Nikolai Gorbunov <n.gorbunov@swd.ru> wrote:

Hi folks,

Could you explain such a case please?

I’ve got a piece of code for testing qnx_name_locate(). Here it is:

-----------------------8<---------------------------

\
> if ( pid = qnx_name_locate( 0, PNAME, 0, NULL ) == -1 )

if ( (pid = qnx_name_locate( 0, PNAME, 0, NULL )) == -1 )
_ _

-----------------------8<---------------------------

It registers the name OK (I can see it via “sin na”), but then qnx_name_locate() returns PID 0 instead of the real PID, so that the program reports that it has “located swd/test at PID 0”.

What may be wrong? I’m using WCC 10.6B under QNX 4.25C.

Thanks,

  • Nick

You tripped over a weak link in the C language syntax where:
if ( a = b() == c )
actually says
evaluate the truthfullness of the return value from b() being equal to c
and assign that truthfullness to a.
Obviously you intended to do the assignment first but in this case you would
need an extra set of parens:
if ( a = b() ) == c )

It’s actually not so goofy though. If that same expression were NOT in an
if() statement you would expect it to evaluate exactly the way it does:
a = b() == c;

Nikolai Gorbunov <n.gorbunov@swd.ru> wrote in message
news:Voyager.000817134527.347A@gorba…

Hi folks,

Could you explain such a case please?

I’ve got a piece of code for testing qnx_name_locate(). Here it is:

-----------------------8<---------------------------

#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <errno.h

#define PNAME “swd/test”

pid_t pid;
int nameid;

int main ()
{
if ( nameid = qnx_name_attach( 0, PNAME ) == -1 )
{
fprintf( stderr, "cannot attach name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

puts( “Name attached; strike a key…” );
getch();

if ( pid = qnx_name_locate( 0, PNAME, 0, NULL ) == -1 )
{
fprintf( stderr, "cannot locate name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

printf( “located %s at PID %d\n”, PNAME, pid );
qnx_name_detach( 0, nameid );
return( EXIT_SUCCESS );
}

-----------------------8<---------------------------

It registers the name OK (I can see it via “sin na”), but then
qnx_name_locate() returns PID 0 instead of the real PID, so that the program

reports that it has “located swd/test at PID 0”.

What may be wrong? I’m using WCC 10.6B under QNX 4.25C.

Thanks,

  • Nick

Bill at Sierra Design <BC@SierraDesign.com> wrote in message
news:8nh4on$29k$1@inn.qnx.com

Obviously you intended to do the assignment first but in this case you
would
need an extra set of parens:
if ( a = b() ) == c )

Oops!

OK make that:
if ( ( a = b() ) == c )

Hi Nick,

I don’t have a QNX system handy but I have an idea.
‘pid==0’ means the current process, doesn’t it?

It’s just my 0.02 CAD > :slight_smile:
– Dmitri

Nope, it definitely doesn’t. For instance, ‘RCV-blocked on PID 0’
means ‘ready to receive from any process’.

  • Nick

Bill at Sierra Design <BC@SierraDesign.com> ??? ?
???:8nh4ta$2a4$1@inn.qnx.com

Bill at Sierra Design <> BC@SierraDesign.com> > wrote in message
news:8nh4on$29k$> 1@inn.qnx.com> …
Obviously you intended to do the assignment first but in this case
you
would
need an extra set of parens:
if ( a = b() ) == c )

Oops!

OK make that:
if ( ( a = b() ) == c )

Oh, … .

<blush.>

Thanks Bill!

Nikolai Gorbunov <n.gorbunov@swd.ru> wrote:

Hi folks,

Could you explain such a case please?

I’ve got a piece of code for testing qnx_name_locate(). Here it is:

-----------------------8<---------------------------

#include <stdio.h
#include <stdlib.h
#include <unistd.h
#include <errno.h

#define PNAME “swd/test”

pid_t pid;
int nameid;

int main ()
{
if ( nameid = qnx_name_attach( 0, PNAME ) == -1 )
{
fprintf( stderr, "cannot attach name %s: ", PNAME );
perror( NULL );
return( EXIT_FAILURE );
}

puts( “Name attached; strike a key…” );
getch();

if ( pid = qnx_name_locate( 0, PNAME, 0, NULL ) == -1 )

Order of operations.

== has a higher precedence than =, so it gets evaluated first.
Your code gets evaluated as if bracketed:

if ( pid = (qnx_name_locate(…) == -1)

You need to add brackets:

if ( (pid = qnx_name_locate(…)) == -1 )

If you had the compiler warnings turned on (cc -w3) it would
probably have generated a warning about this if statement.

-David