Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7767743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:45:04+00:00 2026-06-01T15:45:04+00:00

I am trying to write a C++ program to delete the shared memory segments.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T15:45:05+00:00Added an answer on June 1, 2026 at 3:45 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that can stop and start services using SilverLight
I'm trying to write a file from my Java program, but nothing happens. I'm
I'm trying to write a program using SQL and OleDB and I get an
I'm trying to write an SQL query for my program, but I just can't
I'm trying to write a thread-safe queue using pthreads in c++. My program works
I'm trying to write a program that will delete a set of files/folders that
I am trying to write a program that allows a binary to be run,
I'm trying to write a program in Python and I'm told to run an
I am trying to write a program that displays the integers between 1 and
I'm trying to write a program with Hibernate. My domain is now complete and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.