我使用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
}
}
我使用ipcs作爲創建共享內存/信號量的相同用戶。服務器創建共享內存並初始化共享內存中的信號量。我在後臺運行服務器,然後在運行客戶端之前鍵入ipcs。 – Anonymous 2009-05-18 22:51:50
僅當客戶端完成時,纔會刪除共享內存和信號量。 – Anonymous 2009-05-18 22:54:45