2

我使用 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
    }  
}
4

2 回答 2

5

ipcs 显示有关 System V IPC 系统的信息。POSIX 信号量和共享内存是一个独立的(更好的)系统,不受“ipcs”的监控。

于 2009-05-18T23:43:17.767 回答
2

几个问题:

  • 您是否ipcs以创建共享内存/信号量的同一用户(或超级用户)身份运行?
  • ipcs程序运行时你在运行吗?(你确定它退出时不会删除它们吗?)

更新

实际上,在阅读了这个帖子之后,我不确定 ipcs 是否应该能够显示 POSIX 信号量。/dev/shm我尝试了您的示例代码(进行了一些编辑以修复编译错误),您可以在目录中看到共享内存段。

于 2009-05-18T22:39:51.547 回答