How to return an existing msg message queue id

I am using msgget () syscall to get a new message queue. I used the flags IPC_CREAT and IPC_EXCL. like message_queue = msgget(ftok("/tmp", 100), (0666 | IPC_CREAT | IPC_EXCL)); Now, when my program exists unexpectedly, the msg queue remains, and I was unable to create the msg message queue. So my question is: "How can I return an existing msg queue id?"

and by the way, where does the msg queue store its id?

+2
source share
3 answers

Regd "How can I return an existing msg queue id?"

From user msgget

   If  msgflg  specifies both IPC_CREAT and IPC_EXCL and a message queue already exists for key, then msgget() fails with errno set to EEX-
   IST.  (This is analogous to the effect of the combination O_CREAT | O_EXCL for open(2).)

Try without the IPC_EXCL flag.

Regd. where the msg queue stores its id

from man proc

   /proc/sysvipc
          Subdirectory  containing  the  pseudo-files  msg,  sem  and  shm.  These files list the System V Interprocess Communication (IPC)
          objects (respectively: message queues, semaphores, and shared memory) that currently  exist  on  the  system,  providing  similar
          information  to that available via ipcs(1).  These files have headers and are formatted (one IPC object per line) for easy under-
          standing.  svipc(7) provides further background on the information shown by these files.
+2
source

, , Linux. -, .

ipcs IPC System V.

ipcs -q: Show only message queues
ipcs -s: Show only semaphores
ipcs -m: Show only shared memory
ipcs --help: Additional arguments

ipcrm IPC . ( , ), , , IPC .

:

ipcrm <msg | sem | shm> <IPC ID>
+1

. IPC_CREAT | IPC_EXCL msgget.

msgget

msgflg IPC_CREAT, IPC_EXCL , msgget() errno, EEXIST. ( O_CREAT | O_EXCL open (2).)

, msgget , IPC_CREAT. ftok, msgget , , man-. errno.

Also, if you have too many problems with an existing message queue, you can delete it by calling msgctlalong with the flagIPC_RMID

Also, another answer about where msg queues are stored. You will be tempted to remove the msg alarm queue :) But remember, these are read-only files based on the / proc virtual file system!

0
source

Source: https://habr.com/ru/post/1538873/


All Articles