尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

Linux文件与fd

Linux文件与fd 一个程序#include stdio.h #include string.h int main() { FILE *fd fopen(hello.txt,w); if(fd NULL) { return 1; } const char *msg hello world\n; fwrite(msg, 1, strlen(msg)1, fd); fclose(fd); return 0; }运行程序之后查看一下文件的内容在上面的程序当中我们以写的方式打开了一个文件如果文件没有建立会自动帮你建立而且以写的方式打开的话你多次运行程序的话会将写的内容覆盖如果想不被覆盖的话可以采用追加的方式打开文件这都是C语言的内容我们简单的复习一下接下来才是重点为什么我们在读一个文件的时候需要先打开文件等到程序结束之后我们又要关闭文件原因就是我们的文件是在磁盘上面的当我们要写/读文件的时候打开文件实际上就是将文件加载到内存关闭就是将文件又写回磁盘上这也是我们在window电脑上面写完文件需要点击保存的原因下面我们演示一下不关闭文件就无法写入成功的情况#includestdio.h #includestring.h #includeunistd.h int main() { FILE *fdfopen(hello.txt,w); if(fdNULL) { return 1; } const char *msghello world; fwrite(msg,1,strlen(msg)1,fd); //fclose(fd); _exit(0); //return 0; //不能写这个写这个一样回刷新缓冲区看不见不关闭文件就写写失败的现象 }运行结果这个时候我们发现再查看文件的内容的话就显示为空了在文件系统当中在内存当中存在一个文件缓冲区的地方当我们一直写文件的时候内容首先回被写到缓冲区当中当满足刷新条件的时候就会将缓冲区里面的内容写到磁盘上面去这样能够避免频繁的I/O导致效率的降低将内容打印到显示器在linux中有一切皆文件的说法同样的我们的标准输入(stdin)标准输出(stdout),标准错误stderror也是一个文件对象的指针其中标准输入就是我们的键盘stdout就是显示器stderr暂时不介绍下面展示集中将文件打印到显示器的操作#include stdio.h #include string.h int main() { printf(hello world\n); fprintf(stdout, hello world\n); const char *message hello world\n; fwrite(message, 1, strlen(message), stdout); return 0; }运行结果文件操作的系统接口除了C语言标准的文件接口我们还有系统级别的文件调用接口参数介绍pathname:想要创建的文件名字这里是加上路径一起的flags:表示想要用什么样的方式去创建文件通常有下面的几种方式O_CREAT: 不存在就创建O_TRUNC 清空文件的内容O_APPEND 采用追加的方式mode:表示创建文件的使用权限关于文件的使用权在这里介绍一下上面的红色的方框里面就是文件的权限第一个字符-表示是文件 d表示是目录第二到四表示所属者的权限rwx分别表示读写执行第五到七表示所属组成员的权限rwx分别表示读写执行第八到十表示其他用户的权限rwx分别表示读写执行现在我们来写一个程序#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { int fd open(log.txt,O_CREAT|O_TRUNC|O_WRONLY,0666); const char *buffer hello world\n; write(fd, buffer, strlen(buffer)); close(fd); return 0; }查看一下文件的内容我们解释一下上面打开文件的时候里面的参数O_CREAT|O_TRUNC|O_WRONLY这里表示没有就创建先清空文件的内容用写的方式为什么用“|”链接理解为二进制的方式每一个二进制比特位代表一种方式或运算不就是把他们都结合起来吗后面的0666是设置文件的权限都是以二进制的方式111 111 111就是拥有者所属组其他人的读写执行都设置成为1同样666的二进制是110 110 110,分析方法和上面的一样但是发现文件的权限不是这样的呀这是因为出与安全的考虑最后总是比设置的权限要低一些如果想要设置的话可以像下面的这样#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { //umask(0); //设置umask的值为0 int fd open(log.txt, O_APPEND|O_TRUNC|O_WRONLY, 0666); const char *buffer hello world\n; write(fd, buffer, strlen(buffer)); chmod(log.txt, 0666); //改变一下权限 close(fd); return 0; }文件fd下面看一个程序#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { //umask(0); //设置umask的值为0 int fd open(log.txt O_APPEND|O_TRUNC|O_WRONLY, 0666); printf(the fd of log.txt is %d\n, fd); close(fd); return 0; }运行结果我们发现fd居然是一个数字其实这个数字是一个下标这个下标是一个文件指针数组的下标请看下图在每一个进程的PCB里面都有有一个文件指针变量指向一个文件数组fd是这个文件数组的下标这样就能访问到对应的文件下面再看一个文件#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { //umask(0); //设置umask的值为0 int fd1 open(log1.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd2 open(log2.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd3 open(log3.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd4 open(log4.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd5 open(log5.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); printf(the fd of log1.txt is %d\n, fd1); printf(the fd of log2.txt is %d\n, fd2); printf(the fd of log3.txt is %d\n, fd3); printf(the fd of log4.txt is %d\n, fd4); printf(the fd of log5.txt is %d\n, fd5); close(fd1); close(fd2); close(fd3); close(fd4); close(fd5); return 0; }运行结果文件fd都是连续晚上可以增长的看了上面的文件fds数组可以理解但是为什么是从3开始那012有是什么嘞其实012分别对应的就是stdin,stdout,stderr三个但是这是默认情况下系统回给文件分配最小并且没有使用的fd下面我们关闭一下stderr#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { close(2); //关闭一下stderr int fd1 open(log1.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd2 open(log2.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd3 open(log3.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd4 open(log4.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd5 open(log5.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); printf(the fd of log1.txt is %d\n, fd1); printf(the fd of log2.txt is %d\n, fd2); printf(the fd of log3.txt is %d\n, fd3); printf(the fd of log4.txt is %d\n, fd4); printf(the fd of log5.txt is %d\n, fd5); close(fd1); close(fd2); close(fd3); close(fd4); close(fd5); return 0; }运行结果文件fd便开始从2开始了但是如果我们关闭1的话就会出现不一样的状况#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { close(1); //关闭文件stdout对应的文件 int fd1 open(log1.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd2 open(log2.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd3 open(log3.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd4 open(log4.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); int fd5 open(log5.txt, O_CREAT|O_TRUNC|O_WRONLY, 0666); printf(the fd of log1.txt is %d\n, fd1); printf(the fd of log2.txt is %d\n, fd2); printf(the fd of log3.txt is %d\n, fd3); printf(the fd of log4.txt is %d\n, fd4); printf(the fd of log5.txt is %d\n, fd5); close(fd1); close(fd2); close(fd3); close(fd4); close(fd5); return 0; }但是运行的时候却是什么结果都没有出现但是内容却被打印到log1.txt文件当中去了其实我们语言层的每一个函数再系统都有一个封装printf底层可能就是write函数在写的时候文件默认是stdout对对应的1当我们改变了fd就写到别的文件当中去了这也叫做重定向重定向我们结合上卖弄的例子再来理解一下重定向原本fd1默认是指向stdout的但是我们关闭fd1的文件在以新建的方式打开文件fd1就会分配给新的文件printf底层默认封装的一个函数参数中就有fd1,这个时候当我们调用printf的时候想要打印的内容就打印到了新建的文件当中去int dup2(int oldfd, int newfd);这个函数的作用就是让newfd指向oldfd所指向的文件如果newfd的文件是打开的就先关闭但是oldfd不会关闭也就是说oldfd和newfd指向同一个文件#include stdio.h #include sys/types.h #include sys/stat.h #include fcntl.h #include string.h #include unistd.h int main() { int fd open(log.txt,O_CREAT|O_TRUNC|O_WRONLY,0666); dup2(fd, 1); printf(hello world\n); fflush(stdout); //需要刷新一下缓冲区当指向文件的时候需要等缓冲区满了之后才刷新 close(1); //不管有没有\n,但是指向的是stdout的时候只要有\n就会刷新 close(fd); return 0; }运行查看log.txt文件用户级缓冲区在上面我们提到关于文件系统调用当我们在写文件的时候会有一个系统级别的缓冲区同样的我们在使用C语言的文件操作的时候也存在一个用户级别的缓冲区当我们调用C语言的文件操作的时候比如在写文件的时候先是需要将内容写到用户级别的缓冲区当中然后等到刷新刷新到系统级别的缓冲区再刷新到磁盘上面去#include iostream #include cstdio #include unistd.h using namespace std; int main() { FILE *pf fopen(log.txt,w); fprintf(pf,hello world); //写入用户级别的缓冲区 int fd fileno(pf); fsync(fd); //刷新一下系统级别的缓冲区 _exit(0); //不能用return 0或exit,会刷新用户还有系统级别 //的缓冲区 }运行我们查看log.txt文件里面确是发现什么也没有这是因为当我们调用C语言的文件函数之后刷新的是系统的缓冲区但是内容还在用户级别的缓冲区查看一下文件内容在上面的程序当中我们先刷新了用户缓冲区再刷新文件缓冲区这个时候内容才能够写到文件当中去下面我们再看一个程序#includeiostream 2 #includecstdio 3 #includeunistd.h 4 #includecstring 5 using namespace std; 6 7 8 int main() 9 { 10 printf(hello printf\n); 11 fprintf(stdout,hello fprintf\n); 12 const char *messagehello fwrite\n; 13 fwrite(message,1,strlen(message),stdout); 14 15 const char *whello write\n; 16 write(1,w,strlen(w)); 17 fork(); 18 19 return 0; 20 }直接运行当我们直接运行的时候由于是输入到显示器上的遇到换行就刷新但是当我们重新定向到文件当中去的时候就会出现不一样的效果我们发现调用C语言的打印了两次系统调用的打印了一次这是因为在fork之前调用C语言的时候内容都在用户缓冲区当中当fork之后出现父子进程父子进程分别都有用户缓冲区内容相同结束的时候各自刷新系统调用的是系统自己有的和父子进程没有关系于是调用系统的只是调用了一份
返回列表