C++ 为什么以及何时应该使用IPC_NEW来创建信号量?

C++ 为什么以及何时应该使用IPC_NEW来创建信号量?,c++,c,linux,ipc,semaphore,C++,C,Linux,Ipc,Semaphore,我们可以通过两种方式创建信号量。 1 和 二, 正如Linux手册中提到的那样 IPC_PRIVATE不是标志字段,而是密钥类型。如果这是特别的 值用于键,系统调用将忽略除最小值以外的所有值 shmflg的有效9位,并创建一个新的共享内存段。 我不明白手册上说的。有人能再解释一下吗 与第二种方法相比,使用第一种方法的优缺点是什么? Edit: IPC_PRIVATE => IPC_NEW 首先,你应该观察这个BUG The name choice IPC_PRIVATE was

我们可以通过两种方式创建信号量。
1


二,

正如Linux手册中提到的那样

IPC_PRIVATE不是标志字段,而是密钥类型。如果这是特别的 值用于键,系统调用将忽略除最小值以外的所有值 shmflg的有效9位,并创建一个新的共享内存段。

我不明白手册上说的。有人能再解释一下吗
与第二种方法相比,使用第一种方法的优缺点是什么?

Edit:
IPC_PRIVATE => IPC_NEW 

首先,你应该观察这个BUG

   The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would
   more clearly show its function.
好的。我刚才解释了我对
shmget

   int shmget(key_t key, size_t size, int shmflg);

DESCRIPTION
   shmget()  returns  the identifier of the System V shared memory segment
   associated with the value of the argument key. A  new  shared  memory
   segment,  with size equal to the value of size rounded up to a multiple
   of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key  isn't
   IPC_PRIVATE,  no shared memory segment corresponding to key exists, and
   IPC_CREAT is specified in shmflg.
请参见
shmget

   int shmget(key_t key, size_t size, int shmflg);

DESCRIPTION
   shmget()  returns  the identifier of the System V shared memory segment
   associated with the value of the argument key. A  new  shared  memory
   segment,  with size equal to the value of size rounded up to a multiple
   of PAGE_SIZE, is created if key has the value IPC_PRIVATE or key  isn't
   IPC_PRIVATE,  no shared memory segment corresponding to key exists, and
   IPC_CREAT is specified in shmflg.
ipc.h
文件中
ipc\u create
定义为类似宏

 /* resource get request flags */
 #define IPC_CREAT  00001000   /* create if key is nonexistent */
 #define IPC_EXCL   00002000   /* fail if key exists */
 #define IPC_NOWAIT 00004000   /* return error on wait */
如果单独使用IPC_CREAT,shmget()将返回新创建的段的段标识符,或返回具有相同键值的段的标识符。如果IPC_EXCL与IPC_CREAT一起使用,则会创建一个新段,或者如果该段存在,则调用将失败,返回-1。IPC_EXCL本身是无用的,但当与IPC_CREAT结合使用时,它可以用作一种工具,以确保不打开任何现有段进行访问

如果满足以下条件之一,则会为密钥创建至少大小为字节的共享内存标识符和关联数据结构以及共享内存段:

   o  The key argument is equal to IPC_PRIVATE.

   o  The key argument does not already have a shared  memory
      identifier  associated  with it, and (shmflg&IPC_CREAT)
      is true.

您可能需要向下滚动到。谢谢您指出错误。@JoachimPileborg您能给出答案吗?您是对的,您获取了所有信息。为什么要使用第二个选项?IPC_create:如果不存在具有指定键的信号量集,则创建一个新集。第一个方法也在做同样的事情?
   o  The key argument is equal to IPC_PRIVATE.

   o  The key argument does not already have a shared  memory
      identifier  associated  with it, and (shmflg&IPC_CREAT)
      is true.