Question

I construit une application de serveur de client à l'aide posix mémoire partagée et des sémaphores posix anonymes avec pshared = 1. Les sémaphores sont placés à l'intérieur de la mémoire partagée. Le programme fonctionne très bien, mais quand je tape IPCS -m ou IPCS -s, je ne vois pas de segments de mémoire partagée ou sémaphores que j'ai créé. Pourquoi est-il?

/* 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
    }  
}
Était-ce utile?

La solution

Quelques questions:

  • Vous utilisez ipcs que le même utilisateur qui a créé la mémoire partagée / sémaphores (ou super-utilisateur)?
  • Vous utilisez ipcs alors que le programme est en cours? (Êtes-vous sûr que ce n'est pas de les enlever quand il sort?)

Mise à jour :

En fait, après avoir lu ce fil Je ne suis pas sûr IPCS est censé être en mesure de montrer les sémaphores POSIX. J'ai essayé votre exemple de code (avec quelques modifications pour corriger les erreurs de compilation) et vous pouvez voir le segment de mémoire partagée dans le répertoire /dev/shm.

Autres conseils

IPCS affiche des informations sur le système System V IPC. sémaphores POSIX et la mémoire partagée sont un système indépendant (et mieux) non suivie par « IPCS ».

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top