用man命令查看这两个命令:
1. unlink
unlink有两个,1类和2类,函数中使用的是2类,因此使用以下命令查看:
$ man 2 unlink
NAME
unlink, unlinkat - delete a name and possibly the file it refers to
SYNOPSIS
#include <unistd.h>
int unlink(const char *pathname);
#include <fcntl.h> /* Definition of AT_* constants */
#include <unistd.h>
int unlinkat(int dirfd, const char *pathname, int flags);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
unlinkat():
Since glibc 2.10:
_XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
DESCRIPTION
unlink() deletes a name from the filesystem. If that name was the last link to a file and
no processes have the file open, the file is deleted and the space it was using is made
available for reuse.
If the name was the last link to a file but any processes still have the file open, the file
will remain in existence until the last file descriptor referring to it is closed.
If the name referred to a symbolic link, the link is removed.
If the name referred to a socket, FIFO, or device, the name for it is removed but processes
which have the object open may continue to use it.
2. remove
NAME
remove - remove a file or directory
SYNOPSIS
#include <stdio.h>
int remove(const char *pathname);
DESCRIPTION
remove() deletes a name from the filesystem. It calls unlink(2) for files, and rmdir(2) for
directories.
If the removed name was the last link to a file and no processes have the file open, the
file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file, but any processes still have the file open, the
file will remain in existence until the last file descriptor referring to it is closed.
If the name referred to a symbolic link, the link is removed.
If the name referred to a socket, FIFO, or device, the name is removed, but processes which
have the object open may continue to use it.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
ERRORS
The errors that occur are those for unlink(2) and rmdir(2).
3. 总结
可以看到,当删除文件时,remove调用的就是unlink。
功能:
1. 当文件没有被别的应用打开且它是最后一个指向文件的链接,那么此文件将被删除,它占用的空间将被释放。
2. 当文件虽然是最后一个指向文件的链接,但有其他进程正使用此文件,此文件要到它最后一个文件描述符被关闭,才能被删除。
3. 当文件名指向的是一个符号链接,那么此符号链接被删除。
4. 当文件名指向的是一个 socket / FIFO / device,这个文件名将会删除,但持有此文件对象的进程还是会使用它。
返回值:
成功返回0; 失败返回-1, 并置对应的失败码。