file descriptors

hallo,
i m programming a middelware application on and i have some problems with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while opening a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji

Hello Dadji

Can you post a code sample of what you are doing?

Thanks,
Rodney


dadji wrote:

hallo,
i m programming a middelware application on and i have some problems with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while opening a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji

While 0 is a perfectly good file descriptor, it would be very unusual
to have it assigned dynamically when you are running a program. The
first three file descriptors are usually assigned automatically to
stdin, stdout, and stderr. You could close these and then not be
surprised at geting 0 back from an open() or shm_open(). In your
case, the zero suggests an underlying problem. What processor are
you running this on?

the application is working on an Intel Pentium (R) 4, 2.80GHz
I have installed QNX6.3.0
“maschoen” <maschoen@pobox-dot-com.no-spam.invalid> schrieb im Newsbeitrag
news:ep5pg3$e83$1@inn.qnx.com

While 0 is a perfectly good file descriptor, it would be very unusual
to have it assigned dynamically when you are running a program. The
first three file descriptors are usually assigned automatically to
stdin, stdout, and stderr. You could close these and then not be
surprised at geting 0 back from an open() or shm_open(). In your
case, the zero suggests an underlying problem. What processor are
you running this on?

Hello Rodney,
in the attachment there is the code to create and to release the shared
memory. I hope you will have any suggestion.
Thanks.

Dadji

“Rodney Dowdall” <rdowdall@qnx.com> schrieb im Newsbeitrag
news:ep5e27$74b$1@inn.qnx.com

Hello Dadji

Can you post a code sample of what you are doing?

Thanks,
Rodney


dadji wrote:
hallo,
i m programming a middelware application on and i have some problems
with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while opening
a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji

