flock

FLOCK(2)                   Linux Programmer's Manual                  FLOCK(2)



åå
       flock - ãªã¼ãã³ããããã¡ã¤ã«ã«å¯¾ããã¢ããã¤ã¶ãªã»ããã¯ã®é©ç¨ã解é¤ãè¡ã

æ¸å¼
       #include <sys/file.h>

       int flock(int fd, int operation);

説æ
       ãªã¼ãã³ããããã¡ã¤ã«ã«ã¢ããã¤ã¶ãªã»ãã㯠(advisory lock) ã®é©ç¨ ã解é¤ãè¡ãã
       ãã¡ã¤ã«ã¯ fd ã§æå®ãããå¼ãæ° operation ã«ã¯ä»¥ä¸ã®ããããä¸ã¤ãæå®ãã:

           LOCK_SH  å±æããã¯ãé©ç¨ããã æå®ãããã¡ã¤ã«ã«å¯¾ãã¦ã ä¸ã¤ä»¥ä¸ã®ãã‐
                    ã»ã¹ãåæã«å±æããã¯ãä¿æãããã¨ãã§ããã

           LOCK_EX  æä»ããã¯ãé©ç¨ããã æå®ãããã¡ã¤ã«ã«å¯¾ãã¦ã ãã ä¸ã¤ã®ãã‐
                    ã»ã¹ã ããåæã«æä»ããã¯ãä¿æãããã¨ãã§ããã

           LOCK_UN  ãã®ããã»ã¹ãä¿æãã¦ããæ¢åã®ããã¯ã解é¤ããã

       flock()  ãå¼ã³åºããã¨ãã«ãæå®ããããã¯ç¨®å¥ã¨ç°ãªãããã¯ãå¥ããã»ã¹ã«ãã£ã¦
       ä¿æããã¦ããã¨ã flock()  ã¯åæ¢ (block) ããããã¨ãããã éåæ¢ (nonblocking)
       ã¿ã¤ãã®è¦æ±ãè¡ãããã«ã¯ã ä¸è¨ã®æä½ (operation) ã« LOCK_NB
       ãè«çåã®å½¢ã§æå®ããã

       ä¸ã¤ã®ãã¡ã¤ã«ã«å±æããã¯ã¨æä»ããã¯ãåæã«è¨å®ãããã¨ã¯ã§ããªãã

       flock()  ã«ãã£ã¦ä½ãããããã¯ã¯ã
       ãªã¼ãã³ããããã¡ã¤ã«ã®ãã¼ãã«ã»ã¨ã³ããªã¨é¢é£ä»ããããã
       ãããã£ã¦ããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã®è¤è£½ (fork(2)  ã dup(2)
       ãªã©ã«ããä½æããã) ã¯åãããã¯ãåç§ãã ãããã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã®ã©ãã使ã£ã¦ã
       ãã®ããã¯ãå¤æ´ããã解æ¾ãããã§ããã ã¾ããããã¯ã®è§£æ¾ã¯ã
       ä¸è¨ã®è¤æ°ã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã®ããããã«å¯¾ã㦠æ示çã« LOCK_UN
       æä½ãæ示ããå ´åãããããã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ããã¹ã¦ éããããå ´åã«è¡ãããã

       ããããã»ã¹ã open(2)  (ãããã¯åæ§ã®æ¹æ³) ã使ã£ã¦åããã¡ã¤ã«ã«å¯¾ãã¦
       è¤æ°ã®ãã£ã¹ã¯ãªãã¿ãåå¾ããå ´åã flock()
       ã¯ãããè¤æ°ã®ãã£ã¹ã¯ãªãã¿ãåãç¬ç«ã®ãã®ã¨ãã¦æ±ãã
       ãããã®ãã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã®ä¸ã¤ã使ã£ã¦ãã¡ã¤ã«ãããã¯ããã㨠ããéããã®ã‐
       ãã¯è¦æ±ã¯ãå¼ã³åºãåã®ããã»ã¹ããã®ãã¡ã¤ã«ã®å¥ã® ãã£ã¹ã¯ãªãã¿çµç±ã§ãã§ã«è¨‐
       å®ãã¦ããããã¯ã«ãã£ã¦æå¦ãããå ´åãããã

       ä¸ã¤ã®ããã»ã¹ã¯ãä¸ã¤ã®ãã¡ã¤ã«ã«å¯¾ã㦠(å±æããã¯ã¨æä»ããã¯ã®ãã¡)
       ããããä¸ç¨®é¡ã®ããã¯ããè¨å®ã§ããªãã æ¢ã«ããã¯ããããã¡ã¤ã«ã«å¯¾ã㦠flock()
       ãå¼ã³åºãã¨ãæ¢åã®ããã¯ãæ°ããããã¯ã¢ã¼ãã«å¤æ´ãããã¨ã«ãªãã

       flock()  ã«ããä½æãããããã¯ã¯ execve(2)  ã®åå¾ã§ä¿åãããã

       å±æããã¯ãæä»ããã¯ãããã¡ã¤ã«ãã©ã®ã¢ã¼ãã§ãªã¼ãã³ããããã«
       é¢ä¿ãªãé©ç¨ãããã¨ãã§ããã

