close - close a file descriptor
close - 关闭一个文件描述符
所需头文件
#include <unistd.h>
int close(int fd);
close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).
关闭一个文件描述符,所以它将不再关联任何文件和重用。文件保存的任何关联记录锁都被清除。
If fd is the last file descriptor referring to the underlying open file description (see open(2)), the resources associated with the open file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.
如果fd是打开文件的最后一个文件描述符,打开文件的资源都将被释放,如果描述符是该文件的最后一个,那么文件将被unlink删除掉
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
成功的时候返回0,失败的时候返回-1,errno被设置成合适值
EBADF fd isn't a valid open file descriptor.
fd不是一个可用的打开的文件描述符
EINTR The close() call was interrupted by a signal; see signal(7).
close调用被信号中断了
EIO An I/O error occurred.
发生了I/O错误
Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.
不检查close的返回值是一个常见,然而严重的程序错误,很有可能发生前面的写操作在最后的close中被报告,关闭文件的时候不检查返回值可能导致隐式的数据丢失,这个尤其会被NFS磁盘限额所观察到
A successful close does not guarantee that the data has been successfully saved to disk, as the kernel defers writes. It is not common for a file system to flush the buffers when the stream is closed. If you need to be sure that the data is physically stored use fsync(2). (It will depend on the disk hardware at this point.)
成功的关闭并不能保证数据成功的保存到磁盘,当kernel延迟写的时候,文件系统在stream关闭的时候冲刷缓冲区并不常见,如果需要确保文件被物理性的存储,需要使用fsync(需要取决于此刻的磁盘硬件)
It is probably unwise to close file descriptors while they may be in use by system calls in other threads in the same process. Since a file descriptor may be reused, there are some obscure race conditions that may cause unintended side effects.
当它在被当前进程的其他线程的系统调用使用时,关闭文件描述符或许是不明智的,因为文件描述符可能会被重用,有一些模糊的竞争关系可能会导致不可预测的副作用
close - 关闭一个文件描述符
所需头文件
#include <unistd.h>
int close(int fd);
close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).
关闭一个文件描述符,所以它将不再关联任何文件和重用。文件保存的任何关联记录锁都被清除。
If fd is the last file descriptor referring to the underlying open file description (see open(2)), the resources associated with the open file description are freed; if the descriptor was the last reference to a file which has been removed using unlink(2) the file is deleted.
如果fd是打开文件的最后一个文件描述符,打开文件的资源都将被释放,如果描述符是该文件的最后一个,那么文件将被unlink删除掉
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
成功的时候返回0,失败的时候返回-1,errno被设置成合适值
EBADF fd isn't a valid open file descriptor.
fd不是一个可用的打开的文件描述符
EINTR The close() call was interrupted by a signal; see signal(7).
close调用被信号中断了
EIO An I/O error occurred.
发生了I/O错误
Not checking the return value of close() is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close(). Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and with disk quota.
不检查close的返回值是一个常见,然而严重的程序错误,很有可能发生前面的写操作在最后的close中被报告,关闭文件的时候不检查返回值可能导致隐式的数据丢失,这个尤其会被NFS磁盘限额所观察到
A successful close does not guarantee that the data has been successfully saved to disk, as the kernel defers writes. It is not common for a file system to flush the buffers when the stream is closed. If you need to be sure that the data is physically stored use fsync(2). (It will depend on the disk hardware at this point.)
成功的关闭并不能保证数据成功的保存到磁盘,当kernel延迟写的时候,文件系统在stream关闭的时候冲刷缓冲区并不常见,如果需要确保文件被物理性的存储,需要使用fsync(需要取决于此刻的磁盘硬件)
It is probably unwise to close file descriptors while they may be in use by system calls in other threads in the same process. Since a file descriptor may be reused, there are some obscure race conditions that may cause unintended side effects.
当它在被当前进程的其他线程的系统调用使用时,关闭文件描述符或许是不明智的,因为文件描述符可能会被重用,有一些模糊的竞争关系可能会导致不可预测的副作用