Penguin
Note: You are viewing an old revision of this page. View the current version.

This page describes some programming hints that might help you to write more portable C code.

File Handling

File locking

BSD-style unixes uses flock(2), which uses "advisory" locks. Ie, a process with sufficient read or write permission can ignore the lock and read/write a file. SysV-style unixes use either advisory locks or "mandatory" locks (if enabled in the filesystem), and access them by the fcntl(2) command and a struct flock object.

A more portable way (POSIX 1003.1-2001) is to use the lockf(3) function from unistd.h, which will do the correct type of locking for the unix it is compiled on. (In Linux/GNU libc, this function is a wrapper around fcntl(2)).


Part of CategoryProgramming