write

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



åå
       write - ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ (file descriptor) ã«æ¸ãè¾¼ã

æ¸å¼
       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);

説æ
       write()  ã¯ã buf ãæããããã¡ããããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ fd ãåç§ãããã¡ã¤ã«ã¸ãæ大
       count ãã¤ããæ¸ãè¾¼ãã

       æ¸ãè¾¼ã¾ãããã¤ãæ°ã¯ count ãããå°ãããªããã¨ãããã
       ä¾ãã°ãæ¸ãè¾¼ã¿å¯¾è±¡ã®ç©çã¡ãã£ã¢ã«ååãªé åããªãå ´åã ãªã½ã¼ã¹ä¸é
       RLIMIT_FSIZE ã«éããå ´å (setrlimit(2)  åç§)ã count
       ãã¤ãæªæºã®æ¸ãè¾¼ã¿ãè¡ãããå¾ã§ å¼ã³åºããã·ã°ãã«ãã³ãã©ã«ããå²ãè¾¼ã¾ããå ´åã
       ãªã©ã§ããã (pipe(7)  ãåç§ã®ãã¨ã)

       seek å¯è½ãªãã¡ã¤ã« (ã¤ã¾ã lseek(2)
       ãé©ç¨ã§ãããã¡ã¤ã«ãä¾ãã°é常ã®ãã¡ã¤ã«) ã§ã¯ã
       æ¸ãè¾¼ã¿ã¯ç¾å¨ã®ãã¡ã¤ã«ãªãã»ããããè¡ããã
       ãã¡ã¤ã«ãªãã»ããã¯å®éã«æ¸ãè¾¼ã¿ãè¡ããããã¤ãæ°å å ç®ãããããã¡ã¤ã«ã O_APPEND ã§
       open(2)  ãããå ´åããã¡ã¤ã«ãªãã»ããã¯æ¸ãè¾¼ã¿åã« ãã¡ã¤ã«ã®æ«å°¾ã«è¨å®ãããã
       ãã¡ã¤ã«ãªãã»ããã®èª¿æ´ã¨æ¸ãè¾¼ã¿æä½ã¯ã¢ãããã¯ãªå¦çã¨ã㦠å®è¡ãããã

       POSIX 㯠write()  ãè¡ãªãããå¾ã«å®è¡ãã read(2)  ã
       æ°ãããã¼ã¿ãè¿ããã¨ãè¦æ±ãã¦ããã å¨ã¦ã®ãã¡ã¤ã«ã·ã¹ãã ã POSIX
       æºæ ã§ã¯ãªãç¹ã«æ³¨æãããã¨ã

è¿ãå¤
       æåããå ´åãæ¸ãè¾¼ã¾ãããã¤ãæ°ãè¿ããã (ã¼ãã¯ä½ãæ¸ãè¾¼ã¾ããªãã£ããã¨ã示ã)ã
       ã¨ã©ã¼ãªãã° -1 ãè¿ãããerrno ãé©åã«è¨å®ãããã

       count ã 0 ã§ã fd ãé常ã®ãã¡ã¤ã« (regular file) ãåç§ãã¦ããå ´åã write()
       ã¯å¾è¿°ã®ã¨ã©ã¼ã®ãããããæ¤åºããå ´åã失æãè¿ããã¨ãããã
       ã¨ã©ã¼ãæ¤åºãããªãã£ãå ´åã¯ã 0 ãè¿ããä»ã«ä½ã®å½±é¿ãä¸ããªãã count ã 0 ã§ã
       fd ãé常ã®ãã¡ã¤ã«ä»¥å¤ã®ãã¡ã¤ã«ãåç§ãã¦ããå ´åã ãã®çµæã¯è¦å®ããã¦ããªãã

