¿por qué algunas POSIX segmentos de memoria compartida y semáforos POSIX que no son visibles a ipcs

StackOverflow https://stackoverflow.com/questions/880127

Pregunta

he construido una aplicación cliente-servidor usando la memoria y POSIX POSIX semáforos sin nombre compartido con pshared = 1. Los semáforos se colocan dentro de la memoria compartida. El programa funciona bien, pero cuando escribo ipcs -m o ipcs -s, no veo ningún segmentos de memoria compartida o semáforos que creé. ¿Por qué es así?

/* 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
    }  
}
¿Fue útil?

Solución

Un par de preguntas:

  • ¿Está ejecutando ipcs como el mismo usuario que creó la memoria compartida / semáforos (o como superusuario)?
  • ¿Está ejecutando ipcs mientras que el programa se está ejecutando? (¿Estás seguro de que no es la eliminación de ellos cuando sale?)

Actualizar

En realidad, después de leer este hilo no soy seguro ipcs se supone que es capaz de mostrar los semáforos POSIX. Probé el código de muestra (con algunas modificaciones para corregir errores de compilación) y se puede ver el segmento de memoria compartida en el directorio /dev/shm.

Otros consejos

ipcs muestra información sobre el sistema System V IPC. POSIX semáforos y memoria compartida son un sistema independiente (y mejor) que no está supervisado por 'ipcs'.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top