我使用posix共享内存构建了一个客户端服务器应用程序,并使用pshared = 1构造了posix未命名的信号量。信号量被放置在共享内存中。程序运行正常,但是当我键入ipcs -m或ipcs -s时,我没有看到我创建的任何共享内存段或信号量。为什么会这样?
/* Server main function for implementing client server program using Posix Shared Memory and Posix Unnamed Semaphores*/
#include "shm_sem.h"
int main(int argc,char ** argv)
{
int fd;
struct shmstruct *ptr;
shm_unlink(MYSHM); // delete shared memory segment, if it already exists
/* create shared memory, set its size, map it and close descriptor */
fd=shm_open(MYSHM,O_RDWR|O_CREAT|O_EXCL,0777);
ptr=mmap(NULL,sizeof(struct shmstruct),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
// truncate the size of shared memory to the size of shmstruct
ftruncate(fd,sizeof(struct shmstruct));
close(fd);
// initialize the semaphores in shared memory
sem_init(&ptr->client_mutex,1,1); // set client semaphore to 1
sem_init(&ptr->server_mutex,1,0); // set server semaphore to 0
for(;;)
{
serverPosixShmSem(ptr); // calling server
}
}
/* Server main function for implementing client server program using Posix Shared Memory and Posix Unnamed Semaphores*/
#include "shm_sem.h"
int main(int argc,char ** argv)
{
int fd;
struct shmstruct *ptr;
shm_unlink(MYSHM); // delete shared memory segment, if it already exists
/* create shared memory, set its size, map it and close descriptor */
fd=shm_open(MYSHM,O_RDWR|O_CREAT|O_EXCL,0777);
ptr=mmap(NULL,sizeof(struct shmstruct),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
// truncate the size of shared memory to the size of shmstruct
ftruncate(fd,sizeof(struct shmstruct));
close(fd);
// initialize the semaphores in shared memory
sem_init(&ptr->client_mutex,1,1); // set client semaphore to 1
sem_init(&ptr->server_mutex,1,0); // set server semaphore to 0
for(;;)
{
serverPosixShmSem(ptr); // calling server
}
}
答案 0 :(得分:5)
ipcs显示System V IPC系统的信息。 POSIX信号量和共享内存是一个独立的(更好的)系统,不受'ipcs'监控。
答案 1 :(得分:2)
几个问题:
ipcs
?ipcs
? (你确定它退出时不会删除它们吗?)更新:
实际上,在阅读了这个thread后,我不确定ipcs应该能够显示POSIX信号量。我尝试了您的示例代码(通过一些编辑来修复编译错误),您可以在/dev/shm
目录中看到共享内存段。