操作系统实验5文件系统:Linux文件管理

上传人:桔**** 文档编号:568337299 上传时间:2024-07-24 格式:PDF 页数:5 大小:141.19KB
返回 下载 相关 举报
操作系统实验5文件系统:Linux文件管理_第1页
第1页 / 共5页
操作系统实验5文件系统:Linux文件管理_第2页
第2页 / 共5页
操作系统实验5文件系统:Linux文件管理_第3页
第3页 / 共5页
操作系统实验5文件系统:Linux文件管理_第4页
第4页 / 共5页
操作系统实验5文件系统:Linux文件管理_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《操作系统实验5文件系统:Linux文件管理》由会员分享,可在线阅读,更多相关《操作系统实验5文件系统:Linux文件管理(5页珍藏版)》请在金锄头文库上搜索。

1、-实验实验 5 5 文件系统文件系统 :Linu*Linu*文件管理文件管理1 1实验目的实验目的1掌握 Linu*提供的文件系统调用的使用法;2熟悉文件和目录操作的系统调用用户接口;3了解操作系统文件系统的工作原理和工作式。2 2实验容实验容1 利用 Linu*有关系统调用函数编写一个文件工具filetools,要求具有以下功能:*0. 退出1. 创立新文件2. 写文件3. 读文件4. 复制文件5. 修改文件权限6. 查看文件权限7. 创立子目录8. 删除子目录9. 改变当前目录到指定目录10. 操作*代码:代码:*include*include*include*include*includ

2、e*include*include*include*include*include*include*include*include*include*include*includevoid menu(void);void menu(void);void openfile(void);void openfile(void);voidvoidwritefile(void);writefile(void);void readfile(void);void readfile(void);void copyfile(void);void copyfile(void);void chmd(void);voi

3、d chmd(void);void ckq*(void);void ckq*(void);void cjml(void);void cjml(void);void scml(void);void scml(void);void ggml(void);void ggml(void);void ylj(void);void ylj(void);int main()int main() int choose;int choose;int suliangjin=1;int suliangjin=1;.z.-menu();menu();scanf(%d,&choose);scanf(%d,&choose

4、);while(choose!=0)while(choose!=0) switch(choose)switch(choose) case 1:openfile();break;case 1:openfile();break;case 2:writefile();break;case 2:writefile();break;case 3:readfile();break;case 3:readfile();break;case 4:copyfile();break;case 4:copyfile();break;case 5:chmd();break;case 5:chmd();break;ca

5、se 6:ckq*();break;case 6:ckq*();break;case 7:cjml();break;case 7:cjml();break;case 8:scml();break;case 8:scml();break;case 9:ggml();break;case 9:ggml();break;case 10:ylj();break;case 10:ylj();break; menu();menu();scanf(%d,&choose);scanf(%d,&choose); return 0;return 0; void menu(void)void menu(void)

6、printf(printf(文件系统文件系统n);n);printf(1.printf(1.创立新文件创立新文件n);n);printf(2.printf(2.写文件写文件n);n);printf(3.printf(3.读文件读文件n);n);printf(4.printf(4.复制文件复制文件n);n);printf(5.printf(5.修改文件权限修改文件权限n);n);printf(6.printf(6.查看文件权限查看文件权限n);n);printf(7.printf(7.创立子目录创立子目录n);n);printf(8.printf(8.删除子目录删除子目录n);n);printf

7、(9.printf(9.改变目前目录到指定目录改变目前目录到指定目录n);n);printf(10.printf(10.操作操作n);n);printf(0.printf(0.退出退出n);n);printf(printf(请输入您的选择请输入您的选择.n);.n); void openfile(void)void openfile(void) int fd;int fd;if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)perr

8、or(open);perror(open);elseelse.z.-printf(open file:hileo.cprintf(open file:hileo.c%dn,fd);%dn,fd);if(close(fd)0)if(close(fd)0)perror(close);perror(close);elseelseprintf(Close );printf(Close ); voidvoidwritefile(void)writefile(void) int fd,size,len;int fd,size,len;char *buf=Hello!Im writing to this f

9、ile!;char *buf=Hello!Im writing to this file!;len=strlen(buf);len=strlen(buf);if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)perror(open);perror(open);elseelseprintf(open file:hileo.cprintf(open file:hileo.c%dn,fd);%dn,fd);if(size=write(fd

10、,buf,len)0)if(size=write(fd,buf,len)0)perror(write);perror(write);elseelseprintf(Write:%sn,buf);printf(Write:%sn,buf);if(close(fd)0)if(close(fd)0)perror(close);perror(close);elseelseprintf(Close hello.cprintf(Close );n); void readfile(void)void readfile(void) int fd,size;int fd,size;char b10;char b1

