.\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson. .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith .\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt .\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer : .\" corrected description of effect on locks (thanks to .\" Tigran Aivazian ). .\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond .\" Modified 2000-07-22 by Nicolás Lichtmaier .\" added note about close(2) not guaranteeing that data is safe on close. .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya .\" all rights reserved. .\" Translated Sat Jun 1 22:22:05 JST 1997 .\" by HANATAKA Shinya .\" Modified Mon Sep 23 20:42:13 JST 2000 .\" by HANATAKA Shinya .\" Updated & Modified Sat Apr 7 03:24:03 JST 2001 .\" by Yuichi SATO .\" Updated & Modified Mon Jan 14 12:41:36 JST 2002 by Yuichi SATO .\" Updated & Modified Wed Dec 29 07:01:14 JST 2004 .\" by Yuichi SATO .\" Updated 2007-10-12, Akihiro MOTOKI, LDP v2.66 .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" .TH CLOSE 2 2013\-12\-30 Linux "Linux Programmer's Manual" .SH 名前 close \- ファイルディスクリプタをクローズする .SH 書式 .nf \fB#include \fP .sp \fBint close(int \fP\fIfd\fP\fB);\fP .fi .SH 説明 \fBclose\fP() は、ファイルディスクリプタをクローズする。 そのディスクリプタは、どのファイルも参照していない状態になり、 再利用が可能になる。 そのファイルディスクリプタに関連づけられたファイルに かけられたレコードロック (\fBfcntl\fP(2) 参照) のうち、そのプロセスが保有しているものは、 (そのファイルディスクリプタがロック取得に利用されたか どうかによらず) すべて削除される。 .PP \fIfd\fP が、対応するオープンファイル記述 (open file description) (\fBopen\fP(2) 参照) を参照する最後のファイルディスクリプタだった場合、 オープンファイル記述に関連するリソースが解放される。 そのディスクリプタが、 \fBunlink\fP を使用して削除 (remove) されたファイルに対する最後の参照だった場合には、 そのファイルは削除 (delete) される。 .SH 返り値 \fBclose\fP() は成功した場合は 0 を返す。 エラーが発生した場合は \-1 を返して、 \fIerrno\fP を適切に設定する。 .SH エラー .TP \fBEBADF\fP \fIfd\fP が有効なオープンされたディスクリプタでない。 .TP \fBEINTR\fP \fBclose\fP() コールがシグナルにより中断 (interrupt) された。 \fBsignal\fP(7) 参照。 .TP \fBEIO\fP I/O エラーが発生した。 .SH 準拠 .\" SVr4 documents an additional ENOLINK error condition. SVr4, 4.3BSD, POSIX.1\-2001. .SH 注意 \fBclose\fP() の返り値のチェックはよく省略されるが、 これは深刻なプログラミングエラーである。 前の \fBwrite\fP(2) 処理に関するエラーが最後の \fBclose\fP() のときになって初めて通知される場合がありうる。 ファイルクローズの際に返り値をチェックしないと、 気付かないうちにデータを失ってしまうかもしれない。 これは特に NFS やディスククォータを使用した場合に見られる。返り値は状態確認用にのみ利用すべき点に注意すること。特に、\fBEINTR\fP 後に \fBclose\fP() を再度行うべきではない。なぜなら、これにより、別のスレッドが再利用したディスクリプタをクローズしてしまう可能性があるからだ。 .PP クローズに成功しても、データがディスクに保存されたかどうかは 保証されない (カーネルが書きこみを遅延させることがあるためである)。 ストリームがクローズされるときにバッファをフラッシュするかどうかは、 ファイルシステムによって異なる。 データが物理的に保存されることを保証する必要がある場合には、 \fBfsync\fP(2) を使用すること (\fBfsync\fP(3) を行った時点で、データの保存はディスクのハードウェアに依存する 問題となる)。 .PP .\" Date: Tue, 4 Sep 2007 13:57:35 +0200 .\" From: Fredrik Noring .\" One such race involves signals and ERESTARTSYS. If a file descriptor .\" in use by a system call is closed and then reused by e.g. an .\" independent open() in some unrelated thread, before the original system .\" call has restarted after ERESTARTSYS, the original system call will .\" later restart with the reused file descriptor. This is most likely a .\" serious programming error. 同じプロセス内の他のスレッドのシステムコールが使用している可能性がある間に、 ファイルディスクリプタをクローズするのは、おそらく賢明ではないだろう。 ファイルディスクリプタは再利用されるかもしれないので、 あいまいな競合条件となることがあり、意図しない副作用の原因となりうる。 .SH 関連項目 \fBfcntl\fP(2), \fBfsync\fP(2), \fBopen\fP(2), \fBshutdown\fP(2), \fBunlink\fP(2), \fBfclose\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。