main() { char buf[1]; int fd=open("./test.tmp",O_RDWR); buf[1]='c'; write(fd,buf,1); write_lock(fd, -1, SEEK_END, 0); write(fd,buf,1); un_lock(fd, 0, SEEK_END,0); write(fd,buf,1); if(fork()==0){ if(is_write_lockable(fd, -2, SEEK_END, 1)) puts("the last third is not locked"); else puts("the last third is locked"); if(is_write_lockable(fd, -1, SEEK_END, 1)) puts("the last third is not locked"); else puts("the last third is locked"); if(is_write_lockable(fd, -0, SEEK_END, 1)) puts("the last third is not locked"); else puts("the last third is locked"); } }
int main() { int fd; int pid; fd = open(LOCK_FILE, O_WRONLY | O_CREAT); while (1) { pid = is_write_lockable(fd, 0, SEEK_SET, 0); if (!pid) break; printf("File is locked by pid(%d)\n", pid); sleep(1); } write_lock(fd, 0, SEEK_SET, 0); printf("Lock!\n"); sleep(10); write_unlock(fd, 0, SEEK_SET, 0); printf("Unlock!\n"); close(fd); return 0; }