Skip to content
Advertisement

shm_open and ftruncate() in Shared Memory Programming

I want to create a Shared Memory Object and truncate it to a specific size.

SHMSIZE is defined with 512

MODE is set with S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IWOTH | S_IROTH

Here is my Code

JavaScript

While Debugging i watched that the return value of shm_open() is 0 every time, but i can see this object in /dev/shm. And while executing ftruncate() it returns every time with error “invalid argument”.

Why fd is 0 every time and why ftruncate doesn’t work? What should i do?

Advertisement

Answer

The order of operations in this statement is wonky:

JavaScript

You’re assigning the result of shm_open(...) < 0 to fd, which is definitely not what you want.

Move the comparison outside the parentheses:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement