【发布时间】:2014-10-26 13:01:13
【问题描述】:
在我的应用程序中,我在父子之间(在 Linux 和 Windows 上)为 IPC 使用共享内存。 Linux 的完整代码位于https://github.com/devendermishra/SharedMemoryTest/blob/master/shmem_linux.cpp
我在 Linux 上有以下代码可以从共享内存中读取:
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);
如下代码编写:
//Data is in buf, pBuf is shared memory.
memcpy(buf, pBuf, sizeof(buf));
sem_post(semn);
sem_wait(sem0);
在这种情况下,如果一个进程长时间不写,那么另一个进程将无限期地等待。一种解决方案是在操作无法完成时使用sem_trywait 立即返回。但在这种情况下,需要有人再次调用sem_trywait 来检查是否可以锁定。像文件一样,是否有任何类似于select 或poll 的机制来检查多个信号量的状态,如果有人收到信号,则执行操作而不是被单个信号量阻塞?
【问题讨论】:
标签: c++ c linux shared-memory semaphore