title: Asynchronous Shared Memory Read/Write
body: <p>In my application, I am using shared memory for IPC between parent and child (on both Linux and Windows). Full code for Linux is at <a href="https://github.com/devendermishra/SharedMemoryTest/blob/master/shmem_linux.cpp" rel="nofollow">https://github.com/devendermishra/SharedMemoryTest/blob/master/shmem_linux.cpp</a></p>
  
  <p>I have following code on Linux to read from the shared memory:</p>
  
  <pre><code>char buf[BUF_SIZE/4];
  //pBuf is the shared memory location
  
  sem_wait(semn);
  //Wait for the parent process to write on the shared memory.
  memcpy(buf, pBuf, sizeof(buf));
  //Signal the parent
  sem_post(sem0);
  </code></pre>
  
  <p>Following code to write:</p>
  
  <pre><code>//Data is in buf, pBuf is shared memory.
  memcpy(buf, pBuf, sizeof(buf));
  sem_post(semn);
  sem_wait(sem0);
  </code></pre>
  
  <p>In this case, if one process does not write for a long time, then other process will be waiting indefinitely. One solution is to use <code>sem_trywait</code> to return immediately if operation cannot be completed. But in this case, someone needs to call <code>sem_trywait</code> again to check if it can be locked. Like file, is there any similar mechanism to <code>select</code> or <code>poll</code> to check the status of multiple semaphores and if anyone is signaled, perform the operation rather than getting blocked on a single semaphore?</p>