레코드(record) 단위로 lock을 설정할 수 있도록 하는 함수
#include <fcntl.h>
int fcntl(int fildes, int cmd, struct flock *lock);
struct flock {
...
short l_type; // lock type : 1) F_RDLCK 2) F_WRLCK 3) F_UNLCK
short l_whence; // offset : 1)SEEK_SET 2) SEEK_CUR 3)SEEK_END
off_t l_start; // 시작위치
off_t l_len; // lock 범위(길이) : 0이면 file 전체에 lock
off_t l_pid; // lock을 가진 프로세스의 pid (F_GETLK와 함께 사용)
#include <fcntl.h>
int flock(int fd, int operation);
Advisory locking
- 프로그래머의 책임
- 커널의 개입이 없음
- read나 write 동작이 locking protocol을 위반할 수 있음
- 프로세스들이 자율적으로 설정한 locking protocol을 준수해야 함
Mandatory locking
- 커널이 locking protocol을 강제적으로 따르게 만듦
- read와 write 동작은 모두 locking protocol을 따름
- 모든 read, write 동작에 대해 커널이 check하므로 overhead가 크다
- set-group-ID bit는 켜고 group-excute bit를 꺼서 설정함
($ chmod 2644 lockfile 또는 $ chmod g+s, g-x lockfile)