Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared memory disappears after writer process finishes, even though shmctl is not used to remove it

I am using System V shared memory in my program. The writer process successfully creates the shared memory using shmget(). When the writer process ends, I notice that the shared memory segment disappears, even though I do not call shmctl() with IPC_RMID to remove it.

Here is the code for the writer:

// writer.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

#define SHM_KEY 0x1234
#define SHM_SIZE sizeof(int)

int main() {
    int shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0666);
    if (shmid == -1) {
        perror("shmget failed");
        exit(1);
    }

    int *shm_ptr = (int *)shmat(shmid, NULL, 0);
    if (shm_ptr == (void *)-1) {
        perror("shmat failed");
        exit(1);
    }

    *shm_ptr = 42;
    printf("Writer: wrote %d to shared memory.\n", *shm_ptr);

    // Keeping writer alive
    printf("Writer: sleeping for 30 seconds...\n");
    sleep(30);

    shmdt(shm_ptr);
    return 0;
}

Here is the code for the reader:

// reader.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SHM_KEY 0x1234
#define SHM_SIZE sizeof(int)

int main() {
    int shmid = shmget(SHM_KEY, SHM_SIZE, 0666);
    if (shmid == -1) {
        perror("shmget failed");
        exit(1);
    }

    int *shm_ptr = (int *)shmat(shmid, NULL, 0);
    if (shm_ptr == (void *)-1) {
        perror("shmat failed");
        exit(1);
    }

    printf("Reader: read %d from shared memory.\n", *shm_ptr);
    shmdt(shm_ptr);
    return 0;
}

When I run the writer, I can see the shared memory using ipcs -m. However, after the writer finishes and exits, the shared memory segment disappears, and I can’t access it with the reader.

What could be causing this behavior? Is there a system setting that might cause shared memory to be deleted automatically after the writer exits? This program works correctly on macOS, where the shared memory persists after the writer exits. However, it fails on Linux: the shared memory segment disappears once the writer process ends, and I cannot access it with the reader.

I’m using Ubuntu Linux 5.15.0-127-generic.

Thank you for your attention, and I would appreciate any additional insights on this topic!

like image 205
StaY_Hungry Avatar asked Oct 26 '25 05:10

StaY_Hungry


1 Answers

I am the OP of this question. Thank you all for your attention!

I just figured out the reason behind this unexpected behavior. It seems to be related to the Linux kernel configuration -- kernel.shm_rmid_forced. When I changed it with the following command:

sudo sysctl -w kernel.shm_rmid_forced=0

everything started working as expected.

I hope this answer make sense to you!

like image 78
StaY_Hungry Avatar answered Oct 27 '25 19:10

StaY_Hungry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!