gpt4 book ai didi

client-server - 为什么某些 posix 共享内存段和 posix 信号量对 ipc 不可见

转载 作者:行者123 更新时间:2023-12-04 07:35:34 25 4
gpt4 key购买 nike

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

最佳答案

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

关于client-server - 为什么某些 posix 共享内存段和 posix 信号量对 ipc 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/880127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com