1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Sat Jul 24 00:06:00 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Wed Jan 17 16:02:32 1996 by Michael Haardt
28 .\" <michael@cantor.informatik.rwth-aachen.de>
29 .\" Modified Thu Apr 11 19:26:35 1996 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified Sun Jul 21 18:59:33 1996 by Andries Brouwer <aeb@cwi.nl>
31 .\" Modified Fri Jan 31 16:47:33 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Sat Jul 12 20:45:39 1997 by Michael Haardt
33 .\" <michael@cantor.informatik.rwth-aachen.de>
35 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
36 .\" all rights reserved.
37 .\" Translated 1997-02-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
38 .\" Modified 1997-09-28, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
39 .\" Updated & Modified 2005-02-24, Yuichi SATO <ysato444@yahoo.co.jp>
40 .\" Updated 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
41 .\" Updated 2005-10-14, Akihiro MOTOKI
42 .\" Updated 2008-02-10, Akihiro MOTOKI, LDP v2.77
43 .\" Updated 2009-04-13, Akihiro MOTOKI, LDP v3.20
45 .\"WORD: descriptor ¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼
46 .\"WORD: buffer ¥Ð¥Ã¥Õ¥¡¡¼
47 .\"WORD: signal ¥·¥°¥Ê¥ë
48 .\"WORD: process group ¥×¥í¥»¥¹¡¦¥°¥ë¡¼¥×
49 .\"WORD: backgraound ¥Ð¥Ã¥¯¥°¥é¥ó¥É
51 .\"WORD: open ¥ª¡¼¥×¥ó
52 .\"WORD: directory ¥Ç¥£¥ì¥¯¥È¥ê
53 .\"WORD: object ¥ª¥Ö¥¸¥§¥¯¥È
55 .TH READ 2 2009-02-23 "Linux" "Linux Programmer's Manual"
58 .\"O read \- read from a file descriptor
59 read \- ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼¤«¤éÆɤ߹þ¤à
63 .B #include <unistd.h>
65 .BI "ssize_t read(int " fd ", void *" buf ", size_t " count );
70 .\"O attempts to read up to
72 .\"O bytes from file descriptor
74 .\"O into the buffer starting at
77 ¤Ï¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼ (file descriptor)
83 ¤Ç»Ï¤Þ¤ë¥Ð¥Ã¥Õ¥¡¡¼¤ØÆɤ߹þ¤â¤¦¤È¤¹¤ë¡£
89 .\"O returns zero and has no other results.
94 .\"O the result is unspecified.
98 ¤Ï 0 ¤òÊÖ¤·¡¢Â¾¤Ë²¿¤âµ¯¤¤Ê¤¤¡£
102 ¤è¤êÂ礤±¤ì¤Ð¡¢·ë²Ì¤ÏÆÃÄê¤Ç¤¤Ê¤¤¡£
103 .\"O .SH "RETURN VALUE"
105 .\"O On success, the number of bytes read is returned (zero indicates end of
106 .\"O file), and the file position is advanced by this number.
107 .\"O It is not an error if this number is smaller than the number of bytes
108 .\"O requested; this may happen for example because fewer bytes are actually
109 .\"O available right now (maybe because we were close to end-of-file, or
110 .\"O because we are reading from a pipe, or from a terminal), or because
112 .\"O was interrupted by a signal.
113 À®¸ù¤·¤¿¾ì¹ç¡¢Æɤ߹þ¤ó¤À¥Ð¥¤¥È¿ô¤òÊÖ¤¹ (0 ¤Ï¥Õ¥¡¥¤¥ë¤Î½ª¤ê¤ò°ÕÌ£¤¹¤ë)¡£
114 ¥Õ¥¡¥¤¥ë°ÌÃ֤Ϥ³¤Î¿ô¤À¤±¿Ê¤á¤é¤ì¤ë¡£
115 ¤³¤Î¿ô¤¬Í׵ᤷ¤¿¿ô¤è¤ê¾®¤µ¤«¤Ã¤¿¤È¤·¤Æ¤â¥¨¥é¡¼¤Ç¤Ï¤Ê¤¤;
116 Î㤨¤Ðº£¤¹¤°¤Ë¤Ï¼ÂºÝ¤Ë¤½¤ì¤À¤±¤Î¿ô¤·¤«¤Ê¤¤¾ì¹ç (¥Õ¥¡¥¤¥ë¤ÎºÇ¸å¤Ë¶á¤¤¤Î¤«¤â
117 ¤·¤ì¤Ê¤¤¤·¡¢¥Ñ¥¤¥× (pipe) ¤äüËö (terminal) ¤«¤éÆɤ߹þ¤ó¤Ç¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤) ¤ä
119 ¤¬¥·¥°¥Ê¥ë (signal) ¤Ë¤è¤Ã¤Æ³ä¤ê¹þ¤Þ¤ì¤¿¾ì¹ç¤Ë¤³¤ì¤Ïµ¯¤³¤ê¤¨¤ë¡£
120 .\"O On error, \-1 is returned, and
122 .\"O is set appropriately.
123 .\"O In this case it is left unspecified whether
124 .\"O the file position (if any) changes.
125 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï¡¢\-1 ¤¬ÊÖ¤µ¤ì¡¢
127 ¤¬Å¬ÀÚ¤ËÀßÄꤵ¤ì¤ë¡£¤³¤Î¾ì¹ç¤Ï¥Õ¥¡¥¤¥ë°ÌÃÖ¤¬Êѹ¹¤µ¤ì¤ë¤«¤É¤¦¤«¤Ï
132 .\"O The file descriptor
134 .\"O refers to a file other than a socket and has been marked nonblocking
135 .\"O .RB ( O_NONBLOCK ),
136 .\"O and the read would block.
137 ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼
139 ¤¬¥½¥±¥Ã¥È°Ê³°¤Î¥Õ¥¡¥¤¥ë¤ò»²¾È¤·¤Æ¤¤¤Æ¡¢
140 ÈóÄä»ß (nonblocking) ¥â¡¼¥É
142 ¤ËÀßÄꤵ¤ì¤Æ¤ª¤ê¡¢Æɤ߹þ¤ß¤ò¹Ô¤¦¤ÈÄä»ß¤¹¤ë¾õ¶·¤Ë¤¢¤ë¡£
144 .\"O .BR EAGAIN " or " EWOULDBLOCK
145 .BR EAGAIN " ¤Þ¤¿¤Ï " EWOULDBLOCK
146 .\" Actually EAGAIN on Linux
147 .\"O The file descriptor
149 .\"O refers to a socket and has been marked nonblocking
150 .\"O .RB ( O_NONBLOCK ),
151 .\"O and the read would block.
152 .\"O POSIX.1-2001 allows either error to be returned for this case,
153 .\"O and does not require these constants to have the same value,
154 .\"O so a portable application should check for both possibilities.
155 ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼
157 ¤¬¥½¥±¥Ã¥È¤ò»²¾È¤·¤Æ¤¤¤Æ¡¢ÈóÄä»ß (nonblocking) ¥â¡¼¥É
159 ¤ËÀßÄꤵ¤ì¤Æ¤ª¤ê¡¢Æɤ߹þ¤ß¤ò¹Ô¤¦¤ÈÄä»ß¤¹¤ë¾õ¶·¤Ë¤¢¤ë¡£
160 POSIX.1-2001 ¤Ï¡¢¤³¤Î¾ì¹ç¤Ë¤É¤Á¤é¤Î¥¨¥é¡¼¤òÊÖ¤¹¤³¤È¤âǧ¤á¤Æ¤ª¤ê¡¢
161 ¤³¤ì¤é 2 ¤Ä¤ÎÄê¿ô¤¬Æ±¤¸Ãͤò»ý¤Ä¤³¤È¤âµá¤á¤Æ¤¤¤Ê¤¤¡£
162 ¤·¤¿¤¬¤Ã¤Æ¡¢°Ü¿¢À¤¬É¬Íפʥ¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤Ï¡¢Î¾Êý¤Î²ÄǽÀ¤ò
167 .\"O is not a valid file descriptor or is not open for reading.
169 ¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¼¤Ç¤Ê¤¤¤«¡¢Æɤ߹þ¤ß¤Î¤¿¤á¤Ë
170 ¥ª¡¼¥×¥ó (open) ¤µ¤ì¤Æ¤¤¤Ê¤¤¡£
174 .\"O is outside your accessible address space.
176 ¤¬¥¢¥¯¥»¥¹²Äǽ¤Ê¥¢¥É¥ì¥¹¶õ´Ö¤Î³°¤Ë¤¢¤ë¡£
179 .\"O The call was interrupted by a signal before any data was read; see
181 ²¿¤Î¥Ç¡¼¥¿¤âÆɤ߹þ¤Þ¤Ê¤¤¤¦¤Á¤Ë¥·¥°¥Ê¥ë¤Ë³ä¤ê¹þ¤Þ¤ì¤¿¡£
187 .\"O is attached to an object which is unsuitable for reading;
188 .\"O or the file was opened with the
190 .\"O flag, and either the address specified in
192 .\"O the value specified in
194 .\"O or the current file offset is not suitably aligned.
196 ¤ÏÆɤ߹þ¤ß¤ËŬ¤·¤Æ¤¤¤Ê¤¤¥ª¥Ö¥¸¥§¥¯¥È¤ò»²¾È¤·¤Æ¤¤¤ë¡£
199 ¥Õ¥é¥°¤ò»ØÄꤷ¤Æ¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤¬¡¢
201 ¤Ë»ØÄꤵ¤ì¤¿¥¢¥É¥ì¥¹¡¢
204 ¸½ºß¤Î¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤Î¤¤¤º¤ì¤«¤Î
205 ¥¢¥é¥¤¥ó¥á¥ó¥È¤¬ÉÔŬÀڤǤ¢¤ë¡£
209 .\"O was created via a call to
210 .\"O .BR timerfd_create (2)
211 .\"O and the wrong size buffer was given to
214 .\"O .BR timerfd_create (2)
215 .\"O for further information.
218 .BR timerfd_create (2)
219 ¤Î¸Æ¤Ó½Ð¤·¤ÇºîÀ®¤µ¤ì¤¿¤¬¡¢
221 ¤Ë´Ö°ã¤Ã¤¿¥µ¥¤¥º¤Î¥Ð¥Ã¥Õ¥¡¤¬ÅϤµ¤ì¤¿¡£
223 .BR timerfd_create (2)
228 .\"O This will happen for example when the process is in a
229 .\"O background process group, tries to read from its controlling tty,
230 .\"O and either it is ignoring or blocking
232 .\"O or its process group
234 .\"O It may also occur when there is a low-level I/O error
235 .\"O while reading from a disk or tape.
236 I/O ¥¨¥é¡¼¡£¤³¤ì¤ÏÎ㤨¤Ð¥×¥í¥»¥¹¤¬¥Ð¥Ã¥¯¥°¥é¥ó¥É¡¦¥×¥í¥»¥¹¡¦¥°¥ë¡¼¥×¤Ç¡¢
237 ¤½¤ì¤òÀ©¸æ¤·¤Æ¤¤¤ë tty ¤«¤éÆɤ߹þ¤â¤¦¤È¤·¡¢
239 ¤¬Ìµ»ë (ignore) ¤Þ¤¿¤Ï¶Ø»ß (blocking) ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤ä¡¢
240 ¤½¤Î¥×¥í¥»¥¹¡¦¥°¥ë¡¼¥×¤¬¸ÉΩ (orphan) ¤·¤Æ¤¤¤ë¾ì¹ç¤Ëµ¯¤³¤ë¡£
241 ¤Þ¤¿¥Ç¥£¥¹¥¯¤ä¥Æ¡¼¥×¤òÆɤó¤Ç¤¤¤ë»þ¤ËÄã¥ì¥Ù¥ë I/O ¥¨¥é¡¼
242 ¤¬È¯À¸¤·¤¿¾ì¹ç¤Ë¤âµ¯¤³¤ë¡£
246 .\"O refers to a directory.
248 ¤¬¥Ç¥£¥ì¥¯¥È¥ê¤ò»²¾È¤·¤Æ¤¤¤ë¡£
250 .\"O Other errors may occur, depending on the object connected to
253 ¤¬Àܳ¤·¤Æ¤¤¤ë¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤è¤Ã¤Æ¤Ï¾¤Î¥¨¥é¡¼¤âµ¯¤³¤ê¤¨¤ë¡£
256 .\"O that is interrupted after reading some data
257 .\"O to return \-1 (with
261 .\"O or to return the number of bytes already read.
263 ¤¤¤¯¤é¤«¤Î¥Ç¡¼¥¿¤òÆɤó¤À¸å¤Ë³ä¤ê¹þ¤ß¤¬µ¯¤³¤Ã¤¿¾ì¹ç¡¢
269 ¤òÀßÄꤷ¤Æ) \-1 ¤òÊÖ¤·¤Æ¤â¤è¤¤¤·¡¢
270 ´û¤ËÆɤ߹þ¤ó¤À¥Ð¥¤¥È¿ô¤òÊÖ¤·¤Æ¤â¤è¤¤¡£
271 .\"O .SH "CONFORMING TO"
273 SVr4, 4.3BSD, POSIX.1-2001.
276 .\"O On NFS file systems, reading small amounts of data will only update the
277 .\"O timestamp the first time, subsequent calls may not do so.
279 .\"O by client side attribute caching, because most if not all NFS clients
280 .\"O leave st_atime (last file access time)
281 .\"O updates to the server and client side reads satisfied from the
282 .\"O client's cache will not cause st_atime updates on the server as there are no
283 .\"O server side reads.
284 .\"O Unix semantics can be obtained by disabling client
285 .\"O side attribute caching, but in most situations this will substantially
286 .\"O increase server load and decrease performance.
287 NFS ¤Ë¤ª¤¤¤Æ¡£¾¯Î̤Υǡ¼¥¿¤òÆɤ߹þ¤à¾ì¹ç¡¢ºÇ½é¤Î»þ¤Î¤ß¤Ë¥¿¥¤¥à
288 ¥¹¥¿¥ó¥×¤¬¹¹¿·¤µ¤ì¡¢Â³¤¯¥³¡¼¥ë¤Ç¤Ï¹¹¿·¤µ¤ì¤Ê¤¤¤À¤í¤¦¡£
289 ¤³¤ì¤Ï¥¯¥é¥¤¥¢¥ó¥È¦¤Ç°À¤Î¥¥ã¥Ã¥·¥ó¥°¤ò¹Ô¤Ê¤¦¤¿¤á¤Ç¤¢¤ë¡£
290 ¤Ê¤¼¤Ê¤é¤Ð¡¢¤â¤·Á´¤Æ¤Î NFS ¥¯¥é¥¤¥¢¥ó¥È¤¬ st_atime (ºÇ½ª¥Õ¥¡¥¤¥ë¥¢¥¯¥»¥¹»þ¹ï)
291 ¤Î¹¹¿·¤ò¥µ¡¼¥Ð¡¼¤ËÁ÷¤é¤º¡¢¥¯¥é¥¤¥¢¥ó¥È¦¤Ç¥¥ã¥Ã¥·¥å¤òÆɤळ¤È¤ËËþ¤·¤Æ
292 ¤¤¤ì¤Ð¡¢¥µ¡¼¥Ð¡¼Â¦¤Ç¤Î read ¤ÏȯÀ¸¤·¤Ê¤¤¤Î¤Ç st_atime ¤Î¹¹¿·¤Ï¹Ô¤Ê¤ï¤ì¤«¤é¤À¡£
293 Unix ¤ÎÊý¼°¤Ç¤Ï¡¢¥¯¥é¥¤¥¢¥ó¥È¦¤Î°À¤Î¥¥ã¥Ã¥·¥ó¥°¤ò̵¸ú¤Ë¤¹¤ë¤³¤È¤Ç¡¢
294 ¤³¤ì¤òÆÀ¤ë¤³¤È¤¬¤Ç¤¤ë¡£¤·¤«¤·¤Û¤È¤ó¤É¤Î¾õ¶·¤Ç¤Ï¤³¤ì¤Ï³¤¯¥µ¡¼¥Ð¡¼¤Î
295 Éé²Ù¤òÁý²Ã¤µ¤»¡¢¥Ñ¥Õ¥©¡¼¥Þ¥ó¥¹¤ÎÄã²¼¤ò¤â¤¿¤é¤¹¡£
297 .\"O Many file systems and disks were considered to be fast enough that the
298 .\"O implementation of
300 .\"O was deemed unnecessary.
303 .\"O may not be available on files
305 ¿¤¯¤Î¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤ä¥Ç¥£¥¹¥¯¤Ï
307 ¤Î¼ÂÁõ¤Ï¤·¤Ê¤¯¤Æ¤âºÑ¤à¤¯¤é¤¤¤Ë½½Ê¬¤Ë¹â®¤Ç¤¢¤ë¤È¹Í¤¨¤é¤ì¤Æ¤¤¤ë¡£
308 ¤½¤ì¤Ç¤½¤Î¤è¤¦¤Ê¥Õ¥¡¥¤¥ë¤ä¥Ç¥£¥¹¥¯¤Ë¤Ï
310 ¤ÏÍøÍѤǤ¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£