Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
#define _POSIX_SOURCE
#include <unistd.h>
int close(int fildes);
#define _XOPEN_SOURCE_EXTENDED 1
#include <unistd.h>
int close(int socket);
#define _OE_SOCKETS
#include <unistd.h>
int close(int socket);
Closes a file descriptor, fildes. This frees the file descriptor to be returned by future open() calls and other calls that create file descriptors. The fildes argument must represent a hierarchical file system (HFS) file.
When the last open file descriptor for a file is closed, the file itself is closed. If the file's link count is 0 at that time, its space is freed and the file becomes inaccessible.
When the last open file descriptor for a pipe or FIFO file is closed, any data remaining in the pipe or FIFO file is discarded.
close() unlocks (removes) all outstanding record locks that a process has on the associated file.
struct linger {
int l_onoff; /* zero=off, nonzero=on */
int l_linger; /* time is seconds to linger */
};
If the l_onoff switch is nonzero, the system attempts to deliver any unsent messages. If a linger time is specified, the system waits for n seconds before flushing the data and terminating the socket.
For AF_UNIX, when closing sockets that were bound, you should also use unlink() to delete the file created at bind() time.
Special behavior for XPG4.2: If a STREAMS-based fildes is closed and the calling process was previously registered to receive a SIGPOLL signal for events associated with that STREAM, the calling process will be unregistered for events associated with the STREAM. The last close() for a STREAM causes the STREAM associated with fildes to be dismantled. If O_NONBLOCK is not set and there have been no signals posted for the STREAM, and if there is data on the module's write queue, close() waits for an unspecified time (for each module and driver) for any output to drain before dismantling the STREAM. The time delay can be changed using an I_SETCLTIME ioctl() request. If the O_NONBLOCK flag is set, or if there are any pending signals, close() does not wait for output to drain, and dismantles the STREAM immediately.
If fildes refers to the master side of a pseudoterminal, a SIGHUP signal is sent to the process group, if any, for which the slave side of the pseudoterminal is the controlling terminal.
If fildes refers to the slave side of a pseudoterminal, a zero-length message will be sent to the master.
If fildes refers to a socket, close() causes the socket to be destroyed. If the socket is connection-oriented and the SO_LINGER option is set for the socket and the socket has untransmitted data, then close() will block for up to the current linger interval until all data is transmitted.
If successful, close() returns 0.
For example, in a multithreaded environment, close() fails and returns EAGAIN when the following sequence of events occurs (1) thread is blocked in a read() or select() call on a given file or socket descriptor and (2) another thread issues a simultaneous close() call for the same descriptor.
#define _POSIX_SOURCE
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
main() {
int fd;
char out[20]="Test string";
if ((fd = creat("./myfile", S_IRUSR | S_IWUSR)) < 0)
perror("creat error");
else {
if (write(fd, out, strlen(out)+1) == -1)
perror("write() error");
if (fd = 0) perror("write() error");
close(fd);
}
}