c++unixshared-memoryinter-process-communicatipcs

Delete unix shared memory segments using a C++ program


I am trying to write a C++ program to delete the shared memory segments. I know it can be done from cmd prompt using

ipcs -m | awk '{ print $2 }' | xargs ipcrm -m

But I am trying to do it using C++ so that I create a shared memory garbage collector.

The idea is to delete shared memory segments that are not attached to any process( nattach ==0) or the status == NULL


Solution

  • I finally have the the answer to my own question. It can be done using shmctl flags. shmctl(0,SHM_INFO,&shm_info); gives number of segments currently present.

    shmctl(i , SHM_STAT , &shm_segment) gives the segment id

    it can also be accessed by shm_segment.shm_id

    #include <sys/shm.h>     
    
    int delete_segment(int seg_id){
        if ((shmctl(seg_id,IPC_RMID,0))==-1){
        std::cout<<" ERROR(C++)with shmctl(IPC_RMID): "<<strerror(errno)<<std::endl;
        return -1;
        }else//on success
            return 0;
    }
    
    void clean_segments(){
    
        struct shmid_ds shm_info;
        struct shmid_ds shm_segment;
        int max_id = shmctl(0,SHM_INFO,&shm_info);
        if (max_id>=0){
            for (int i=0;i<=max_id;++i) {
                    int shm_id = shmctl(i , SHM_STAT , &shm_segment);
                    if (shm_id<=0)
                        continue;
                    else if (shm_segment.shm_nattch==0){
                        delete_segment(shm_id);
                    }
            }
        }
        return result;
    }