begin 666 shmem_functions_for news_group.c
M+RHJ"BH)9G5N8W1I;VX@=7-E9"!T;R!C<F5A=&4@82!S:&%R960@;65M;W)Y
M"BHO"G9O:60J($-O;F9I9U]3:$UE;5]#<F5A=&4H(&-H87(J(’!C37-G3F%M
M92P@=%]I;G0S,B J<$9D7U-H365M+"!T7W5I;G0S,B J<$QE;F=T:" I"GL*
M"6-H87()“0D)86-3:$UE;4YA;65;($U!6%]32$U%35].04U%7TQ%3D=42”!=
M.PH)=F]I9"H)“7!-96UO<GD[”@H)+R@;6%K92!3:$UE;4YA;64@8V]N9F]R
M;2!T;R!03U-)6"!S=&%N9&%R9" H;&5A9&EN9R B+R(I"@EA8U-H365M3F%M
M95LP70D]("<O)SL*“6%C4VA-96U.86UE6S%=“3T@,#L*“7-T<F-A=”@@86-3
M:$UE;4YA;64L(’!C37-G3F%M92 I.PH)”@DO+R!G970@9FEL92!D97-C<FEP
M=&]R(&9O<B!3:&%R961-96T*“6EF*” H(“IP1F1?4VA-96T@/2!S:&U?;W!E
M;B@@86-3:$UE;4YA;64L($]?4D174B!($]?0U)%050L(# W-S<@2 I(#T]
M($9!24Q%1" I"@E["@D):68H($=L;V)A;$-O;F9I9RY$14)51U],159%3" F
M($1%0E5’7U=!4DY)3D=3("D
"0E[”@D)“69P<FEN=&8@"!S=&1E<G(L(%]?
M4U127U=!4DY)3D=?7U-(345-7T-214%424].7T9!24Q%1"P@86–;V1U;&5.
M86UE+"!P8TUS9TYA;64@3L"0D)<’)I;G1F
”)<(B5S7”(@<VAM96U#<F5A
M=&5;)7-=(&5R<F]R($524DY/6R5D72!-14%.24Y’6R5S75QN(BP@86–;V1U
M;&5.86UE+"!A8U-H365M3F%M92P@97)R;F\L(’-T<F5R<F]R*&5R<FYO2D[
M"@D)?0H)“7)E='5R;B@@3E5,3” I.PH)?0H)"@DO+R!D969I;F4@;&5N9W1H
M(&]F(%-H87)E9$UE;2!R96=I;VX@=&@<F5G:7-T97(
“6EF*”!F=’)U;F-A
M=&4H(“IP1F1?4VA-96TL(“IP3&5N9W1H(“D@/3T@1D%)3$5$(“D*“7L*“0EI
M9B@@1VQO8F%L0V]N9FEG+D1%0E5’7TQ%5D5,(“8@1$5"54=?5T%23DE.1U,@
M0H)"7L"0D)9G!R:6YT9B@@<W1D97)R+”!?7U-44E]705).24Y’7U]32$U%
M35]325I%7T9!24Q%1"P@86–;V1U;&5.86UE+”!P8TUS9TYA;64@3L"0E]
M”@D)<F5T=7)N*”!.54Q,(“D[”@E]”@H)+R@9V5T(&%D9’)E<W,@;V8@4VAA
M<F5D365M(&]B:F5C= H):68H(”@@<$UE;6]R>2 ](&UM87 H(# L(“IP3&5N
M9W1H+”!04D]47U)%040@?”!04D]47U=2251%(‘P@4%)/5%].3T-!0TA%+"!-
M05!?4TA!4D5$+" J<$9D7U-H365M+" P("D@2 ]/2!-05!?1D%)3$5$("D
M"7L*"0EI9B@@1VQO8F%L0V]N9FEG+D1%0E5’7TQ%5D5,(“8@1$5"54=?5T%2
M3DE.1U,@0H)"7L"0D)9G!R:6YT9B@@<W1D97)R+”!?7U-44E]705).24Y’
M7U]32$U%35]-05!024Y’7T9!24Q%1"P@86–;V1U;&5.86UE+"!P8TUS9TYA
M;64@3L"0E]"@D)<F5T=7)N*"!.54Q,(“D[”@E]"@H)+R@9FEL;"!3:&%R
M961-96T@=VET:"!-25)005]32$U%35])3DE47U9!3%5%"@EM96US970H(’!-
M96UO<GDL($U)4E!!7U-(345-7TE.251?5D%,544L(“IP3&5N9W1H(“D[”@D*
M"2\O(’!R:6YT(&1E8G5G(&1A=&$"6EF”!’;&]B86Q#;VYF:6<N1$5"54=?
M3$5614P@)B!$14)51U]705).24Y’4R I"@E["@D)“7!R:6YT9B@B7&Y#<F5A
M=&5S:&%R961M96TH3H@3F%M95LE<UTL(&9I;&4@9&5S8W)I<‘1O<ELE9%TL
M(&QE;F=T:%LE9%T@6S!X)3!X75QN7&XB+"!P8TUS9TYA;64L(" J<$9D7U-H
M365M+" J<$QE;F=T:“P@<$UE;6]R>2D[”@E]"@H)+R@<F5T=7)N(’!O:6YT
M97(@=&@<VAA<F5D(&UE;0H)<F5T=7)N
”!P365M;W)Y(“D[“GT*”@H*”@HO
MBH**@EF=6YC=&EO;B!T;R!D96QE=&4@=&AE(’-H87)E9"!M96UO<GD**B*
M=F]I9"!$96QE=&53:$UE;2AC:&%R(“H@;F%M92P@=F]I9” J(’!-96UO<GDL
M(&EN=" J<$QE;F=T:"P@:6YT(&9D7U-H365M("D
>PD)"@DO+R!U;FUA<"!S
M:&%R960@;65M;W)Y"@EI9B@@;75N;6%P*’!-96UO<GDL(“IP3&5N9W1H(“D@
M/3T@+3$I”@E[”@D)<’)I;G1F*")<(B5S7"(@97)R;W(@=VAI;&4@=6UA<&EN
M9R!S:&%R960@;65M;W)Y6R5S72!L96YG=&A;)61=7&XB+"!A8TUO9’5L94YA
M;64L(&YA;64LG!,96YG=&@I.PH)?0H)"0H)"2\O(&-L;W-E(&9I;&4@9&5S
M8W)I<'1O<@H):68@
"!F9%]3:$UE;2D@/3T@+3$I"@E["@D):68@"!’;&]B
M86Q#;VYF:6<N1$5"54=?3$5614P@)B!$14)51U]705).24Y’4R I"@D)>PH)
M"0EF<’)I;G1F
’-T9&5R<BP@(B5S(%=!4DY)3D<Z(&-O=6QD;B=T(&-L;W-E
M(’-H87)E9"!M96UO<GD@9FEL92!D97-C<FEP=&]R6R5D75QN(BP@86–;V1U
M;&5.86UE+&9D7U-H365M3L"0D)<’)I;G1F*" B)7,Z(%=!4DY)3D<@.B!%
M4E).3ULE9%T@345!3DE.1ULE<UU<;B(L(&%C36]D=6QE3F%M92P@97)R;F\L
M(’-T<F5R<F]R*&5R<FYO2 I.PH)"7T"7T*“0D*“2\O('5N;&EN:R!T:&4@
M;65M;W)Y”@ES=’)C<'DH86-496UP4VA-96TL(”(O(BD["@ES=’)C870H86-4
M96UP4VA-96TL(&YA;64I.PH):68@"AI56YL:6YT=F%L=64@/2!S:&U?=6YL
M:6YK
&%C5&5M<%-H365M2D@/3T@+3$I"@E["@D)<’)I;G1F")<(B5S7"(@
M<VAM7W5N;&EN:ULE<UT@97)R;W(@15)23D];)61=($U%04Y)3D=;)7-=7&XB
M+"!A8TUO9’5L94YA;64L(&%C5&5M<%-H365M+"!E<G)N;RP@<W1R97)R;W(H
/97)R;F\I3L"7T*"0I]
`
end

Hello Dadji

Where do you actually check to see if the return is 0? In the
Config_ShMem_Create function or somewhere else in code? Is there a
chance that the pointer is getting modified somewhere else?

Also, what name are you using for the shared memory object?

Thanks,
Rodney


dadji wrote:

Hello Rodney,
in the attachment there is the code to create and to release the shared
memory. I hope you will have any suggestion.
Thanks.

Dadji

“Rodney Dowdall” <> rdowdall@qnx.com> > schrieb im Newsbeitrag
news:ep5e27$74b$> 1@inn.qnx.com> …
Hello Dadji

Can you post a code sample of what you are doing?

Thanks,
Rodney


dadji wrote:
hallo,
i m programming a middelware application on and i have some problems
with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while opening
a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji




/**

  • function used to create a shared memory
    /
    void
    Config_ShMem_Create( char* pcMsgName, t_int32 *pFd_ShMem, t_uint32 pLength )
    {
    char acShMemName[ MAX_SHMEM_NAME_LENGTH ];
    void
    pMemory;

// make ShMemName conform to POSIX standard (leading “/”)
acShMemName[0] = ‘/’;
acShMemName[1] = 0;
strcat( acShMemName, pcMsgName );

// get file descriptor for SharedMem
if( ( *pFd_ShMem = shm_open( acShMemName, O_RDWR | O_CREAT, 0777 ) ) == FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf ( stderr, __STR_WARNING__SHMEM_CREATION_FAILED, acModuleName, pcMsgName );
printf(""%s" shmemCreate[%s] error ERRNO[%d] MEANING[%s]\n", acModuleName, acShMemName, errno, strerror(errno));
}
return( NULL );
}

// define length of SharedMem region to register
if( ftruncate( *pFd_ShMem, *pLength ) == FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_SIZE_FAILED, acModuleName, pcMsgName );
}
return( NULL );
}

// get address of SharedMem object
if( ( pMemory = mmap( 0, *pLength, PROT_READ | PROT_WRITE | PROT_NOCACHE, MAP_SHARED, *pFd_ShMem, 0 ) ) == MAP_FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_MAPPING_FAILED, acModuleName, pcMsgName );
}
return( NULL );
}

// fill SharedMem with MIRPA_SHMEM_INIT_VALUE
memset( pMemory, MIRPA_SHMEM_INIT_VALUE, *pLength );

// print debug data
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
printf("\nCreatesharedmem(): Name[%s], file descriptor[%d], length[%d] [0x%0x]\n\n", pcMsgName, *pFd_ShMem, *pLength, pMemory);
}

// return pointer to shared mem
return( pMemory );
}





/**

  • function to delete the shared memory
    */
    void DeleteShMem(char * name, void * pMemory, int *pLength, int fd_ShMem )
    {
    // unmap shared memory
    if( munmap(pMemory, *pLength ) == -1)
    {
    printf(""%s" error while umaping shared memory[%s] length[%d]\n", acModuleName, name,*pLength);
    }

// close file descriptor
if ( fd_ShMem) == -1)
{
if ( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf(stderr, “%s WARNING: couldn’t close shared memory file descriptor[%d]\n”, acModuleName,fd_ShMem);
printf( “%s: WARNING : ERRNO[%d] MEANING[%s]\n”, acModuleName, errno, strerror(errno) );
}
}

// unlink the memory
strcpy(acTempShMem, “/”);
strcat(acTempShMem, name);
if ((iUnlintvalue = shm_unlink(acTempShMem)) == -1)
{
printf(""%s" shm_unlink[%s] error ERRNO[%d] MEANING[%s]\n", acModuleName, acTempShMem, errno, strerror(errno));
}

}

“dadji” <ydadji@hotmail.com> wrote in message
news:ep77pj$dnk$1@inn.qnx.com

Hello Rodney,
in the attachment there is the code to create and to release the shared
memory. I hope you will have any suggestion.
Thanks.

General tips. When passing pointer at least check if they are not NULL (not
to bad because if there where they would create a sigsegv). Also don’t use
strcpy but strncpy for safety.

In create fonction filehanle is of type t_int32 but in delete it’s of type
int. Length is also different. Just being picky here :wink:

And this can’t be the real code as it would not compile properly:

if ( fd_ShMem) == -1)


Dadji

“Rodney Dowdall” <> rdowdall@qnx.com> > schrieb im Newsbeitrag
news:ep5e27$74b$> 1@inn.qnx.com> …
Hello Dadji

Can you post a code sample of what you are doing?

Thanks,
Rodney


dadji wrote:
hallo,
i m programming a middelware application on and i have some problems
with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call
gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while
opening
a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji

\

Hello Rodney and Mario,
I thank you for your suggestion, It helped me to fix the problem.
I used somewhere in my code the qnx function ConnectDetach() with the wrong
parameter (0). This caused a further call of shm_open() to return a 0 as
file descritor.

thanks

Dadji
“Rodney Dowdall” <rdowdall@qnx.com> schrieb im Newsbeitrag
news:ep889s$553$1@inn.qnx.com

Hello Dadji

Where do you actually check to see if the return is 0? In the
Config_ShMem_Create function or somewhere else in code? Is there a
chance that the pointer is getting modified somewhere else?

Also, what name are you using for the shared memory object?

Thanks,
Rodney


dadji wrote:
Hello Rodney,
in the attachment there is the code to create and to release the shared
memory. I hope you will have any suggestion.
Thanks.

Dadji

“Rodney Dowdall” <> rdowdall@qnx.com> > schrieb im Newsbeitrag
news:ep5e27$74b$> 1@inn.qnx.com> …
Hello Dadji

Can you post a code sample of what you are doing?

Thanks,
Rodney


dadji wrote:
hallo,
i m programming a middelware application on and i have some problems
with
the file descriptor (related to the shm_open(…) function) .
As i call the the shm_open(…) it return the file descriptor “fd = 0”.
I later close the file descriptor using “close(fd)”, but this call
gives
back an error; the variable erno has the value 9, meaning “Bad file
descriptor”.
Every later call of shm_open function return an error.

Here is my question:
is it nornal, to become a file descrptor equal 0(zero) while
opening
a
shared memory with shm_open()?
why can’t my application close the shared memory?
where can i find a doc for the handling of file descriptor under QNX?

thanks for helping me.
best regards
Dadji




/**

  • function used to create a shared memory
    /
    void
    Config_ShMem_Create( char* pcMsgName, t_int32 *pFd_ShMem, t_uint32
    pLength )
    {
    char acShMemName[ MAX_SHMEM_NAME_LENGTH ];
    void
    pMemory;

// make ShMemName conform to POSIX standard (leading “/”)
acShMemName[0] = ‘/’;
acShMemName[1] = 0;
strcat( acShMemName, pcMsgName );

// get file descriptor for SharedMem
if( ( *pFd_ShMem = shm_open( acShMemName, O_RDWR | O_CREAT, 0777 ) ) ==
FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf ( stderr, __STR_WARNING__SHMEM_CREATION_FAILED, acModuleName,
pcMsgName );
printf(""%s" shmemCreate[%s] error ERRNO[%d] MEANING[%s]\n",
acModuleName, acShMemName, errno, strerror(errno));
}
return( NULL );
}

// define length of SharedMem region to register
if( ftruncate( *pFd_ShMem, *pLength ) == FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_SIZE_FAILED, acModuleName,
pcMsgName );
}
return( NULL );
}

// get address of SharedMem object
if( ( pMemory = mmap( 0, *pLength, PROT_READ | PROT_WRITE |
PROT_NOCACHE, MAP_SHARED, *pFd_ShMem, 0 ) ) == MAP_FAILED )
{
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf( stderr, __STR_WARNING__SHMEM_MAPPING_FAILED, acModuleName,
pcMsgName );
}
return( NULL );
}

// fill SharedMem with MIRPA_SHMEM_INIT_VALUE
memset( pMemory, MIRPA_SHMEM_INIT_VALUE, *pLength );

// print debug data
if( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
printf("\nCreatesharedmem(): Name[%s], file descriptor[%d], length[%d]
[0x%0x]\n\n", pcMsgName, *pFd_ShMem, *pLength, pMemory);
}

// return pointer to shared mem
return( pMemory );
}





/**

  • function to delete the shared memory
    */
    void DeleteShMem(char * name, void * pMemory, int *pLength, int
    fd_ShMem )
    {
    // unmap shared memory
    if( munmap(pMemory, *pLength ) == -1)
    {
    printf(""%s" error while umaping shared memory[%s] length[%d]\n",
    acModuleName, name,*pLength);
    }

// close file descriptor
if ( fd_ShMem) == -1)
{
if ( GlobalConfig.DEBUG_LEVEL & DEBUG_WARNINGS )
{
fprintf(stderr, “%s WARNING: couldn’t close shared memory file
descriptor[%d]\n”, acModuleName,fd_ShMem);
printf( “%s: WARNING : ERRNO[%d] MEANING[%s]\n”, acModuleName, errno,
strerror(errno) );
}
}

// unlink the memory
strcpy(acTempShMem, “/”);
strcat(acTempShMem, name);
if ((iUnlintvalue = shm_unlink(acTempShMem)) == -1)
{
printf(""%s" shm_unlink[%s] error ERRNO[%d] MEANING[%s]\n",
acModuleName, acTempShMem, errno, strerror(errno));
}

}