首页 操作系统实验报告_线程并发拷贝程序

操作系统实验报告_线程并发拷贝程序

举报
开通vip

操作系统实验报告_线程并发拷贝程序操作系统:线程(进程)并发拷贝程序附录一:程序代码#include#include#include#include#include#include#include#definePSIZE4096/*管道文件的大小*/#defineBSIZE128/*默认缓冲区的大小*/#defineNOFILE20/*u_ofile3705表可分配的个数*/#defineNFILE20/*file表可分配的个数*/#defineNPIPE20/*pipecb3705可分配的个数*//*进程的u_file表*/intu_ofile3...

操作系统实验报告_线程并发拷贝程序
操作系统:线程(进程)并发拷贝程序附录一:程序代码#include#include#include#include#include#include#include#definePSIZE4096/*管道文件的大小*/#defineBSIZE128/*默认缓冲区的大小*/#defineNOFILE20/*u_ofile3705 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 可分配的个数*/#defineNFILE20/*file表可分配的个数*/#defineNPIPE20/*pipecb3705可分配的个数*//*进程的u_file表*/intu_ofile3705[NOFILE];/*模拟file表*/struct{charf_flag;/*读写标志,'w'表示写,'r'表示读*/intf_count;/*表示此表项的状态,=0表示此表项没被使用,可分配;=1表示此表项在被使用,不可再分配*/intf_inode;/*对应的pipecb3705表下标*/longf_offset;/*读写指针,当前已读或已写个数*/}file[NFILE];/*管道控制块*/struct{char*p_addr;/*管道文件基地址*/intp_size;/*管道文件大小,PSIZE*/intp_count;/*=2表示读写都在被进行,=1表示在被读或被写,=0表示管道没被使用,可分配*/}pipecb3705[NPIPE];/*模拟管道文件*/char*pfile;/*管道的写入写出端*/intfd[2];/*锁机制,实现互斥*/pthread_mutex_tlock=PTHREAD_MUTEX_INITIALIZER;/*进程间通信,实现同步*/pthread_cond_trflag=PTHREAD_COND_INITIALIZER;/*读信号量*/pthread_cond_twflag=PTHREAD_COND_INITIALIZER;/*写信号量*//*线程创建函数只能传一个参数,用结构体来封装所有参数*/structarg_set{char*fname;/*文件名*/intf;/*传递fdp[]*/};/*u_ofile3705表初始化*/intu_ofile_init3705(){printf("inittheu_ofile3705\n");inti;for(i=0;i=NOFILE){printf("u_ofile3705分配失败,failuretocreateapipe\n");return-2;}pfile=(char*)malloc(PSIZE*sizeof(char));/*申请模拟管道用的内存空间*/if(pfile==NULL)/*申请可能不成功*/{printf("failuretocreateapipe\n");return-1;}for(i=0;i=NFILE){printf("failuretocreateapipe\n");return-1;}for(i=0;i=NPIPE){printf("failuretocreateapipe\n");return-1;}printf("Secceedcreatethepipe\n");return0;/*分配成功*/}/*关闭管道*/intclose_simulate3705(inta[]){printf("starttoclosethepipeyouhavecreated\n");char*p;inti;for(i=0;i<2;i++){p=pipecb3705[file[u_ofile3705[a[i]]].f_inode].p_addr;/*if(p!=NULL)free(p);//释放管道内存*/pipecb3705[file[u_ofile3705[a[i]]].f_inode].p_count=0;/*管道控制块计数清零*/file[u_ofile3705[a[i]]].f_count=0;/*file表项计数清零*/u_ofile3705[a[i]]=-1;/*u_ofile3705表项清空*/a[i]=-1;/*fdp[]清空?*/}printf("secceedclosethepipe\n");return0;}/*写管道*/intnumwrite_simulate3705;intwrite_simulate3705(intfd,char*ca,intn)//将内存ca内容写入n个字符到管道fd里{printf("(memory---->pipe)inputdatainmemory*caintopipe\n");numwrite_simulate3705++;longoffr,offw;/*读写指针,实际是读写字符个数*/intr;/*管道文件读端*/intm;/*若ca中的字符不能一次写完,m用来表示一次可写入的字符的最大数*/intw=u_ofile3705[fd];/*管道文件写端*/intpf=file[w].f_inode;/*读管道对应的pipecb3705表的下标*/intn1=n;/*一次应该写入的字符个数*/intwstart=0;/*计数器,写入字符个数*/inti=0;for(i;ioffr)/*不能一次写完*/{if(pipecb3705[pf].p_count==0)/*对文件的复制操作已进行结束,管道文件被释放*/{printf("对文件的复制操作已进行结束,管道文件被释放\n");return0;}else{m=PSIZE+offr-offw;/*最多可写入数*/for(wstart=0;wstartpipe)inputdatainmemory*caintopipe\n");returnn;/*返回写入字符个数*/}/*读管道*/intnumread_simulate3705;//read_simulate3705diaoyongshuintread_simulate3705(intfd,char*ca,intn)//将管道fd内容读取n个字符到内存ca里{printf("(pipe---->memory)outputdatafrompipeintomemory*ca\n");numread_simulate3705++;longoffr,offw;/*读写指针,实际是读写字符个数*/intw;/*管道文件写端*/intm;/*若ca中的字符不能一次读完,m用来表示一次可读出的字符的最大数*/intr=u_ofile3705[fd];/*管道文件读端*/intpf=file[r].f_inode;/*读管道对应的pipecb3705表的下标*/intrstart=0;/*计数器,读出字符个数*/inti=0;for(i;imemory)outputdatafromthepipeintomemory*ca\n");returnm;}/*线程调用,读源文件,写管道*/void*pwrite3705(void*a)//将源文件a内容写入管道中{printf("(file---->pipe)inputdatafromtheoriginalfileintopipe\n");charabuf1[BSIZE];structarg_set*args=(structarg_set*)a;/*需要传入多个参数时,用结构体传*/intfdr;intn_r;/*管道文件写入字符数*/if((fdr=open(args->fname,O_RDONLY))!=-1){while((n_r=read(fdr,abuf1,BSIZE))>0)/*读文件,写管道*/{printf("(file---->memory)inputdatainoriginalfileintomemory*ca\n");printf("Secceed(file---->memory)inputdatainoriginalfileintomemory*ca\n");//printf("(memory---->pipe)inputdatainmemory*caintopipe\n");write_simulate3705(args->f,abuf1,n_r);//printf("Secceed(memory---->pipe)input\n");}pipecb3705[file[u_ofile3705[args->f]].f_inode].p_count--;/*文件已读完,关闭管道写端*/}else{perror(args->fname);/*打开源文件可能不成功*/returnNULL;}printf("Secceed(file---->pipe)inputdatafromtheoriginalfileintopipe\n");returnNULL;}/*线程调用,写目标文件,读管道*/void*pread3705(void*a)//读取管道,将其中内容写入新建文件中{printf("(pipe---->file)outputdatafromthepipeintonewfile\n");charabuf2[BSIZE];/*缓冲区*/structarg_set*args=(structarg_set*)a;/*需要传入多个参数时,用结构体传*/intfdw;intn_w;/*管道文件读出字符数*/if((fdw=open(args->fname,O_CREAT|O_RDWR,0777))!=-1){while((n_w=read_simulate3705(args->f,abuf2,BSIZE))>0)/*读管道,写文件*/{//printf("(pipe---->memory)outputdatafrompipeintomemory*ca\n");//printf("Secceed(pipe---->memory)outputdatafrompipeintomemory*ca\n");printf("(memory---->file)outputdatainmemory*caintofile\n");write(fdw,abuf2,n_w);printf("Secceed(memory---->file)outputdatafromthepipeintonewfile\n");}}else{perror(args->fname);/*打开目标文件可能出错*/returnNULL;}printf("Secceed(pipe---->file)outputdatafromthepipeintonewfile\n");returnNULL;}/*主函数*/intmain(intargc,char*argv[]){intx;u_ofile_init3705();while((x=pipe_simulate3705(fd))==-1);/*创建管道,即申请空间*/if(x==-2)return-1;pthread_tt;//threadIDstructarg_setargs[2];/*用结构体传写线程需要的参数:文件名,管道文件读写端*/args[0].fname=argv[1];/*源文件名*/args[0].f=fd[1];/*管道文件写端*/args[1].fname=argv[2];/*目标文件名*/args[1].f=fd[0];/*管道文件读端*/pthread_create(&t,NULL,pwrite3705,(void*)&args[0]);/*创建子线程,写管道*/pread3705((void*)&args[1]);/*主线程调用,读管道*/pthread_join(t,NULL);/*等待写线程结束*/close_simulate3705(fd);printf("\nnumofread_simulate3705=%d\n",numread_simulate3705);printf("numofwrite_simulate3705=%d\n",numwrite_simulate3705);return0;}附录二(实验结果):ls-l总用量2379-rw-r--r--1johnjohn502012-12-1718:063705-rw-r--r--1johnjohn368642012-07-1507:533705.doc-rw-r--r--1johnjohn954412012-08-2722:163705.JPG-rw-r--r--1johnjohn20839692010-11-0813:073705.mp3-rw-r--r--1johnjohn848352012-07-1113:223705.pdf-rw-r--r--1johnjohn737282012-12-1716:483705. ppt 关于艾滋病ppt课件精益管理ppt下载地图下载ppt可编辑假如ppt教学课件下载triz基础知识ppt -rw-r--r--1johnjohn199242012-07-0318:513705.rar-rw-r--r--1johnjohn02012-12-1722:49sta1-rw-r--r--1johnjohn115632012-12-1722:46tc_copy.cpp./tc3705a3705>copy1./tc3705a3705.doc./tc3705.doca3705.doc./tc3705.ppta3705.ppt./tc3705.mp3a3705.mp3./tc3705.rara3705.rar>copy2./tc3705.pdfa3705.rarls-l总用量5163-rw-r--r--1johnjohn502012-12-1718:063705-rw-r--r--1johnjohn368642012-07-1507:533705.doc-rw-r--r--1johnjohn954412012-08-2722:163705.JPG-rw-r--r--1johnjohn20839692010-11-0813:073705.mp3-rw-r--r--1johnjohn848352012-07-1113:223705.pdf-rw-r--r--1johnjohn737282012-12-1716:483705.ppt-rw-r--r--1johnjohn199242012-07-0318:513705.rar-rwxr-xr-x1johnjohn502012-12-1722:51a3705-rwxr-xr-x1johnjohn368642012-12-1722:51a3705.doc-rwxr-xr-x1johnjohn954412012-12-1722:53a3705.JPG-rwxr-xr-x1johnjohn20839692012-12-1722:54a3705.mp3-rwxr-xr-x1johnjohn848352012-12-1722:56a3705.pdf-rwxr-xr-x1johnjohn737282012-12-1722:53a3705.ppt-rwxr-xr-x1johnjohn199242012-12-1722:55a3705.rar-rw-r--r--1johnjohn13202012-12-1722:51copy1-rw-r--r--1johnjohn4192942012-12-1722:56copy2-rw-r--r--1johnjohn6902012-12-1722:49sta1-rw-r--r--1johnjohn02012-12-1722:57sta2-rwxr-xr-x1johnjohn122562012-12-1722:50tc-rw-r--r--1johnjohn115632012-12-1722:46tc_copy.cppcatcopy1inittheu_ofilestarttocreateapipeSecceedcreatethepipe(pipe---->file)outputdatafromthepipeintonewfile(pipe---->memory)outputdatafrompipeintomemory*ca----addthelockpipeisempty,nothingtooutputweakupthewritethread,makethepipewritablereadthreadisblockedandunderthestatementofwaiting(file---->pipe)inputdatafromtheoriginalfileintopipe(file---->memory)inputdatainoriginalfileintomemory*caSecceed(file---->memory)inputdatainoriginalfileintomemory*ca(memory---->pipe)inputdatainmemory*caintopipe----addthelock----releasethelockSecceed(memory---->pipe)inputdatainmemory*caintopipeSecceed(file---->pipe)inputdatafromtheoriginalfileintopipeweakthewritethread,makethepipewritable----releasethelockSecceed(pipe---->memory)outputdatafromthepipeintomemory*ca(memory---->file)outputdatainmemory*caintofileSecceed(memory---->file)outputdatafromthepipeintonewfile(pipe---->memory)outputdatafrompipeintomemory*ca----addthelockpipeisempty,nothingtooutputthewritepointisclosed,thecopyofthefileisfinishedSecceed(pipe---->file)outputdatafromthepipeintonewfilestarttoclosethepipeyouhavecreatedsecceedclosethepipenumofreadp=2numofwritep=1
本文档为【操作系统实验报告_线程并发拷贝程序】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_769254
暂无简介~
格式:doc
大小:57KB
软件:Word
页数:11
分类:
上传时间:2019-07-18
浏览量:0