1CLOSE(3P)                  POSIX Programmer's Manual                 CLOSE(3P)


6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.


12       close - close a file descriptor


15       #include <unistd.h>
17       int close(int fildes);


21       The close() function shall deallocate the file descriptor indicated  by
22       fildes.  To  deallocate means to make the file descriptor available for
23       return by subsequent calls to open() or other functions  that  allocate
24       file  descriptors. All outstanding record locks owned by the process on
25       the file associated with the file descriptor shall be removed (that is,
26       unlocked).
28       If  close()  is  interrupted by a signal that is to be caught, it shall
29       return -1 with errno set to [EINTR] and the state of fildes is unspeci‐
30       fied.  If  an  I/O  error occurred while reading from or writing to the
31       file system during close(), it may return -1 with errno set  to  [EIO];
32       if this error is returned, the state of fildes is unspecified.
34       When  all  file descriptors associated with a pipe or FIFO special file
35       are closed, any data remaining in the pipe or FIFO shall be discarded.
37       When all file descriptors associated with an open file description have
38       been closed, the open file description shall be freed.
40       If  the  link count of the file is 0, when all file descriptors associ‐
41       ated with the file are closed, the space occupied by the file shall  be
42       freed and the file shall no longer be accessible.
44       If  a STREAMS-based fildes is closed and the calling process was previ‐
45       ously registered to receive a SIGPOLL signal for events associated with
46       that STREAM, the calling process shall be unregistered for events asso‐
47       ciated with the STREAM. The last close() for a STREAM shall  cause  the
48       STREAM  associated  with  fildes to be dismantled. If O_NONBLOCK is not
49       set and there have been no signals posted for the STREAM, and if  there
50       is data on the module's write queue, close() shall wait for an unspeci‐
51       fied time (for each module and driver) for any output to  drain  before
52       dismantling  the  STREAM.  The  time delay can be changed via an I_SET‐
53       CLTIME ioctl() request. If the O_NONBLOCK flag is set, or if there  are
54       any  pending  signals,  close() shall not wait for output to drain, and
55       shall dismantle the STREAM immediately.
57       If the implementation supports STREAMS-based pipes, and fildes is asso‐
58       ciated with one end of a pipe, the last close() shall cause a hangup to
59       occur on the other end of the pipe. In addition, if the  other  end  of
60       the pipe has been named by fattach(), then the last close() shall force
61       the named end to be detached by fdetach(). If the named end has no open
62       file descriptors associated with it and gets detached, the STREAM asso‐
63       ciated with that end shall also be dismantled.
65       If fildes refers to the master side of a pseudo-terminal, and  this  is
66       the  last  close,  a  SIGHUP  signal  shall  be sent to the controlling
67       process, if any, for which the slave side of the pseudo-terminal is the
68       controlling terminal. It is unspecified whether closing the master side
69       of the pseudo-terminal flushes all queued input and output.
71       If fildes refers to the slave side of a STREAMS-based  pseudo-terminal,
72       a zero-length message may be sent to the master.
74       When  there  is  an  outstanding  cancelable asynchronous I/O operation
75       against fildes when close() is called, that I/O operation may  be  can‐
76       celed.  An  I/O  operation  that  is  not  canceled completes as if the
77       close() operation had not yet occurred.  All operations  that  are  not
78       canceled  shall complete as if the close() blocked until the operations
79       completed. The close() operation itself need not  block  awaiting  such
80       I/O  completion.   Whether any I/O operation is canceled, and which I/O
81       operation may be canceled upon close(), is implementation-defined.
83       If a shared memory object or a memory mapped file remains referenced at
84       the last close (that is, a process has it mapped), then the entire con‐
85       tents of the memory  object  shall  persist  until  the  memory  object
86       becomes  unreferenced.  If  this  is  the last close of a shared memory
87       object or a memory mapped file and the  close  results  in  the  memory
88       object  becoming unreferenced, and the memory object has been unlinked,
89       then the memory object shall be removed.
91       If fildes refers to a socket, close() shall  cause  the  socket  to  be
92       destroyed.  If  the  socket  is  in  connection-mode, and the SO_LINGER
93       option is set for the socket with non-zero linger time, and the  socket
94       has  untransmitted data, then close() shall block for up to the current
95       linger interval until all data is transmitted.


98       Upon successful completion, 0 shall be returned; otherwise, -1 shall be
99       returned and errno set to indicate the error.


102       The close() function shall fail if:
104       EBADF  The fildes argument is not a valid file descriptor.
106       EINTR  The close() function was interrupted by a signal.
109       The close() function may fail if:
111       EIO    An  I/O error occurred while reading from or writing to the file
112              system.
115       The following sections are informative.


118   Reassigning a File Descriptor
119       The following example closes the file descriptor associated with  stan‐
120       dard  output  for  the current process, re-assigns standard output to a
121       new file descriptor, and closes the original file descriptor  to  clean
122       up.  This  example  assumes  that  the  file descriptor 0 (which is the
123       descriptor for standard input) is not closed.
126              #include <unistd.h>
127              ...
128              int pfd;
129              ...
130              close(1);
131              dup(pfd);
132              close(pfd);
133              ...
135       Incidentally, this is exactly what could be achieved using:
138              dup2(pfd, 1);
139              close(pfd);
141   Closing a File Descriptor
142       In the following example, close() is used to close  a  file  descriptor
143       after an unsuccessful attempt is made to associate that file descriptor
144       with a stream.
147              #include <stdio.h>
148              #include <unistd.h>
149              #include <stdlib.h>
152              #define LOCKFILE "/etc/ptmp"
153              ...
154              int pfd;
155              FILE *fpfd;
156              ...
157              if ((fpfd = fdopen (pfd, "w")) == NULL) {
158                  close(pfd);
159                  unlink(LOCKFILE);
160                  exit(1);
161              }
162              ...


165       An application that had used the stdio routine fopen() to open  a  file
166       should  use  the  corresponding  fclose()  routine rather than close().
167       Once a file is closed, the file descriptor no longer exists, since  the
168       integer corresponding to it no longer refers to a file.


171       The use of interruptible device close routines should be discouraged to
172       avoid problems with the implicit closes of file descriptors by exec and
173       exit(). This volume of IEEE Std 1003.1-2001 only intends to permit such
174       behavior by specifying the [EINTR] error condition.


177       None.


180       STREAMS, fattach(), fclose(), fdetach(), fopen(), ioctl(), open(),  the
181       Base Definitions volume of IEEE Std 1003.1-2001, <unistd.h>
184       Portions  of  this text are reprinted and reproduced in electronic form
185       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
186       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
187       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
188       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
189       event of any discrepancy between this version and the original IEEE and
190       The  Open Group Standard, the original IEEE and The Open Group Standard
191       is the referee document. The original Standard can be  obtained  online
192       at http://www.opengroup.org/unix/online.html .
196IEEE/The Open Group                  2003                            CLOSE(3P)