- 论坛徽章:
- 0
|
如果是两个进程的话,用fcntl()设置锁,参考APUE 14.3:
#include <fcntl.h>
int fcntl(int filedes, int cmd, ... /* struct
flock *flockptr */ );
Returns: depends on cmd if OK (see following), 1 on error
For record locking, cmd is F_GETLK, F_SETLK, or F_SETLKW. The third argument (which we'll call flockptr) is a pointer to an flock structure.
struct flock {
short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */
off_t l_start; /* offset in bytes, relative to l_whence */
short l_whence; /* SEEK_SET, SEEK_CUR, or SEEK_END */
off_t l_len; /* length, in bytes; 0 means lock to EOF */
pid_t l_pid; /* returned with F_GETLK */
};
This structure describes
The type of lock desired: F_RDLCK (a shared read lock), F_WRLCK (an exclusive write lock), or F_UNLCK (unlocking a region)
The starting byte offset of the region being locked or unlocked (l_start and l_whence)
The size of the region in bytes (l_len)
The ID (l_pid) of the process holding the lock that can block the current process (returned by F_GETLK only) |
|