11、0;if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)if(fd=open(/tmp/hello.c,O_CREAT|O_TRUNC|O_RDWR,0666)0)perror(open);perror(open);elseelseprintf(open file:hileo.cprintf(open file:hileo.c%dn,fd);%dn,fd);lseek(fd,0,SEEK_SET);lseek(fd,0,SEEK_SET);if(size=read(fd,b,10)0)if(size=read(fd,b,10)0)perr

12、or(read);perror(read);elseelseprintf(read from file:%sn,b);printf(read from file:%sn,b);if(close(fd)0)if(close(fd)0)perror(close);perror(close);elseelseprintf(Close );printf(Close ); void copyfile(void)void copyfile(void) .z.-if(fork()=0)if(fork()=0)e*eclp(/bin/cp,cp,/tmp/hello.c,/tmp/he.c,NULL);e*e

13、clp(/bin/cp,cp,/tmp/hello.c,/tmp/he.c,NULL);elseelsewait(0);wait(0);printf(printf(将将 hello.chello.c 复制复制 he.c);he.c); void chmd(void)void chmd(void) int a;int a;printf(1.printf(1.文件主可读可写可执行文件主可读可写可执行n);n);printf(2.printf(2.文件主可读文件主可读n);n);printf(3.printf(3.文件主可写文件主可写n);n);printf(4.printf(4.文件主可执行文件主

14、可执行n);n);printf(printf(请输入您的选项请输入您的选项n);n);scanf(%d,&a);scanf(%d,&a);switch(a)switch(a) case 1:chmod(/tmp/hello.c,S_IRW*U);printf(ok!n);break;case 1:chmod(/tmp/hello.c,S_IRW*U);printf(ok!n);break;case 2:chmod(/tmp/hello.c,S_IRUSR);printf(ok!n);break;case 2:chmod(/tmp/hello.c,S_IRUSR);printf(ok!n);br

15、eak;case 3:chmod(/tmp/hello.c,S_IWUSR);printf(ok!n);break;case 3:chmod(/tmp/hello.c,S_IWUSR);printf(ok!n);break;case 4:chmod(/tmp/hello.c,S_I*USR);printf(ok!n);break;case 4:chmod(/tmp/hello.c,S_I*USR);printf(ok!n);break;default:printf(default:printf(您选择有误您选择有误n);n); void ckq*(void)void ckq*(void) ch

16、ar *path=/bin/ls;char *path=/bin/ls;char *argv4=ls,-l,NULL;char *argv4=ls,-l,NULL;if(fork()=0)if(fork()=0)e*ecv(path,argv);e*ecv(path,argv);elseelsewait(0);wait(0); void cjml(void)void cjml(void) if(mkdir(/tmp/a,S_IRW*U)0)if(mkdir(/tmp/a,S_IRW*U)0)perror(Mkdir);perror(Mkdir);elseelseprintf(printf(创立

17、成功创立成功n);n); void scml(void)void scml(void) if(rmdir(/tmp/a)0)if(rmdir(/tmp/a)0)perror(Rmdir);perror(Rmdir);.z.-elseelseprintf(printf(删除成功删除成功n);n); void ggml(void)void ggml(void) if(chdir(/tmp/bc)0)if(chdir(/tmp/bc)0)perror(chdir);perror(chdir);elseelseprintf(printf(更改目录成功更改目录成功n);n); void ylj(void

18、)void ylj(void) if(link(hello.c,h.c)0)if(link(hello.c,h.c)0)perror(Link);perror(Link);elseelseprintf(printf(建立硬连接建立硬连接n);n); 翻开文件:翻开文件:写文件:写文件:读文件:读文件:复制文件:复制文件:修改权限修改权限: :查看权限:查看权限:创立目录:创立目录:删除目录:删除目录:更改目录:更改目录:硬连接:硬连接:问题解决案:问题解决案:1. 1.在复制文件时用在复制文件时用 e*eclpe*eclp 函数,函数, 虽然有复制成功但是他就跳出整个程序,虽然有复制成功但是他

19、就跳出整个程序, 这就是一个缺陷。这就是一个缺陷。后来我在后来我在 e*eclpe*eclp 前面加前面加 if(fork)if(fork)=0)=0)就可以咯就可以咯2. 2.硬连接不成功,老是抛出错误信息,后来发现原来是因为我的代码出错,还有我之前已硬连接不成功,老是抛出错误信息,后来发现原来是因为我的代码出错,还有我之前已经更改了目录所以就会经常报错经更改了目录所以就会经常报错3. 3.到现在还不知道为什么我读出来的是乱码情解决到现在还不知道为什么我读出来的是乱码情解决实验心得体会实验心得体会:通过这次实验掌握 Linu*提供的文件系统调用的使用法;熟悉文件和目录操作的调用用户接口,了解操作系统文件系统的工作原理和工作式。.z.

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号