ftruncate()¶
Purpose¶
Sets the size of a file or POSIX shared memory object.
Header¶
Prototype¶
Parameters¶
fd: open file descriptor.length: target size in bytes.
Return Value¶
- Success: returns
0. - Failure: returns
-1and setserrno.
Minimal Example¶
int fd = shm_open("/demo_shm", O_CREAT | O_RDWR, 0600);
if (fd >= 0) {
ftruncate(fd, sizeof(struct shared_data));
}
Common Pitfalls¶
- POSIX shared memory objects have size 0 after creation.
- Call
ftruncate()beforemmap()when creating the object. - Existing mappings are not automatically revalidated if the object is resized incorrectly.