1close(2)                         System Calls                         close(2)
2
3
4

NAME

6       close - close a file descriptor
7

SYNOPSIS

9       #include <unistd.h>
10
11       int close(int fildes);
12
13

DESCRIPTION

15       The  close()  function  deallocates  the  file  descriptor indicated by
16       fildes. To deallocate means to make the file descriptor  available  for
17       return  by subsequent calls to open(2) or other functions that allocate
18       file descriptors. All outstanding record locks owned by the process  on
19       the  file associated with the file descriptor will be removed (that is,
20       unlocked).
21
22
23       If close() is interrupted by a signal that is to  be  caught,  it  will
24       return  −1  with errno set to EINTR and the state of fildes is unspeci‐
25       fied. If an I/O error occurred while reading from  or  writing  to  the
26       file  system  during close(), it returns -1, sets errno to EIO, and the
27       state of fildes is unspecified.
28
29
30       When all file descriptors associated with a pipe or FIFO  special  file
31       are closed, any data remaining in the pipe or FIFO will be discarded.
32
33
34       When all file descriptors associated with an open file description have
35       been closed the open file description will be freed.
36
37
38       If the link count of the file is 0, when all file  descriptors  associ‐
39       ated  with  the file are closed, the space occupied by the file will be
40       freed and the file will no longer be accessible.
41
42
43       If a streams-based (see Intro(2)) fildes  is  closed  and  the  calling
44       process was previously registered to receive a SIGPOLL signal (see sig‐
45       nal(3C)) for events  associated  with  that  stream  (see  I_SETSIG  in
46       streamio(7I)),  the  calling  process  will  be unregistered for events
47       associated with the stream.  The last close() for a stream  causes  the
48       stream  associated  with  fildes  to  be  dismantled. If O_NONBLOCK and
49       O_NDELAY are not set and there have been  no  signals  posted  for  the
50       stream, and if there is data on the module's write queue, close() waits
51       up to 15 seconds (for each module and driver) for any output  to  drain
52       before  dismantling  the  stream.  The time delay can be changed via an
53       I_SETCLTIME ioctl(2) request (see streamio(7I)). If the  O_NONBLOCK  or
54       O_NDELAY flag is set, or if there are any pending signals, close() does
55       not wait for output to drain, and dismantles the stream immediately.
56
57
58       If fildes is associated with one end of a pipe, the last close() causes
59       a  hangup  to  occur on the other end of the pipe.  In addition, if the
60       other end of the pipe has been named  by  fattach(3C),  then  the  last
61       close()  forces  the  named  end  to be detached by fdetach(3C). If the
62       named end has no open file descriptors  associated  with  it  and  gets
63       detached, the stream associated with that end is also dismantled.
64
65
66       If fildes refers to the master side of a pseudo-terminal, a SIGHUP sig‐
67       nal is sent to the session leader, if any, for which the slave side  of
68       the  pseudo-terminal  is  the  controlling  terminal. It is unspecified
69       whether closing the master side  of  the  pseudo-terminal  flushes  all
70       queued input and output.
71
72
73       If  fildes refers to the slave side of a streams-based pseudo-terminal,
74       a zero-length message may be sent to the master.
75
76
77       When there is an  outstanding  cancelable  asynchronous  I/O  operation
78       against  fildes when close() is called, that I/O operation is canceled.
79       An I/O operation that is not canceled completes as if the close() oper‐
80       ation  had  not yet occurred. All operations that are not canceled will
81       complete as if the close() blocked until the operations completed.
82
83
84       If a shared memory object or a memory mapped file remains referenced at
85       the last close (that is, a process has it mapped), then the entire con‐
86       tents of the memory object will persist until the memory object becomes
87       unreferenced.  If this is the last close of a shared memory object or a
88       memory mapped file and the close results in the memory object  becoming
89       unreferenced,  and the memory object has been unlinked, then the memory
90       object will be removed.
91
92
93       If fildes  refers  to  a  socket,  close()  causes  the  socket  to  be
94       destroyed.   If the socket is connection-mode, and the SO_LINGER option
95       is set for the socket with non-zero linger time,  and  the  socket  has
96       untransmitted  data,  then  close()  will  block  for up to the current
97       linger interval until all data is transmitted.
98

RETURN VALUES

100       Upon successful completion, 0 is returned. Otherwise,  −1  is  returned
101       and errno is set to indicate the error.
102

ERRORS

104       The close() function will fail if:
105
106       EBADF      The fildes argument is not a valid file descriptor.
107
108
109       EINTR      The close() function was interrupted by a signal.
110
111
112       ENOLINK    The  fildes  argument is on a remote machine and the link to
113                  that machine is no longer active.
114
115
116       ENOSPC     There was no free space remaining on the  device  containing
117                  the file.
118
119
120
121       The close() function may fail if:
122
123       EIO    An  I/O error occurred while reading from or writing to the file
124              system.
125
126

EXAMPLES

128       Example 1 Reassign a file descriptor.
129
130
131       The following example closes the file descriptor associated with  stan‐
132       dard  output  for  the current process, re-assigns standard output to a
133       new file descriptor, and closes the original file descriptor  to  clean
134       up.  This  example  assumes  that  the  file descriptor 0, which is the
135       descriptor for standard input, is not closed.
136
137
138         #include <unistd.h>
139         ...
140         int pfd;
141         ...
142         close(1);
143         dup(pfd);
144         close(pfd);
145         ...
146
147
148
149       Incidentally, this is exactly what could be achieved using:
150
151
152         dup2(pfd, 1);
153         close(pfd);
154
155
156       Example 2 Close a file descriptor.
157
158
159       In the following example, close() is used to close  a  file  descriptor
160       after an unsuccessful attempt is made to associate that file descriptor
161       with a stream.
162
163
164         #include <stdio.h>
165         #include <unistd.h>
166         #include <stdlib.h>
167
168         #define LOCKFILE "/etc/ptmp"
169         ...
170         int pfd;
171         FILE *fpfd;
172         ...
173         if ((fpfd = fdopen (pfd, "w")) == NULL) {
174                 close(pfd);
175                 unlink(LOCKFILE);
176                 exit(1);
177         }
178         ...
179
180

USAGE

182       An application that used the stdio function fopen(3C) to  open  a  file
183       should use the corresponding fclose(3C) function rather than close().
184

ATTRIBUTES

186       See attributes(5) for descriptions of the following attributes:
187
188
189
190
191       ┌─────────────────────────────┬─────────────────────────────┐
192       │      ATTRIBUTE TYPE         │      ATTRIBUTE VALUE        │
193       ├─────────────────────────────┼─────────────────────────────┤
194       │Interface Stability          │Standard                     │
195       ├─────────────────────────────┼─────────────────────────────┤
196       │MT-Level                     │Async-Signal-Safe            │
197       └─────────────────────────────┴─────────────────────────────┘
198

SEE ALSO

200       Intro(2),   creat(2),  dup(2),  exec(2),  fcntl(2),  ioctl(2),  open(2)
201       pipe(2), fattach(3C), fclose(3C), fdetach(3C),  fopen(3C),  signal(3C),
202       signal.h(3HEAD), attributes(5), standards(5), streamio(7I)
203
204
205
206SunOS 5.11                        18 Oct 2005                         close(2)
Impressum