closeのヘルプ・マニュアル
日本語 英語
close --help
man close
CLOSE(2) Linux Programmer’s Manual CLOSE(2)
名前
close - ファイルディスクリプタをクローズする
書式
#include
int close(int fd);
説明
close() は、ファイルディスクリプタをクローズする。そのディスクリプタは
、どのファイルも参照していない状態になり、再利用が可能になる。そのフ ァ
イ ルディスクリプタに関連づけられたファイルにかけられたレコード・ロック
(fcntl(2) 参照) のうち、そのプロセスが保有しているものは、 (そのファ イ
ル ディスクリプタがロック取得に利用されたかどうかによらず) すべて削除さ
れる。
fd が、対応するオープンファイル記述 (open file description) (open(2) 参
照) を参照する最後のファイルディスクリプタだった場合、オープンファイル
記述に関連するリソースが解放される。そのディスクリプタが、 unlink を 使
用 して削除 (remove) されたファイルに対する最後の参照だった場合には、そ
のファイルは削除 (delete) される。
返り値
close() は成功した場合は 0 を返す。エラーが発生した場合は -1 を返して、
errno を適切に設定する。
エラー
EBADF fd が有効なオープンされたディスクリプタでない。
EINTR close() コ ー ルがシグナルにより中断 (interrupt) された。 sig-
nal(7) 参照。
EIO I/O エラーが発生した。
準拠
SVr4, 4.3BSD, POSIX.1-2001.
注意
close() の返り値のチェックはよく省略されるが、これは深刻なプログラミ ン
グエラーである。前の write(2) 処理に関するエラーが最後の close() のとき
になって初めて通知される場合がありうる。ファイルクローズの際に返り値 を
チ ェックしないと、気付かないうちにデータを失ってしまうかもしれない。こ
れは特に NFS やディスク・クォータを使用した場合に見られる。
クローズに成功しても、データがディスクに保存されたかどうかは保証され な
い (カーネルが書きこみを遅延させることがあるためである)。ストリームがク
ローズされるときにバッファをフラッシュするかどうかは、ファイルシステ ム
に よって異なる。データが物理的に保存されることを保証する必要がある場合
には、 fsync(2) を使用すること (fsync(3) を行った時点で、データの保存は
ディスクのハードウェアに依存する問題となる)。
同 じプロセス内の他のスレッドのシステムコールが使用している可能性がある
間に、ファイルディスクリプタをクローズするのは、おそらく賢明ではない だ
ろ う。ファイルディスクリプタは再利用されるかもしれないので、あいまいな
競合条件となることがあり、意図しない副作用の原因となりうる。
関連項目
fcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3)
Linux 2007-12-28 CLOSE(2)
CLOSE(2) Linux Programmer’s Manual CLOSE(2)
NAME
close - close a file descriptor
SYNOPSIS
#include
int close(int fd);
DESCRIPTION
close() closes a file descriptor, so that it no longer refers to any
file and may be reused. Any record locks (see fcntl(2)) held on the
file it was associated with, and owned by the process, are removed
(regardless of the file descriptor that was used to obtain the lock).
If fd is the last file descriptor referring to the underlying open file
description (see open(2)), the resources associated with the open file
description are freed; if the descriptor was the last reference to a
file which has been removed using unlink(2) the file is deleted.
RETURN VALUE
close() returns zero on success. On error, -1 is returned, and errno
is set appropriately.
ERRORS
EBADF fd isn’t a valid open file descriptor.
EINTR The close() call was interrupted by a signal; see signal(7).
EIO An I/O error occurred.
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
NOTES
Not checking the return value of close() is a common but nevertheless
serious programming error. It is quite possible that errors on a pre-
vious write(2) operation are first reported at the final close(). Not
checking the return value when closing the file may lead to silent loss
of data. This can especially be observed with NFS and with disk quota.
A successful close does not guarantee that the data has been success-
fully saved to disk, as the kernel defers writes. It is not common for
a file system to flush the buffers when the stream is closed. If you
need to be sure that the data is physically stored use fsync(2). (It
will depend on the disk hardware at this point.)
It is probably unwise to close file descriptors while they may be in
use by system calls in other threads in the same process. Since a file
descriptor may be re-used, there are some obscure race conditions that
may cause unintended side effects.
When dealing with sockets, you have to be sure that there is no recv(2)
still blocking on it on another thread, otherwise it might block for-
ever, since no more messages will be sent via the socket. Be sure to
use shutdown(2) to shut down all parts the connection before closing
the socket.
SEE ALSO
fcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2007-12-28 CLOSE(2)