ã¨ã©ã¼
       EAGAIN ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ fd ãã½ã±ãã以å¤ã®ãã¡ã¤ã«ãåç§ãã¦ãã¦ã éåæ¢
              (nonblocking) ã¢ã¼ã (O_NONBLOCK) ã«è¨å®ããã¦ãããæ¸ãè¾¼ã¿ãè¡ãã¨åæ‐
              ¢ããç¶æ³ã«ããã

       EAGAIN ã¾ã㯠EWOULDBLOCK
              ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ fd ãã½ã±ãããåç§ãã¦ãã¦ãéåæ¢ (nonblocking)
              ã¢ã¼ã (O_NONBLOCK) ã«è¨å®ããã¦ãããæ¸ãè¾¼ã¿ãè¡ãã¨åæ¢ããç¶æ³ã«ããã
              POSIX.1-2001 ã¯ããã®å ´åã«ã©ã¡ãã®ã¨ã©ã¼ãè¿ããã¨ãèªãã¦ããã ããã 2
              ã¤ã®å®æ°ãåãå¤ãæã¤ãã¨ãæ±ãã¦ããªãã
              ãããã£ã¦ã移æ¤æ§ãå¿è¦ãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯ã両æ¹ã®å¯è½æ§ã
              確èªãã¹ãã§ããã

       EBADF  fd ãæå¹ãªãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã§ãªããæ¸ãè¾¼ã¿ã®ããã«ãªã¼ãã³ (open)
              ããã¦ããªãã

       EDESTADDRREQ
              fd ãã connect(2)  ã使ã£ã¦éä¿¡ç¸æã®ã¢ãã¬ã¹ãè¨‐
              å®ããã¦ããªããã¼ã¿ã°ã©ã ã½ã±ããã åç§ãã¦ããã

       EDQUOT The user's quota of disk blocks on the file system containing
              the file referred to by fd has been exhausted.

       EFAULT buf ãã¢ã¯ã»ã¹å¯è½ãªã¢ãã¬ã¹ç©ºéã®å¤ã«ããã

       EFBIG  å®è£å®ç¾©ã®æ大ãã¡ã¤ã«ãµã¤ãºã¾ãã¯ããã»ã¹ã®ãã¡ã¤ã«ãµã¤ãºå¶éã
              è¶ãã¦ãã¡ã¤ã«ã«æ¸ãè¾¼ããã¨ããã
              ã¾ãã¯è¨±å¯ããããªãã»ããå¤ã®éçãè¶ããåã®ä½ç½®ã« æ¸ãè¾¼ããã¨ããã

       EINTR  ä½ã®ãã¼ã¿ãæ¸ããªãéã«ã·ã°ãã«ã«ããå²ãè¾¼ã¾ãã (interrupt)ã signal(7)
              åç§ã

       EINVAL fd ãæ¸ãè¾¼ã¿ãä¸é©åãªãªãã¸ã§ã¯ããåç§ãã¦ããã ãããã¯ããã¡ã¤ã«ã O_DIRECT
              ãã©ã°ãæå®ãã¦ãªã¼ãã³ããã¦ãããã buf ã«æå®ãããã¢ãã¬ã¹ã count
              ã«æå®ãããå¤ã ç¾å¨ã®ãã¡ã¤ã«ãªãã»ããã®ããããã®
              ã¢ã©ã¤ã³ã¡ã³ããä¸é©åã§ããã

       EIO    inode ã®ä¿®æ£ä¸ã«ä½ã¬ã¹ã« (low-level) I/O ã¨ã©ã¼ãçºçããã

       ENOSPC fd ã«ãã£ã¦åç§ããããã¡ã¤ã«ãå«ãããã¤ã¹ (device) ã«ååãªç©ºãããªãã

       EPIPE  fd ããã¤ã (pipe) ãã½ã±ãã (socket) ã«æ¥ç¶ããã¦ããã ãã®åå¯¾å´ (èª‐
              ã¿è¾¼ã¿å´) ãã¯ãã¼ãº (close) ããã¦ããã
              ãããçºçããå ´åã«ã¯ãæ¸ãè¾¼ã¿ãè¡ãªãããã»ã¹ã¯ SIGPIPE ã·ã°ãã«
              (signal)ãåããã (ãããã£ã¦ãããã°ã©ã ããã®ã·ã°ãã«ãæç² (catch)ãåæ¢
              (block)ãç¡è¦ (ignore)  ããå ´åã®ã¿ãwrite ã®è¿ãå¤ãåç§ã§ããã)

       fd ã«æ¥ç¶ããããªãã¸ã§ã¯ãã«ãã£ã¦ã¯ãä»ã®ã¨ã©ã¼ãèµ·ãããããããªãã

æºæ
       SVr4, 4.3BSD, POSIX.1-2001.

       SVr4 ã§ã¯ write ãå²ãè¾¼ã¾ããã¨ããã¼ã¿ãæ¸ãè¾¼ã¾ããç´åã§ã¯ãªãã ãã®æç¹ã§
       EINTR ãè¿ãã

注æ
       write()  ãæåãã¦è¿ã£ã¦ãã¦ãããã¼ã¿ããã£ã¹ã¯ã«è¨é²ããããã¨ã ä¿è¨¼ãããã®ã§ã¯ãªãã
       å®éããã¼ã¿ã®ããã®ã¹ãã¼ã¹ã確ä¿ããããã¨ããä¿è¨¼ãããªãã¨ãã ãã°ã£ã½ãå®è£ãããã
       ããã確å®ã«ããå¯ä¸ã®æ¹æ³ã¯ã å¨ã¦ã®ãã¼ã¿ã write ããå¾ã« fsync(2)
       ãå¼ã³åºããã¨ã§ããã

       write()  ã 1 ãã¤ããæ¸ãè¾¼ã¾ãªããã¡ã«ã·ã°ãã«ãã³ãã©ã«ããå²ãè¾¼ã¾ããå ´åã
       write()  ã¯ã¨ã©ã¼ EINTR ã§å¤±æããã
       1ãã¤ãã§ãæ¸ãè¾¼ãã å¾ã§å²ãè¾¼ã¾ããå ´åã«ã¯ã write()
       ã¯æåããæ¸ãè¾¼ãã ãã¤ãæ°ãè¿ãã

é¢é£é ç®
       close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2),
       read(2), select(2), writev(2), fwrite(3)

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



Linux                             2013-01-27                          WRITE(2)