首先,我们得先理解linux下的两种文件链接方式
1、 硬链接
指通过索引节点来进行链接。在linux文件系统中,保存在磁盘分区中的文件,不管是什么类型,都会给它分配一个编号,我们称之为
inode号。同一个inode号可以有多个文件名,即可以拥有多个有效路径名。其用途就是用户可以创建重要文件的硬连接来防止误删。因
为只有当一个文件的硬链接数为0时,才会真正删除该文件的inode号及其对应的数据块(data block)。
2、 软链接
软链接也叫符号链接,类似于windows中的快捷方式,它是一种特殊的文本文件,里面保存着另一个文件的位置信息。
在命令行终端里,如何用shell命令创建硬连接和软链接呢
1、 shell创建文件
$touch file1
2、 shell创建硬链接文件
$ln file1 file2 #给文件file1创建一个硬连接文件file2
3、 shell创建 软链接文件
$ln -s file1 file3 #给文件file1创建一个符号链接文件file3
此时用$ls -li(-i参数显示文件的inode信息)可以看到file2和file1拥有相同的inode号,而file3则有自己的inode号
实验小结:
1)删除符号链接file3,对file1,file3无影响;
2)删除硬连接file2,对file1,file3也无影响;
3)删除原文件file1,对硬连接file2没有影响,而导致符号链接file3失效;
4)同时删除原文件file1,硬连接file2,整个文件会真正被删除。
注:在redhat中发现同时删除了file1,file2之后往file3里面写数据后保存退出再ls,却能还原出file1。
最后我们小结一下标题里提到的unlink和remove
int remove(const char *pathname); // #include <stdio.h>
int unlink(const char *pathname); // #include <unistd.h>
int rmdir(const char *pathname); // #include <unistd.h>
1、 对于文件,remove和unlink是一样的操作,对于目录,remove和rmdir是一样的操作;
2、 对于符号链接文件,remove和unlink不会删除其指向的文件,而只是删除该符号文件本身;
3、 如果所删除的文件只有最后一个硬链接,并且没有进程打开它,那么该文件会被彻底删除,其所占据的数据块空间会被释放。
4、 如果所删除的文件只有最后一个硬链接,但是仍然有进程打开它,那么文件会继续保持到与之关联的最后一个文件描述符被close后才会
被删除。
5、 如果remove/unlink的文件名是一个socket、FIFO,或者其它设备,但有进程在open了这些socket、FIFO或者其它设备,那么这些进
程依然能继续使用拥有该pathname的文件。
注:创建符号链接文件和读取符号链接文件分别用下面系统函数
#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);
ssize_t readlink(const char *path, char *buf, size_t bufsiz); // 将path所代表的符号链接文件本身的信息读取到buf中。
创建硬链接文件用
#include <unistd.h>
int link(const char *oldpath, const char *newpath);
2、flock
表头文件 #include<sys/file.h>
定义函数 int flock(int fd,int operation);
函数说明 flock()会依参数operation所指定的方式对参数fd所指的文件做各种锁定或解除锁定的动作。此函数只能锁定整个文件,无法锁定文件的某一区域。
参数 operation有下列四种情况:
LOCK_SH 建立共享锁定。多个进程可同时对同一个文件作共享锁定。
LOCK_EX 建立互斥锁定。一个文件同时只有一个互斥锁定。
LOCK_UN 解除文件锁定状态。
LOCK_NB 无法建立锁定时,此操作可不被阻断,马上返回进程。通常与LOCK_SH或LOCK_EX 做OR(|)组合。
单一文件无法同时建立共享锁定和互斥锁定,而当使用dup()或fork()时文件描述词不会继承此种锁定。
返回值 返回0表示成功,若有错误则返回-1,错误代码存于errno。
flock只要在打开文件后,需要对文件读写之前flock一下就可以了,用完之后再flock一下,前面加锁,后面解锁。其实确实是这么简单,但是前段时间用的时候发现点问题,问题描述如下:
一个进程去打开文件,输入一个整数,然后上一把写锁(LOCK_EX),再输入一个整数将解锁(LOCK_UN),另一个进程打开同样一个文件,直接向文件中写数据,发现锁不起作用,能正常写入(我此时用的是超级用户)。google了一大圈发现flock不提供锁检查,也就是说在用flock之前需要用户自己去检查一下是否已经上了锁,说明白点就是读写文件之前用一下flock检查一下文件有没有上锁,如果上锁了flock将会阻塞在那里(An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor ),除非用了LOCK_NB。一个完整的用于测试的事例代码如下所示:
//lockfile.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main()
{
int fd,i;
char path[]="/home/taoyong/test.txt";
extern int errno;
fd=open(path,O_WRONLY|O_CREAT);
if(fd!=-1)
{
printf("open file %s ./n",path);
printf("please input a number to lock the file./n");
scanf("%d",&i);
if(flock(fd,LOCK_EX)==0)
{
printf("the file was locked./n");
}
else
{
printf("the file was not locked./n");
}
printf("please input a number to unlock the file./n");
scanf("%d",&i);
if(flock(fd,LOCK_UN)==0)
{
printf("the file was unlocked./n");
}
else
{
printf("the file was not unlocked./n");
}
close(fd);
}
else
{
printf("cannot open file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s",strerror(errno));
}
}
//testprocess.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
int main()
{
int fd,i;
char path[]="/home/taoyong/test.txt";
char s[]="writing.../nwriting....../n";
extern int errno;
fd=open(path,O_WRONLY|O_CREAT|O_APPEND);
if(fd!=-1)
{
printf("open file %s ./n",path);
if(flock(fd,LOCK_EX|LOCK_NB)==0)
{
printf("the file was locked by the process./n");
if(-1!=write(fd,s,sizeof(s)))
{
printf("write %s to the file %s/n",s,path);
}
else
{
printf("cannot write the file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s/n",strerror(errno));
}
}
else
{
printf("the file was locked by other process.Can't write.../n");
printf("errno:%d:",errno);
}
close(fd);
}
else
{
printf("cannot open file %s/n",path);
printf("errno:%d/n",errno);
printf("errMsg:%s",strerror(errno));
}
}
3、fileno(返回文件流所使用的文件描述词)
相关函数 open,fopen
表头文件 #include<stdio.h>
定义函数 int fileno(FILE * stream);
函数说明 fileno()用来取得参数stream指定的文件流所使用的文件描述词。
返回值 返回文件描述词。
范例: #include<stdio.h> |