实验二.docx
《实验二.docx》由会员分享,可在线阅读,更多相关《实验二.docx(13页珍藏版)》请在冰点文库上搜索。
实验二
《操作系统》实验报告
课程名称:
操作系统
姓名:
实验名称:
实验二
学号:
任课教师:
专业:
计算机科学与技术
指导教师:
班级:
实验成绩:
批阅教师签字:
一、
实验目的
(1)文件部分系统调用;
(2)通过系统调用实现文件的顺序读取,文件顺序写入,文件的追加,文件随机存取。
二、实验环境
一台装有Windows操作系统PC机,上装有虚拟机系统VMWare,实验过程通过VMWare系统启Linux系统工作。
三、实验内容与实验过程及分析(写出详细的实验步骤,并分析实验结果)
实验步骤:
实验1:
文件的创建与(追加)写入
#include
#include
#include
main(){
intfd,i,j;
char*my_str="Thisismy1stprogram!
!
\n";
char*myf="myfile",tmpc[100];
//可首先判断命令行参数是否够数
if((fd=open(myf,O_RDWR|O_CREAT|O_APPEND,0644))==-1){
perror(myf);
exit(errno);
}
if(write(fd,my_str,strlen(my_str))!
=strlen(my_str)){
perror(myf);
close(fd);
exit(errno);
}
}
(1)令文件名为app.c,使用vi编辑器,编辑并输入此文件;
(2)调通该程序,实现文件的创建、追加与顺序写入。
(3)编译方法:
cc–oappapp.c
(4)执行办法:
./app
(5)观察文件内容:
catmyfile
实验2:
文件复制
本实验主要理解并解决文件的顺序读写操作。
#defineL512
#include
#include
#include
main(intargc,char**argv)
{
intfdi,fdo,i,j;
charbuf[L];
//可首先判断命令行参数是否够数
if((fdi=open(argv[1],O_RDONLY))==-1){
fprintf(stderr,"Sourcefile%sdosen'texist!
\n",argv[1]);
exit
(1);//exitwithreturncode-1
}
//if((fdo=open(argv[2],O_RDONLY))>0){
//fprintf(stderr,"Destfile%sexist!
\n",argv[2]);
//close(fdi);exit
(2);//exitwithreturncode-2
//}
//close(fdo);
if((fdo=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644))==-1){
fprintf(stderr,"File%scan'topenforwrite!
\n",argv[2]);
close(fdi);exit(3);//exitwithreturncode-3
}
while
(1){
if((i=read(fdi,buf,L))<0){
fprintf(stderr,"File%sreaderror!
\n",argv[1]);
close(fdi);close(fdo);
exit(4);//exitwithreturncode-4
}
if(i==0)break;
if((i!
=write(fdo,buf,i))){
fprintf(stderr,"File%swriteerror!
\n",argv[2]);
close(fdi);close(fdo);
exit(5);//exitwithreturncode-5
}
}
close(fdi);close(fdo);
}
(1)令文件名为copy.c,使用vi编辑器,编辑并输入此文件。
(2)调通该程序,实现文件的创建、追加与顺序写入。
(3)编译方法:
cc–ocopycopy.c
(4)执行办法:
./copyifileofile
(5)观察文件内容
可以使用ls–lifileofile查看两个文件的大小,也可使用cmpifileofile比较两者差别,也可直接使用cat命令查看文件的内容。
实验3:
文件删除
#include
#include
main(intargc,char*argv[]){
if(argc==1){
fprintf(stderr,"Nofiletobedeleted!
\n");
exit
(1);
}
if(unlink(argv[1])==-1){
fprintf(stderr,"File%sdeleteerror!
\n",argv[1]);
exit
(2);
}
elseprintf("File%sdeleteOK!
\n",argv[1]);
exit(0);
}
(1)令文件名为del.c,使用vi编辑器,编辑并输入此文件。
(2)调通该程序,实现文件的创建、追加与顺序写入。
(3)编译方法:
cc–odeldel.c
(4)执行方法:
./delsomefile
(5)观察执行结果:
ls–lsomefile
实验4:
文件的随机访问
在实验1的app.c的基础上修改,使程序具有随机访问功能。
#include
#include
#include
main(){
intfd,i,j;
char*my_str="Thisismy1stprogram!
!
\n";
char*myf="myfile",tmpc[100];
if((fd=open(myf,O_RDWR|O_CREAT|O_APPEND,0644))==-1){
perror(myf);
exit(errno);
}
if(write(fd,my_str,strlen(my_str))!
=strlen(my_str)){
perror(myf);
close(fd);
exit(errno);
}//前面部分与实验1相同
j=-strlen(my_str);
lseek(fd,(long)j,SEEK_CUR);//指针移到刚写入的字符串的开始处
read(fd,tmpc,strlen(my_str));//可能会越界
//tmpc[strlen(my_str)]='\0';
printf("%s\n",tmpc);//可能会越界
lseek(fd,0L,SEEK_END);//指针移到文件结束处,为追加做准备
for(i=1;i<10;i++){//向文件追加信息
for(j=0;j
tmpc[j++]='\n';tmpc[j]='\0';
write(fd,tmpc,strlen(tmpc));
}
close(fd);
}
(1)令文件名为filerw.c,使用vi编辑器,编辑并输入此文件。
(2)调通该程序,实现文件的创建、追加与顺序写入。
(3)编译方法:
cc–ofilerwfilerw.c
(4)执行方法:
./filerw
(5)观察文件内容:
catmyfile
四、实验总结(每项不少于20字)
存在问题:
字母大小写及标点符号输入不正确,易导致代码运行不出来。
解决方法:
需认真仔细,多多练习打代码,勤奋用功`。
收获:
掌握了VMWare虚拟机的基本操作及编译方法。
教师批语