Trying to understand flock()
Greetings friends, I wonder if someone would be kind enough to enlighten me about the semantics of the flock(2) function. I have RTFM'd, and I am sad to say that I am still rather mystified that flock() doesn't seem to do what it is documented as doing. (I am testing it on 4.10-RELEASE, by the way.) The short test program attached below illustrates the source of my abundant confusion. When I compile this program with -DUSE_FCNTL (thus forcing it to use fcntl(2) to implement exclusive file locking) and then execute it, the resulting behavior is exactly what I expect, i.e. the program prints the string "Temp file locked (1)", and then it pauses for 10 seconds, and then it prints "Temp file locked (2)". The delay time between the appearance of the two message indicates clearly that exclusive file locking is working as expected. When I compile this program WITHOUT the -DUSE_FCNTL option however (thus forcing the program to use flock() rather then fcntl() for file locking), there is no apparent delay between the printing of the first message and the printing of the second message. That is what has me mystified. Obviously, there is something (or maybe several things) about the actual semantics of flock(2) that I don't understand. I would appreciate it if someone would enlighten me about that. Regards, rfg P.S. My apologies in advance if you try to Cc: me directly on your reply to this posting, and if your response gets rejected by the local spam filters. It's nothing personal. Really. We just have about 2/5ths of the entire Internet blacklisted here due to past spamming incidents. I will look for replies also in the freebsd-general list archives, so if you prefer, you can just repl to the list. Thanks and hasta la vista. #include #include #include #include #include #include static void die (register char const *const fmt) { fprintf (stderr, fmt, strerror (errno)); fprintf (stderr, "\n"); exit (1); } static int lock_exclusive (register int const fd) { #if USE_FCNTL auto struct flock fl; fl.l_start = 0; fl.l_len = 0; fl.l_pid = 0; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; return fcntl (fd, F_SETLKW, &fl); #else return flock (fd, LOCK_EX); #endif } int main (void) { static char template[] = "/tmp/temp.XX"; register int fd; fd = mkstemp (template); unlink (template); if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (1)\n"); if (fork () == 0) { if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (2)\n"); } sleep (10); close (fd); return 0; } ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Trying to understand flock()
Ronald F. Guilmette wrote: Greetings friends, I wonder if someone would be kind enough to enlighten me about the semantics of the flock(2) function. I have RTFM'd, and I am sad to say that I am still rather mystified that flock() doesn't seem to do what it is documented as doing. (I am testing it on 4.10-RELEASE, by the way.) The short test program attached below illustrates the source of my abundant confusion. When I compile this program with -DUSE_FCNTL (thus forcing it to use fcntl(2) to implement exclusive file locking) and then execute it, the resulting behavior is exactly what I expect, i.e. the program prints the string "Temp file locked (1)", and then it pauses for 10 seconds, and then it prints "Temp file locked (2)". The delay time between the appearance of the two message indicates clearly that exclusive file locking is working as expected. When I compile this program WITHOUT the -DUSE_FCNTL option however (thus forcing the program to use flock() rather then fcntl() for file locking), there is no apparent delay between the printing of the first message and the printing of the second message. That is what has me mystified. Obviously, there is something (or maybe several things) about the actual semantics of flock(2) that I don't understand. I would appreciate it if someone would enlighten me about that. Regards, rfg P.S. My apologies in advance if you try to Cc: me directly on your reply to this posting, and if your response gets rejected by the local spam filters. It's nothing personal. Really. We just have about 2/5ths of the entire Internet blacklisted here due to past spamming incidents. I will look for replies also in the freebsd-general list archives, so if you prefer, you can just repl to the list. Thanks and hasta la vista. #include #include #include #include #include #include static void die (register char const *const fmt) { fprintf (stderr, fmt, strerror (errno)); fprintf (stderr, "\n"); exit (1); } static int lock_exclusive (register int const fd) { #if USE_FCNTL auto struct flock fl; fl.l_start = 0; fl.l_len = 0; fl.l_pid = 0; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; return fcntl (fd, F_SETLKW, &fl); #else return flock (fd, LOCK_EX); #endif } int main (void) { static char template[] = "/tmp/temp.XX"; register int fd; fd = mkstemp (template); unlink (template); if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (1)\n"); if (fork () == 0) { if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (2)\n"); } sleep (10); close (fd); return 0; } ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]" I am on 5.3RC1, and I know your version is not the same, but have you updated you docs? It works just like updating the system. From my manpage you are missing #include (I see you have this, but I will leave it anyway) #define LOCK_SH0x01 /* shared file lock */ #define LOCK_EX0x02 /* exclusive file lock */ #define LOCK_NB0x04 /* don't block when locking */ #define LOCK_UN0x08 /* unlock file */ int flock(int fd, int operation); An example: int flock(int fd, int LOCK_EX); Where is the above in your code? I must say I have only done some generic C programming for my school class, but this seems easly enough. I hope it is outof date docs on your system causing you pain. Jason ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Trying to understand flock()
Ronald F. Guilmette wrote: Greetings friends, I wonder if someone would be kind enough to enlighten me about the semantics of the flock(2) function. I have RTFM'd, and I am sad to say that I am still rather mystified that flock() doesn't seem to do what it is documented as doing. (I am testing it on 4.10-RELEASE, by the way.) The short test program attached below illustrates the source of my abundant confusion. When I compile this program with -DUSE_FCNTL (thus forcing it to use fcntl(2) to implement exclusive file locking) and then execute it, the resulting behavior is exactly what I expect, i.e. the program prints the string "Temp file locked (1)", and then it pauses for 10 seconds, and then it prints "Temp file locked (2)". The delay time between the appearance of the two message indicates clearly that exclusive file locking is working as expected. When I compile this program WITHOUT the -DUSE_FCNTL option however (thus forcing the program to use flock() rather then fcntl() for file locking), there is no apparent delay between the printing of the first message and the printing of the second message. That is what has me mystified. Obviously, there is something (or maybe several things) about the actual semantics of flock(2) that I don't understand. I would appreciate it if someone would enlighten me about that. Regards, rfg P.S. My apologies in advance if you try to Cc: me directly on your reply to this posting, and if your response gets rejected by the local spam filters. It's nothing personal. Really. We just have about 2/5ths of the entire Internet blacklisted here due to past spamming incidents. I will look for replies also in the freebsd-general list archives, so if you prefer, you can just repl to the list. Thanks and hasta la vista. #include #include #include #include #include #include static void die (register char const *const fmt) { fprintf (stderr, fmt, strerror (errno)); fprintf (stderr, "\n"); exit (1); } static int lock_exclusive (register int const fd) { #if USE_FCNTL auto struct flock fl; fl.l_start = 0; fl.l_len = 0; fl.l_pid = 0; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; return fcntl (fd, F_SETLKW, &fl); #else return flock (fd, LOCK_EX); #endif } int main (void) { static char template[] = "/tmp/temp.XX"; register int fd; fd = mkstemp (template); unlink (template); if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (1)\n"); if (fork () == 0) { if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (2)\n"); } sleep (10); close (fd); return 0; } Greetings! From the flock manpage: "...file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock, but rather multiple references to the same lock." You're basically trying to place a lock you already hold, making the flock function return immediately (this is what I gather, anyhow). The fcntl function seems to operate slightly differently in this respect. This is the only explanation I can think of, others might think differently, though. -- Henrik W Lund ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"
Re: Trying to understand flock()
In message <[EMAIL PROTECTED]>, you wrote: > From the flock manpage: > >"...file descriptors duplicated through dup(2) or fork(2) do not result >in multiple instances of a lock, but rather multiple references to the >same lock." > >You're basically trying to place a lock you already hold, making the >flock function return immediately (this is what I gather, anyhow). The >fcntl function seems to operate slightly differently in this respect. > >This is the only explanation I can think of, others might think >differently, though. Thank you. That seems to make sense, now that you have explained it for me. ___ [EMAIL PROTECTED] mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-questions To unsubscribe, send any mail to "[EMAIL PROTECTED]"