close() — Close a file
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1 |
both |
Format
#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);
General description
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.
- Parameter
- Description
- socket
- The descriptor of the socket to be closed.
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.
Returned value
If successful, close() returns 0.
- Error Code
- Description
- EAGAIN
- The call did not complete because the specified socket descriptor
is currently being used by another thread in the same process.
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.
- EBADF
- fildes is not a valid open file descriptor, or the socket parameter is not a valid socket descriptor.
- EBUSY
- The file cannot be closed because it is blocked.
- EINTR
- close() was interrupted by a signal. The file may or may not be closed.
- EIO
- Added for XPG4.2: An I/O error occurred while reading from or writing to the file system.
- ENXIO
- fildes does not exist. The minor number for the file is incorrect.
Example
#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);
}
}
Related information
- unistd.h
- accept() — Accept a new connection on a socket
- creat() — Create a new file or rewrite an existing one
- dup() — Duplicate an open file descriptor
- exec functions
- fclose() — Close file
- fcntl() — Control open file descriptors
- fork() — Create a new process
- getsockopt() — Get the options associated with a socket
- open() — Open a file
- pipe() — Create an unnamed pipe
- setsockopt() — Set options associated with a socket
- socket() — Create a socket
- shutdown() — Shut down all or part of a duplex connection
- unlink() — Remove a directory entry