《《操作系统原理》实验8-- Linux文件操作实践.docx》由会员分享,可在线阅读,更多相关《《操作系统原理》实验8-- Linux文件操作实践.docx(10页珍藏版)》请在第壹文秘上搜索。
1、操作系统原理实验报告实验序号:8实验项目:Linux文件操作实践学号姓名专业、班级实验地点指导教师实验时间一、实验目的及要求通过本实验的学习,掌握Linux文件系统操作的基本方法。二、实验设备(环境)及要求VMware虚拟机软件,Ubuntu系统三、实验内容与步骤1、根据教材178-181页实践LinUX文件系统操作A.创建文件joejo-virtual-Rachtne:*/ceprogram$ vim create.cjojo-virtualmachte:cprogram5 gcc -o create create.c jojovirtual-Rachtnez-c-progrmS Isl.h
2、 3.h2.h close createclose.c create.c ex-wrtte.c ftle-work.cexwrtte ftle.txt nyftlejon9jon-virtual-machine:-cprograS /createjon0jon-virtualRachtne:-/c_progran$ Isl.h 3.h close.c create.c exwrtte.c ftlework.cyftle2 openopen.c shell2.shshell3.sh shell4.shshells.sh thread2.cjojo-virtual-nachte:*/ceprogr
3、an$ nyftlenyftle2 openopen.cShell2.shshell3.sh shell4.shshells.sh testftle.txttt main() (creat(,S_IRUSR|S_IXOTH);de#ir/include(利用creat创建testflle.txt)B.删除文件jon0jonvirtualwchtre:-/c.progrRS vtn unlink.c Jon9jon-vtrtualfwchtnc:*/c_prograR$ gcc o unlink UnItnkc jonjocvtrtulfMcht.rer-c-progrnS Is1 .h3.hc
4、lose.ccreate.cex-wrtte.cftle-work.c2 .hclosecreateexwrtteftle.txtnyftleJoMJOnvirtual.itmai()(unlink();)(利用unlink删除testfile.txt)C.关闭文件D.打开文件jo0jon-virtual-machine:/c_program$gcc-ocloseclose.cjo0jo-virtual-machine:/c_program$Is1 .hclosecreate.cfile.txtshe113.shthread2.c2 .hclose.cex_wrttenyftleSheII4.
5、sh3 .hcreateex_wrtte.cshe112.shshells,shjo0jon-virtual-machine:/c_program$./closejo0jon-virtual-machine:/c_program$IS1 .hclosecreate.cfile.txtshefl2.shshells.sh2 .hclose,cex_wrttenyftlesheaL13.shthread2.c3 .hcreateex-wrtte.cInyfIIe2she114.shjo0jon-virtual-machine:*/c_program$1 Sinclude2 #include.ys八
6、ypes.h3 #include,,./tat.114 ffinclude56 itmain()7 (8 ttfd;fd=open(,O_CREAT,S_IRUSRS_IXOTH);10close(fd);U)(利用OPen打开文件然后再用close关闭)E.读文件F.写文件1 /Include2 #tnclude3 #include4 #include:to.h5 #deftneLENGTHlu()6main()7(8ttfd,le;9charstrLENGTH;10fd=ope(,O_CREATI0_RDWR.S_IRUSRIWUSR);11tf(fd)1213write(fd,fstrl
7、en();14close(fd);15)1617fd=open(,0_RDWR);18Ien=read(fd,str,LENGTH);19strlen=7;20prntf(,str);21close(fd);22)(综合运用)2、利用以上操作编写函数,实现文件拷贝、移动操作,不可直接使用操作系统命令与函数。Ijonzubutu:/CProgram$vimcopy.cIjonzubuntu:/CProgram$gcc-ocopycopy.cjonzubuntu:/CProgram$Isclientcopy.cexa3.c-ftle.txtshelll.shshells.shclient.cexa
8、n3exa5servershell2.shuserdatacopyexam3.cexam5.cserver.cSheIl3.shHonzQubuntu:*/CProgram$./copycpfile.,txtftle2.txtfilecopysucceedjonzubuntu:/CProgram$ISclientcopy.cexam3.c-file2.txtserver.cShelI3.shclient.cean3ean5file.txtshelll.shshell5.shcopyexam3.cexan5.cservershell2.shuserdataCopy函数用来复制文件Qtnclude
9、#includesstdt.L-/include一rI.h/Include,;UntStd.heinclude#includeffdeftneBUFFSIZE096mai(rtargc,Cha-COn*argv)intfdsrc,fdtgt,siz;ttop三;charbufBUFFSIZE;If(!strcmp(argv,)op=;if(!strcmp(argv,)op=;if(argc!=op=)printf(,argv);return;)fdsrc=ope(argv,O_RDONLY);if(fdsrc)printf(i,3n);return;)fdtgtscreat(argv,S_IR
10、WXU);if(fdtgt)prtntf();return;While(StZ=read(fdsrc,buf,BUFFSIZE)if(write(fdtgt,buf,siz)!=stz)prttf();return;if(siz)prtntf();return;)if(op=)prttf();else(close(fdsrc);If(unltk(argv)pritf();return)elseprintf();)止畴内核自动关闭文件:return程序源代码如图Jonzgubuntu:CProgranSvimmove.cjonzubuntu:-/CProgranSgcc-onovemove.cj
11、onzgubutu:-/CProgramSISclientcopy.cexan3.c-ftle2.txtnove.cshelll.shshells.shclient.cexan3exan5ftle.txtserverSheVl2.shuserdataxshell3.shuser37user41user46copyexa3.cean5.cmovejonz0ubuntu:/CProgram$IS./userdata/userluserl4userl9user23user28server.user32user5userluserl5user2user24ser29user33user38user42
12、user47USe6userlluser16user2user25user3user34ser39user43ser48user7userl2user17user21user26user3user35user4user44user49user8userl3userl8user22user27user31user36user46user45usersuser9jonzeubuntu:CProgra11S.noveftle.txt./userdata/IMVftle.txtt。./userdata/FatlUre移动失败。仍存在问题四、分析与讨论1.inux文件系统管理有何特点?-将所有东西都变成文件来管理,包括设备等。同时送迎文件本身来识别,而不是通过文件的后缀名等来识别。五、教师评语成绩签名:日期:W1 .文件复制函数源代码#include#include#include#include#include#include#defineBUFFSIZE4096intmain(intargc,charconst*argv)intfdsrc,fdtgt,siz;intop