unix - why vi can modify a file while this file is write locked? -
i compile file , run in 1 console.
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> int main(int argc, char *argv[]) { /* l_type l_whence l_start l_len l_pid */ struct flock fl = {f_wrlck, seek_set, 0, 0, 0 }; int fd; fl.l_pid = getpid(); if (argc > 1) fl.l_type = f_rdlck; if ((fd = open("lockdemo.c", o_rdwr)) == -1) { perror("open"); exit(1); } printf("press <return> try lock: "); getchar(); printf("trying lock..."); if (fcntl(fd, f_setlkw, &fl) == -1) { perror("fcntl"); exit(1); } printf("got lock\n"); printf("press <return> release lock: "); getchar(); fl.l_type = f_unlck; /* set unlock same region */ if (fcntl(fd, f_setlk, &fl) == -1) { perror("fcntl"); exit(1); } printf("unlocked.\n"); close(fd); return 0; }
it outputs:
zj:~/documents/c$ ./a.out press <return> try lock: trying lock...got lock press <return> release lock:
i open console , vi lockdemo.c , had modified lockdemo.c successfully. why? isn't file locked? while open console
zj:~/documents/c$ ./a.out press <return> try lock:
the a.out running getchar(), , cannot execute printf("trying lock..."); totally confused.
you applying advisory lock on lockdemo.c
file. vi
free ignore design. should have used mandatory lock afaik isn't standardized under unix enforce vi
not it.
Comments
Post a Comment