实验4文件操作

上传人:优*** 文档编号:87732501 上传时间:2022-05-10 格式:DOC 页数:15 大小:223.50KB
收藏 版权申诉 举报 下载
实验4文件操作_第1页
第1页 / 共15页
实验4文件操作_第2页
第2页 / 共15页
实验4文件操作_第3页
第3页 / 共15页
资源描述:

《实验4文件操作》由会员分享,可在线阅读,更多相关《实验4文件操作(15页珍藏版)》请在装配图网上搜索。

1、实验4、文件操作 学生姓名: 李亚军 学 号: 6100412196 专业班级: 卓越计科121班 1实验目的 通过编写文件读写及上锁的程序,进一步熟悉 Linux 中文件 I/O 相关的应用开发, 并且熟练掌握 open()、read()、write()、fcntl()等函数的使用。 2实验内容 在 Linux 中 FIFO 是一种进程之间的管道通信机制。Linux 支持完整的 FIFO通信 机制。 本实验内容,通过使用文件操作,仿真 FIFO(先进先出)结构以及生产者-消费者运行模型。 本实验中需要打开两个虚拟终端,分别运行生产者程序(producer)和消费者程序(customer)。此

2、时两个进程同时对同一个文件进行读写操作。因为这个文件是临界资源,所以可以使用文件锁机制来保证两个进程对文件的访问都是原子操作。 先启动生产者进程,它负责创建仿真 FIFO 结构的文件(其实是一个普通文件)并投入生产,就是按照给定的时间间隔,向 FIFO 文件写入自动生成的字符(在程序中用宏定义选择使用数字还是使用英文字符),生产周期以及要生产的资源数通过参数传递给进程(默认生产周期为 1s,要生产的资源数为 10 个字符)。 后启动的消费者进程按照给定的数目进行消费,首先从文件中读取相应数目的字符并在屏幕上显示,然后从文件中删除刚才消费过的数据。为了仿真 FIFO 结构,此时需要使用两次复制来

3、实现文件内容的偏移。每次消费的资源数通过参数传递给进程,默认值为10 个字符。 3实验步骤 (1)实验流程图 本实验的两个程序的流程图如图: 开始开始(producer)(customer)消费资源 创建FIFO结构文件(打印字符)否消费够了吗?生产一个资源是上锁上锁将剩下的数据拷贝到等待临时文件tmp中一秒将“生产”的字符写入到FIFO结构文件用临时文件tmp覆盖原数据文件,这样模拟FIFO结构解锁解锁 整理为word格式生产完了吗? 删除临时文件结束结束图 6.4 节流程图(2)代码 头部文件代码:mylock.hstruct myflock short l_type; /*文件锁类型:

4、F_RDLOCK 读取锁;F_WRLCK 写入锁;F_UNLCK 解锁 */ off_t l_start; /*相对位移量*/ short l_whence; /*相对位移量的起点SEEK_SET;SEEK_CUR; SEEK_END: */ off_t l_len; /* 加锁区域长度 */ pid_t l_pid; /* */ ;/* lock_set */ int lock_set(int fd, int type) struct myflock old_lock, lock; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len

