lockf( fd, mode, size );
mode 为 1 时表示加锁,为 0 时表示解锁。
#include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<stdlib.h> int main() { pid_t pid; int retval; ] = "hello"; ) { printf("fork error\n"); exit(-); } ) { ) { ,,) < ) { printf("lockf on error\n"); exit(-); } sleep(); printf("this is in child!\n"); sleep(); printf("this is in child!\n"); ,,) < ) { printf("lockf off error\n"); exit(-); } sleep(); } printf("this is child end\n"); exit(-); } //wait(&retval); ) { ,,) < ) { printf("lockf on error\n"); exit(-); } sleep(); printf("this is in parent!\n"); sleep(); printf("this is in parent!\n"); ,,) < ) { printf("lockf off error\n"); exit(-); } sleep(); } printf("this is parent process end\n"); ; }