è¿ãå¤
       æåããå ´å㯠0 ãè¿ããããã¨ã©ã¼ã®å ´å㯠-1 ãè¿ããã errno ãé©åã«è¨å®ãããã

ã¨ã©ã¼
       EBADF  fd ããªã¼ãã³ããããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã§ã¯ãªãã

       EINTR  ããã¯ã®ç²å¾ãå¾ã£ã¦ããéã«ããã³ãã©ã«ããææãããã·ã°ãã«ã åä¿¡ãã flock()
              ãä¸æãããã signal(7)  åç§ã

       EINVAL oepration ãç¡å¹ã§ããã

       ENOLCK ããã¯ã»ã¬ã³ã¼ããå²ãå½ã¦ãããã®ã¡ã¢ãªãä¸è¶³ãã¦ããã

       EWOULDBLOCK
              æå®ãããã¡ã¤ã«ãããã¯ããã¦ããã LOCK_NB ãã©ã°ãæå®ããã¦ããã

æºæ
       4.4BSD (flock()  ã³ã¼ã«ã¯ 4.2BSD ã§æåã«ç»å ´ãã)ã fcntl(2)
       ã§å®è£ããã¦ãããã®ãªã©ãå«ããã¨ã flock()  ã®æ©è½ã¯ã»ã¨ãã©ã® UNIX
       ã·ã¹ãã ã§å®è£ããã¦ããã

注æ
       flock()  㯠NFS ä¸ã®ãã¡ã¤ã«ã®ããã¯ãããªãã代ããã« fcntl(2)
       ã使ç¨ãããã¨ãããã«ãããååã«æ°ãããã¼ã¸ã§ã³ã® Linux ã¨ãããã¯æ©è½ã ãµãã¼ããã NFS
       ãµã¼ãã使ç¨ãããã¨ã«ãããNFS ä¸ã§ããã¯ãã§ããã

       kernel 2.0 以éã§ã¯ã flock()  ã¯ãGNU C ã©ã¤ãã©ãªã§ã® fcntl(2)
       ãå¼ã³åºãã¦ã®ã¨ãã¥ã¬ã¼ã·ã§ã³ã§ã¯ãªãã
       ããèªä½ãã·ã¹ãã ã³ã¼ã«ã¨ãã¦å®è£ããã¦ããã ããã«ããæ£çæ£éã® BSD ã§ã®åä½ãéæããã:
       flock()  㨠fcntl(2)  ã§é©ç¨ãããããã¯ã®ç¨®å¥ã«ã¯ç¸äºä½ç¨ããªããªãã flock()
       ããããããã¯ãæ¤åºããªããªãã

       flock()  ã¢ããã¤ã¶ãªã»ããã¯ã ããé©ç¨ããããããã£ã¦ããã¡ã¤ã«ã«é©åãªã¢ã¯ã»ã¹æ¨©ã
       ä»ä¸ãã¦ããã°ãããã»ã¹ã¯ flock()
       ã®ä½¿ç¨ã«ç¡è¦ãã¦ããã¡ã¤ã«ã¸ã®å¥åºåãè¡ããã¨ãã§ããã

       flock()  㨠fcntl(2)  㯠fork ãããããã»ã¹ã¨ dup(2)  ã§éã£ãåä½ãããã flock()
       ã fcntl(2)  ã使ã£ã¦å®è£ãã¦ããã·ã¹ãã ã§ã¯ã flock()
       ã®åä½ã¯ãã®ããã¥ã¢ã«ã»ãã¼ã¸ã«è¨è¼ããã¦ãããã®ã¨ã¯éãã ããã

       ããã¯ã®å¤æ (å±æããã¯ããæä»ããã¯ã¸ããããã¯ãã®å対) ãã¢ãããã¯ã«
       è¡ããããã¨ã¯ä¿è¨¼ããã¦ããªã: æ¢åã®ããã¯ãã¾ãåé¤ãããããããæ°ãã ããã¯ãè¨å®ãããããã®
       2ã¤ã®ã¹ãããã®éã«ãä»ã®ããã»ã¹ããã®å¦çå¾ã¡ã® ã‐
       ãã¯è¦æ±ãèªããããããããããçµæã¨ãã¦å¤æã¯åæ¢ (block) ãããã (LOCK_NB
       ãæå®ãããå ´åã«ã¯) 失æãããããã (ããã¯åãã® BSD
       ã®åä½ã§ãããå¤ãã®ä»ã®å®è£ã§ãèµ·ããã)

é¢é£é ç®
       flock(1), close(2), dup(2), execve(2), fcntl(2), fork(2), open(2),
       lockf(3)

       Linux ã«ã¼ãã«ã½ã¼ã¹åã® Documentation/filesystem/locks.txt
       (以åã®ã«ã¼ãã«ã§ã¯ Documentation/locks.txt)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.51 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2013-02-11                          FLOCK(2)