Eric
Eric

Reputation: 768

Why shm_open() succeeds even there is no /dev/shm?

There is no /dev/shm in my Linux machine (I manually unmounted and removed it), but when I try shm_open() in my application, like shm_open("foo", O_CREAT | O_RDWR, 0666);, it still succeeds and returns 3 as the fd. So in this case where is the shared memory created? Can it still be shared with other processes?

And in this case why does shm_open only succeed when it runs as root user but fail as any non-root users?

Upvotes: 1

Views: 1257

Answers (2)

YouJiacheng
YouJiacheng

Reputation: 687

https://github.com/bminor/glibc/blob/4290aed05135ae4c0272006442d147f2155e70d7/NEWS#L892-L895

It seems that glibc<2.34 will search among the system's mount points for a suitable replacement if /dev/shm is not available.

Upvotes: 0

emmrk
emmrk

Reputation: 276

shm_open creates a shared memory object if it doesn't exist.

Your fd should be a valid file descriptor you may use with mmap to share this memory area with other processes.


When you unmounted and "removed" the /dev/shm mount point, nothing changed really. It only serves as an access point to have a RAM-based filesystem.

Upvotes: 1

Related Questions