Search code examples
clinuxipc

C - System V - remove shared memory segment


I'm using shared memory with System V IPC. I create segments using keys with the following command:

shmid1 = shmget(key1,1024,0666|IPC_CREAT);

now, I'm trying to close/remove the shared memory segment.

I'm using the following command to verify it's existence:

sm_id = shmget(roomNumber, 1024, IPC_EXCL | 0666);

and I need it to have value -1 or similar if program is interrupted. So I need to remove the segment before my program is interrupted. How can I achieve this? I read about ipcrm but not sure how to use it.

Thanks


Solution

  • As a follow-up to the comments which shows how to remark shared memory segment for destruction:

    shmid1 = shmget(key1,1024,0666|IPC_CREAT);
    ...
    shmctl(shmid1, IPC_RMID, NULL)