版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、. . jz* 操作系统:线程进程并发拷贝程序附录一:程序代码#include #include #include #include #include #include #include #define psize 4096 /* 管道文件的大小*/ #define bsize 128 /* 默认缓冲区的大小*/ #define nofile 20 /*u_ofile3575 表可分配的个数*/ #define nfile 20 /*file 表可分配的个数*/ #define npipe 20 /*pipecb3575 可分配的个数 */ /* 进程的 u_file 表*/ int u_of
2、ile3575nofile; /* 模拟 file 表*/ struct char f_flag;/* 读写标志, w表示写, r表示读 */ int f_count;/* 表示此表项的状态,=0 表示此表项没被使用,可分配;=1 表示此表项在被使用,不可再分配*/ int f_inode;/* 对应的 pipecb3575表下标 */ long f_offset;/* 读写指针,当前已读或已写个数*/ filenfile; /* 管道控制块 */ struct char *p_addr;/* 管道文件基地址*/ int p_size;/* 管道文件大小,psize*/ int p_count
3、;/*=2表示读写都在被进展,=1 表示在被读或被写,=0 表示管道没被使用,可分配 */ pipecb3575npipe; /* 模拟管道文件*/ char *pfile; /* 管道的写入写出端*/ int fd2; /* 锁机制,实现互斥*/ pthread_mutex_t lock = pthread_mutex_initializer; /* 进程间通信,实现同步*/ pthread_cond_t rflag = pthread_cond_initializer;/*读信号量 */ pthread_cond_t wflag = pthread_cond_initializer;/*写
4、信号量 */ . . jz* /* 线程创立函数只能传一个参数,用构造体来封装所有参数*/ struct arg_set char *fname; /* 文件名 */ int f; /* 传递 fdp*/ ; /*u_ofile3575 表初始化 */ int u_ofile_init3575() printf(init the u_ofile3575n); int i; for(i=0;inofile;i+) u_ofile3575i = -1; u_ofile35750=0; u_ofile35751=0; u_ofile35752=0; return 0; /* 创立管道 */ int
5、pipe_simulate3575(int a) printf(start to create a pipen); int i; for(i=0;inofile;i+) if(u_ofile3575i=-1) a0 = i;/* 读*/ u_ofile3575i = 0;/* 读端 */ break; for(i;i=nofile) printf(u_ofile3575 分配失败 ,failure to create a pipen); . . jz* return -2; pfile = (char *)malloc(psize*sizeof(char);/* 申请模拟管道用的存空间*/ i
6、f(pfile=null)/*申请可能不成功*/ printf(failure to create a pipen); return -1; for(i=0;infile;i+) if(filei.f_count!=1) filei.f_flag = r;/* 读标志 */ /filei.f_inode = 0;/*读对应 pipecb3575表下标 */ filei.f_count = 1;/*file0这个表项在被使用,不可再分配*/ filei.f_offset = 0;/* 读指针 */ u_ofile3575a0 = i;/* 读端 */ break; for(i=0;i=nfile
7、) printf(failure to create a pipen); return -1; for(i=0;i=npipe) printf(failure to create a pipen); return -1; printf(secceed create the pipen); return 0;/* 分配成功 */ /* 关闭管道 */ int close_simulate3575(int a) printf(start to close the pipe you have createdn); char *p; int i; for(i=0;ipipe)input data in
8、 memory *ca into pipen); numwrite_simulate3575+; long offr,offw;/* 读写指针,实际是读写字符个数*/ int r;/* 管道文件读端 */ int m;/* 假设 ca中的字符不能一次写完,m 用来表示一次可写入的字符的最大数*/ int w = u_ofile3575fd;/* 管道文件写端 */ . . jz* int pf = filew.f_inode;/*读管道对应的pipecb3575表的下标 */ int n1 = n;/* 一次应该写入的字符个数*/ int wstart = 0;/* 计数器,写入字符个数*/
9、int i = 0; for(i;ioffr)/*不能一次写完 */ if(pipecb3575pf.p_count=0)/*对文件的复制操作已进展完毕,管道文件被释放*/ printf( 对文件的复制操作已进展完毕,管道文件被释放n); return 0; else m = psize+offr-offw;/*最多可写入数*/ for(wstart=0;wstartm;wstart+) *(pipecb3575pf.p_addr+offw%psize) = *ca; ca+; offw+; filew.f_offset = offw;/*重定位写指针位置*/ n1 = n1-m;/* 剩余需
10、要读的字符个数*/ printf(weak up the read thread , pipe is readablen); pthread_cond_signal(&rflag);/* 唤醒读线程,管道可读*/ printf(write thread is blocked and under the statement of waitingn); pthread_cond_wait(&wflag,&lock);/* 写线程封锁等待*/ . . jz* /* 一次性可将ca中容全部写入管道*/ offr = filer.f_offset; offw = filew.f_
11、offset; for(wstart=0;wstartpipe)input data in memory *ca into pipen); return n;/* 返回写入字符个数*/ /* 读管道 */ int numread_simulate3575;/read_simulate3575 diaoyongshu int read_simulate3575(int fd,char *ca,int n)/ 将管道 fd 容读取 n 个字符到存ca里 printf(pipe-memory)output data from pipe into memory *can); numread_simul
12、ate3575+; long offr,offw;/* 读写指针,实际是读写字符个数*/ int w;/* 管道文件写端 */ int m;/* 假设 ca中的字符不能一次读完,m 用来表示一次可读出的字符的最大数*/ int r = u_ofile3575fd;/*管道文件读端 */ int pf = filer.f_inode;/*读管道对应的pipecb3575表的下标 */ int rstart = 0;/* 计数器,读出字符个数*/ int i = 0; for(i;infile;i+)/*寻找读管道对应的读管道的端*/ if(filei.f_flag=w)&(filei.f
13、_inode=pf) w = i; break; else continue; . . jz* printf(-add the lockn); pthread_mutex_lock(&lock);/* 互斥锁,相当于进入临界区*/ offr = filer.f_offset;/*赋值读指针 */ offw = filew.f_offset;/*赋值写指针 */ if(offr=offw)/*管道空,无容可读*/ printf(pipe is empty , nothing to outputn); if(pipecb3575pf.p_count=1)/*写端关闭 */ pipecb35
14、75pf.p_count-;/* 文件的复制以完成,释放管道文件的空间*/ printf(the write point is closed,the copy of the file is finishedn); return 0; else printf(weak up the write thread , make the pipe writablen); pthread_cond_signal(&wflag);/* 唤醒写线程,管道可写*/ printf(read thread is blocked and under the statement of waitingn); pt
15、hread_cond_wait(&rflag,&lock);/* 读线程封锁等待*/ offr = filer.f_offset; offw = filew.f_offset; m = n=(offw-offr)n:(offw-offr);/*得到可读字符个数*/ for(rstart=0;rstartmemory)output data from the pipe into memory *can); return m; /* 线程调用,读源文件,写管道*/ . . jz* void *pwrite3575(void *a)/ 将源文件a 容写入管道中 printf(file
16、-pipe)input data from the original file into pipen); char abuf1bsize; struct arg_set *args=(struct arg_set *)a;/*需要传入多个参数时,用构造体传*/ int fdr; int n_r;/* 管道文件写入字符数*/ if(fdr=open(args-fname,o_rdonly)!=-1) while(n_r=read(fdr,abuf1,bsize)0)/*读文件,写管道*/ printf(file-memory)input data in original file into me
17、mory *can); printf(secceed (file-memory)input data in original file into memory *can); /printf(memory-pipe)input data in memory *ca into pipen); write_simulate3575(args-f,abuf1,n_r); /printf(secceed (memory-pipe)inputn); pipecb3575fileu_ofile3575args-f.f_inode.p_count-;/* 文件已读完,关闭管道写端*/ else perror(
18、args-fname);/* 翻开源文件可能不成功*/ return null; printf(secceed (file-pipe)input data from the original file into pipen); return null; /* 线程调用,写目标文件,读管道*/ void *pread3575(void *a)/ 读取管道,将其中容写入新建文件中 printf(pipe-file)output data from the pipe into new filen); char abuf2bsize;/* 缓冲区 */ struct arg_set *args=(st
19、ruct arg_set *)a;/*需要传入多个参数时,用构造体传*/ int fdw; int n_w;/* 管道文件读出字符数*/ if(fdw=open(args-fname,o_creat|o_rdwr,0777)!=-1) while(n_w=read_simulate3575(args-f,abuf2,bsize)0)/* 读管道,写文件*/ /printf(pipe-memory)output data from pipe into memory *can); /printf(secceed (pipe-memory)output data from pipe into mem
20、ory *can); printf(memory-file)output data in memory *ca into filen); write(fdw,abuf2,n_w); . . jz* printf(secceed (memory-file)output data from the pipe into new filen); else perror(args-fname);/* 翻开目标文件可能出错*/ return null; printf(secceed (pipe-file)output data from the pipe into new filen); return n
21、ull; /* 主函数 */ int main(int argc,char *argv) int x; u_ofile_init3575(); while(x=pipe_simulate3575(fd)=-1);/* 创立管道,即申请空间*/ if(x=-2) return -1; pthread_t t;/thread id struct arg_set args2;/*用构造体传写线程需要的参数:文件名 ,管道文件读写端*/ args0.fname=argv1;/* 源文件名 */ args0.f=fd1;/* 管道文件写端*/ args1.fname=argv2;/* 目标文件名 */
22、args1.f=fd0;/* 管道文件读端*/ pthread_create(&t,null,pwrite3575,(void *)&args0);/* 创立子线程 ,写管道 */ pread3575(void *)&args1);/* 主线程调用 ,读管道 */ pthread_join(t,null);/*等待写线程完毕*/ close_simulate3575(fd); printf(nnum of read_simulate3575 = %dn,numread_simulate3575); printf(num of write_simulate3575 = %
23、dn,numwrite_simulate3575); return 0; 附录二实验结果 :ls -l 总用量2379 -rw-r-r- 1 john john 50 2012-12-17 18:06 3575 -rw-r-r- 1 john john 36864 2012-07-15 07:53 3575.doc -rw-r-r- 1 john john 95441 2012-08-27 22:16 3575.jpg -rw-r-r- 1 john john 2083969 2010-11-08 13:07 3575.mp3 . . jz* -rw-r-r- 1 john john 8483
24、5 2012-07-11 13:22 3575.pdf -rw-r-r- 1 john john 73728 2012-12-17 16:48 3575.ppt -rw-r-r- 1 john john 19924 2012-07-03 18:51 3575.rar -rw-r-r- 1 john john 0 2012-12-17 22:49 sta1 -rw-r-r- 1 john john 11563 2012-12-17 22:46 tc_copy.cpp ./tc 3575 a3575 copy1 ./tc 3575 a3575.doc ./tc 3575.doc a3575.doc
25、 ./tc 3575.ppt a3575.ppt ./tc 3575.mp3 a3575.mp3 ./tc 3575.rar a3575.rar copy2 ./tc 3575.pdf a3575.rar ls -l 总用量5163 -rw-r-r- 1 john john 50 2012-12-17 18:06 3575 -rw-r-r- 1 john john 36864 2012-07-15 07:53 3575.doc -rw-r-r- 1 john john 95441 2012-08-27 22:16 3575.jpg -rw-r-r- 1 john john 2083969 20
26、10-11-08 13:07 3575.mp3 -rw-r-r- 1 john john 84835 2012-07-11 13:22 3575.pdf -rw-r-r- 1 john john 73728 2012-12-17 16:48 3575.ppt -rw-r-r- 1 john john 19924 2012-07-03 18:51 3575.rar -rwxr-xr-x 1 john john 50 2012-12-17 22:51 a3575 -rwxr-xr-x 1 john john 36864 2012-12-17 22:51 a3575.doc -rwxr-xr-x 1
27、 john john 95441 2012-12-17 22:53 a3575.jpg -rwxr-xr-x 1 john john 2083969 2012-12-17 22:54 a3575.mp3 -rwxr-xr-x 1 john john 84835 2012-12-17 22:56 a3575.pdf -rwxr-xr-x 1 john john 73728 2012-12-17 22:53 a3575.ppt -rwxr-xr-x 1 john john 19924 2012-12-17 22:55 a3575.rar -rw-r-r- 1 john john 1320 2012
28、-12-17 22:51 copy1 -rw-r-r- 1 john john 419294 2012-12-17 22:56 copy2 -rw-r-r- 1 john john 690 2012-12-17 22:49 sta1 -rw-r-r- 1 john john 0 2012-12-17 22:57 sta2 -rwxr-xr-x 1 john john 12256 2012-12-17 22:50 tc -rw-r-r- 1 john john 11563 2012-12-17 22:46 tc_copy.cpp cat copy1 init the u_ofile start to create a pipe secceed create the pipe (pipe-file)output data from the pipe into new file (pipe-memory)output data from pipe into memory *ca . . jz* -add the lock pipe is empty , nothing to output weak up the write thread , make the pipe writable read thread is blocked a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高级统计师资格考试(高级统计实务与案例分析)试题库及答案(江苏省扬州市2026年)
- 初级统计师资格考试(统计专业知识和实务)试题库及答案(上海市2026年)
- 2026年验船师考试(C级船舶检验专业实务)强化训练试题及答案一
- 2026年上半年甘肃嘉峪关全国英语等级考试(PETS)一级B全真模拟试题及答案解析
- 2026年全国成人高等学校招生考试(数学(文)-高起点)模拟试题及答案
- 煤矿开采单位驾驶员定期维护安全操作规程
- 有机奶粉全球市场总体规模
- 公共营养师之四级营养师高分题库附答案
- 2026年高级统计师资格考试(高级统计实务与案例分析)试题库及答案(浙江温州)
- 电梯使用单位焊工运行操作安全操作规程
- 2026年西安市总工会建强实业集团有限公司招聘(26人)笔试备考试题及答案详解
- 2026年完整版临床三基考试试题及答案
- 福建省粮油食品进出口集团有限公司及其权属企业招聘笔试题库2026
- 2026年中级注册安全工程师《其他安全实务》能力检测及参考答案详解(模拟题)
- 2026年技术转移经纪人人才培养与职业资质认定知识考核
- (2026版)建筑施工特种作业人员管理规定课件
- 检验机构轮岗工作制度
- 医院投诉处理流程标准化手册
- 2026年护理安全警示教育与质量提升实践
- GB/T 13320-2025钢质模锻件金相组织评级图及评定方法
- 市政照明养护工程施工方案
评论
0/150
提交评论