2009-05-18 105 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