about msgget troble

hi,all
i have in trouble about msgget.
i create a queue and the program like blow:

openMsg( pid_t key )
{
....
int msgid;
....
msgid=msgget(key,IPC_CREAT|IPC_EXCL|0666)
if( msgid<=0 ){
fprintf( stdout,"%s,%d",strerror(errno),errno );
return -1;
}
else{
return msgid;
}
}

when i open my computer and run this program first ,the answer always is "Invalid argument" .
use ipc to see the queue stat, i found a queue is already exist just like this
"Message Queues
q 0 0x00000835 --rw-rw-rw- iccard informix
"
when i delete the queue ,and restart my program ,everything right.
so why? please tell me.
thank you.

:rolleyes:

Hi!

It may be because you are not deleting the "Message Queue" before exiting your application. You should be using the command IPC_RMID in your shmctl\(\) function to remove the Message Queue before exiting your program.

Regards
SHAIK

the queue not exist before i run this program.

What is the value of key ?

the key is 2101

Are you sure you are getting the Error Message "Invalid argument" for this msgget()? Maybe the error is on some other function like: msgctl() or msgsnd() or msgrcv() . . .

yes, i'm sure.
because i have log file and trace the problem.

I dont see any error in the code pasted above, though I still think the error is somewhere else! Why dont you paste the whole program, we'll hopefully solve the problem.

/* this is main entry */
main()
{
char sErrMsg[100];
EXEC SQL BEGIN DECLARE SECTION;
char sWorkDB[20];
EXEC SQL END DECLARE SECTION;

/* control signal */
signal\( SIGCHLD,SIG_IGN  \);
signal\( SIGKILL,exitfunc \);
signal\( SIGQUIT,exitfunc \);
signal\( SIGPIPE,SIG_IGN  \);
signal\( SIGTRAP,sigerr   \);

/* to build a queue */
msgid__=openMsg\(2101\);
if\( msgid__ &lt;=0 \)\{

fprintf( stderr,"%s\n",strerror(errno) );
fprintf( stderr,"create queue error!!\n" );
exit(-1);
}
.......
}

/* this is the function body about openMsg /
int openMsg( key_t k )
{
int iRtn;
key_t key;
char sFilePath[200];
if( k == 0 ){
/
if k is zero , get key by a file */
sprintf( sFilePath,"%s/MSG_ERR_ID",getenv("CNF_PATH") );
key=ftok(sFilePath,'@');
if( key < 0 ){
errorlog("%s.[%s,%d]",strerror(errno),__FILE__,__LINE__);
return -1;
}
}
else{
key = k;
}

iRtn=msgget\(key,IPC\_CREAT|IPC_EXCL|0666\);
if\( iRtn&lt;=0\)\{
   errorlog\("%s.[%s,%d]",strerror\(errno\),\_\_FILE\_\_,\_\_LINE__\);
\}

end:
return iRtn;
}

just modified the main entry program.main()
{
char sErrMsg[100];
EXEC SQL BEGIN DECLARE SECTION;
char sWorkDB[20];
EXEC SQL END DECLARE SECTION;

signal\( SIGCHLD,SIG_IGN  \);
signal\( SIGKILL,exitfunc \);
signal\( SIGQUIT,exitfunc \);
signal\( SIGPIPE,SIG_IGN  \);
signal\( SIGTRAP,sigerr   \);
msgid\_\_=openMsg\(SYSMSG_ID\);
/*if\( msgid__ &lt;=0 \)\{  this is the trouble*/
 if\(msgid__&lt;0\)\{
    fprintf\( stderr,"%s\\n",strerror\(errno\) \);
    fprintf\( stderr,"create queue error!\\n" \);
    exit\(-1\);
\}

Thank for your advices deeply.