2009-05-18 107 views
2

我使用posix共享内存构建了一个客户端服务器应用程序,并使用pshared = 1标记了未命名的信号量。信号被放置在共享内存中。该程序运行良好,但是当我输入ipcs -m或ipcs -s时,我没有看到我创建的任何共享内存段或信号量。为什么这样?为什么有些posix共享内存段和posix信号对ipcs不可见

/* 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 
    } 
} 

回答

2

几个问题:

  • 你运行ipcs作为创建的共享内存/信号灯(或超级用户)相同的用户?
  • 您在程序运行时是否正在运行ipcs? (你确定退出时它不删除它们?)

更新

事实上,读这thread后,我不知道IPCS应该是能够显示POSIX信号量。我试过你的示例代码(通过一些修改来修复编译错误),你可以在/dev/shm目录中看到共享内存段。

+0

我使用ipcs作为创建共享内存/信号量的相同用户。服务器创建共享内存并初始化共享内存中的信号量。我在后台运行服务器,然后在运行客户端之前键入ipcs。 – Anonymous 2009-05-18 22:51:50

+0

仅当客户端完成时,才会删除共享内存和信号量。 – Anonymous 2009-05-18 22:54:45

5

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

+1

它会真的帮助,如果你可以添加信息,如什么来代替ipcs做同样的工作,但对于POSIX sems和shmems,提前感谢。 – 2012-08-06 16:06:34