5、= 0; lock.l_type = type; lock.l_pid = -1; /* 判断文件是否可以上锁 */ fcntl(fd, F_GETLK, &lock); if (lock.l_type != F_UNLCK) /* 判断文件不能上锁的原因 */ if (lock.l_type = F_RDLCK) /* 该文件已有读取锁 */ printf(Read lock already set by %dn, lock.l_pid);整理为word格式 else if (lock.l_type = F_WRLCK) /* 该文件已有写入锁 */ printf(Write lock al

6、ready set by %dn, lock.l_pid); /* l_type 可能已被 F_GETLK 修改过 */ lock.l_type = type; /* 根据不同的 type 值进行阻塞式上锁或解锁 */ if (fcntl(fd, F_SETLKW, &lock) 0) printf(Lock failed:type = %dn, lock.l_type); return 1; switch(lock.l_type) case F_RDLCK: printf(Read lock set by %dn, getpid(); break; case F_WRLCK: printf(

7、Write lock set by %dn, getpid(); break; case F_UNLCK: printf(Release lock by %dn, getpid(); return 1; break; default: break; /* end of switch*/ return 0;生产者程序的源代码: producer.c/* producer.c */ #include #include #include #include #include #include mylock.h 整理为word格式#define MAXLEN 10 /* 缓冲区大小最大值*/#defin

8、e ALPHABET1 /* 表示使用英文字符 */#define ALPHABET_START a /* 头一个字符,可以用 A*/ #define COUNT_OF_ALPHABET 26 /* 字母字符的个数 */#define DIGIT 2 /* 表示使用数字字符 */#define DIGIT_START 0 /* 头一个数字字符 */#define COUNT_OF_DIGIT 10 /* 数字字符的个数 */#define SIGN_TYPE ALPHABET/* 本实例用英文字符 */const char *fifo_file = ./myfifo;/* ! FIFO 文件名

9、 */char buffMAXLEN;/* 缓冲区 */* 函数product() 产生一个字符并写入仿真 FIFO 文件中 */ int roduct(void) int fd; unsigned int sign_type, sign_start, sign_count, size; static unsigned int counter = 0; /* 打开! FIFO 文件 */ if (fd = open(fifo_file, O_CREAT|O_RDWR|O_APPEND, 0644) 0) printf(Open fifo file errorn); exit(1); sign_

10、type = SIGN_TYPE; switch(sign_type) case ALPHABET: /* 英文字符 */ sign_start = ALPHABET_START; sign_count = COUNT_OF_ALPHABET; break; case DIGIT: /* 数字字符 */ 整理为word格式 sign_start = DIGIT_START; sign_count = COUNT_OF_DIGIT; break; default: return -1; /*end of switch*/ sprintf(buff, %c, (sign_start + count

11、er); counter = (counter + 1) % sign_count; lock_set(fd, F_WRLCK); /* 上写锁*/ if (size = write(fd, buff, strlen(buff) 1) /* 第一个参数表示生产周期 */ sscanf(argv1, %d, &time_step); if (argc 2) /* 第二个参数表示需要生产的资源数 */ sscanf(argv2, %d, &time_life); while (time_life-) 整理为word格式 if (product() 0) break; sleep(time_step

12、); exit(EXIT_SUCCESS);消费者程序的源代码: customer.c /* customer.c */ #include #include #include #include #include #include mylock.h #define MAX_FILE_SIZE100 * 1024 * 1024 /* 100M*/const char *fifo_file = ./myfifo; /* 仿真 FIFO 文件名 */ const char *tmp_file = ./tmp; /* 临时文件名 */ /* 资源消费函数customing */int customing

13、(const char *myfifo, int need) int fd; char buff; int counter = 0; if (fd = open(myfifo, O_RDONLY) 0) 整理为word格式 printf(Function customing errorn);return -1; printf(Enjoy:); lseek(fd, SEEK_SET, 0); while (counter need) while (read(fd, &buff, 1) = 1) & (counter need) fputc(buff, stdout); /* -.就是在屏幕上/0

14、的显示 */ counter+; fputs(n, stdout); close(fd); return 0;/* myfilecopy()函数: 实现从 sour_file 文件的 offset 偏移处开始将 count 个字节数据复制到 dest_file 文件 */ int myfilecopy(const char *sour_file,const char *dest_file, int offset, int count, int copy_mode) int in_file, out_file; int counter = 0; char buff_unit; if (in_fi

15、le = open(sour_file, O_RDONLY|O_NONBLOCK) 0) printf(Function myfilecopy error in source filen); return -1; if (out_file = open(dest_file, O_CREAT|O_RDWR|O_TRUNC|O_NONBLOCK, 0644) 0) 整理为word格式 printf(Function myfilecopy error in destination file:); return -1; lseek(in_file, offset, SEEK_SET); while (

16、read(in_file, &buff_unit, 1) = 1) & (counter count) write(out_file, &buff_unit, 1); counter+; close(in_file); close(out_file); return 0; /* custom()函数:实现 FIFO 消费者 */ int custom(int need) int fd; /* 对资源进行消费,need 表示该消费的资源数目 */ customing(fifo_file, need); if (fd = open(fifo_file, O_RDWR) 1) /* 第一个参数指定需

17、要消费的资源数目,默认值为 10 */ sscanf(argv1, %d, &customer_capacity); if (customer_capacity 0) custom(customer_capacity); exit(EXIT_SUCCESS);(3) 分别编译生产者程序producer.c 和消费者程序customer.c(4) 确保编译没有错误后,先在控制台终端1上运行生产者程序:./producer 1 20再在控制台终端2上运行消费者程序:./customer 54.完成实验报告 (1)将实验过程和实验结果截图整理为word格式 (2)认真阅读程序代码,参考代码并根据实验

18、结果,完成实验总结。该程序利用文件模拟管道来传输数据,使用设置文件描述符的状态来实现文件上锁与解锁的功能,防止在读写同一文件由于同步操作产生错误,对比于操作系统的信号同步,其原理是一样的。整理为word格式5附加实验作业:用write, read, open等系统调用编写分别实现如下功能的程序(要求进行必要的出错检查):(1)创建一个文件testfile.txt,文件内容从键盘输入;(2)将testfile.txt的内容显示在屏幕上,并将testfile.txt的内容复制到一个新的文件file2.txt中。input.c#include #include #include #include #

19、include const char *fifo_file = ./testfile.txt;int main() int fd; char buffer1024; if (fd = open(fifo_file, O_CREAT|O_RDWR|O_APPEND, 0644) 0) printf(Open fifo file errorn); exit(1); printf(please input some data:); scanf(%s,buffer); write(fd,buffer,strlen(buffer); close(fd); return 0;整理为word格式copy_o

20、utput.c#include #include #include #include const char *fifo_file_src = ./testfile.txt;const char *fifo_file_dst = ./file2.txt;int main() int fd_src; int fd_dst;int real_num; char buffer1024; if (fd_src = open(fifo_file_src,O_RDWR|O_APPEND) 0) /第三个参数便是创建的文件的属性 printf(Open textfile errorn); exit(1); i

21、f (fd_dst = open(fifo_file_dst, O_CREAT|O_RDWR|O_APPEND, 0644) 0) printf(Open file2 errorn); exit(1); buffer100=0;while(real_num=read(fd_src,buffer,100)!=0)整理为word格式 if(real_num=-1) printf(read textfile errorn); exit(1); if(write(fd_dst,buffer,real_num)!=real_num) printf(write textfile errorn); exit(1); printf(%s,buffer);/打印输出/memzero(buffer);/printf(n);/close(fd_src); close(fd_dst); return 0; 整理为word格式 友情提示:本资料代表个人观点,如有帮助请下载,谢谢您的浏览! 整理为word格式

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!