2

I built a client server application using posix shared memory and posix unnamed semaphores with pshared=1. The semaphores are placed inside the shared memory. The program runs fine, but when I type ipcs -m or ipcs -s, I do not see any shared memory segments or semaphores that I created. Why is it so?

/* 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
    }  
}
Lesmana
  • 25,663
  • 9
  • 82
  • 87
Anonymous
  • 4,133
  • 10
  • 31
  • 38

2 Answers2

5

ipcs displays information on the System V IPC system. POSIX semaphores and shared memory are an independent (and better) system which is not monitored by 'ipcs'.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 1
    it would really help if you could add info such as what to use in place of ipcs to do the same job but for POSIX sems and shmems, thanks in advance. – Art Swri Aug 06 '12 at 16:06
2

A couple of questions:

  • Are you running ipcs as the same user that created the shared memory/semaphores (or as the superuser)?
  • Are you running ipcs while the program is running? (Are you sure it's not removing them when it exits?)

Update:

Actually, after reading this thread I'm not sure ipcs is supposed to be able to show the POSIX semaphores. I tried your sample code (with some edits to fix compile errors) and you can see the shared memory segment in the /dev/shm directory.

Jacob Gabrielson
  • 34,800
  • 15
  • 46
  • 64
  • I'm using ipcs as the same user that created the shared-memory/semaphores. The server creates the shared memory and initializes the semaphores in the shared memory. I run the server in the back-ground and then type ipcs before running the client. – Anonymous May 18 '09 at 22:51
  • The client alone removes the shared-memory and semaphores when it is done. – Anonymous May 18 '09 at 22:54