SQL线程拷贝并发程序_第1页
SQL线程拷贝并发程序_第2页
SQL线程拷贝并发程序_第3页
SQL线程拷贝并发程序_第4页
SQL线程拷贝并发程序_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、二、代码:#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>#include<pthread.h>#include<malloc.h>#define PSIZE 4096 /*管道文件的大小*/#define BSIZE 512 /*默认缓冲区的大小*/#define NOFILE 20 /*u_ofile3083表可分配的个数*/#define NFILE 20 /*file

2、表可分配的个数*/#define NPIPE 20 /*pipecb3083可分配的个数*/*进程的u_file表*/int u_ofile3083NOFILE;/*模拟file表*/struct charf_flag;/*读写标志,'w'表示写,'r'表示读*/ int f_count;/*表示此表项的状态,=0表示此表项没被使用,可分配;=1表示此表项在被使用,不可再分配*/ int f_inode;/*对应的pipecb3083表下标*/ long f_offset;/*读写指针,当前已读或已写个数*/fileNFILE;/*管道控制块*/struct c

3、har *p_addr;/*管道文件基地址*/ int p_size;/*管道文件大小,PSIZE*/ int p_count;/*=2表示读写都在被进行,=1表示在被读或被写,=0表示管道没被使用,可分配*/pipecb3083NPIPE;/*模拟管道文件*/char *pfile;/*管道的写入写出端*/int fd2;/*锁机制,实现互斥*/pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;/*进程间通信,实现同步*/pthread_cond_t rflag = PTHREAD_COND_INITIALIZER;/*读信号量*/pthrea

4、d_cond_t wflag = PTHREAD_COND_INITIALIZER;/*写信号量*/*线程创建函数只能传一个参数,用结构体来封装所有参数*/struct arg_set char*fname; /*文件名*/ int f; /*传递fdp*/;unsigned long GetTickCount() struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (ts.tv_nsec/1000);unsigned longWrite_time;unsigned longRead_time;/*u_ofil

5、e3083表初始化*/int u_ofile_init3083() printf("init the u_ofile3083n"); int i; for(i = 0; i < NOFILE; i+) u_ofile3083i=-1; u_ofile30830=0; u_ofile30831=0; u_ofile30832=0; return 0;/*创建管道*/int pipe_simulate3083(int a)unsigned longtime = GetTickCount();printf("Creat pipe starts at %d usn&

6、quot;, time); printf("start creat pipen"); int i; for(i = 0; i < NOFILE; i+) if(u_ofile3083i = -1) a0 = i;/*读*/ u_ofile3083i = 0;/*读端*/ break; for(i; i < NOFILE; i+) if(u_ofile3083i = -1) a1 = i;/*写*/ u_ofile3083i = 1;/*写端*/ break; if(i >= NOFILE) printf("u_ofile3083 is false

7、,creat pipe is filuren"); return -2; pfile = (char *)malloc(PSIZE*sizeof(char);/*申请模拟管道用的内存空间*/ if(pfile = NULL)/*申请可能不成功*/ printf("malloc is false,failure to create a pipen"); return -1; for(i = 0; i < NFILE; i+) if(filei.f_count != 1) filei.f_flag = 'r'/*读标志*/ /filei.f_in

8、ode = 0;/*读对应pipecb3083表下标*/ filei.f_count = 1;/*file0这个表项在被使用,不可再分配*/ filei.f_offset = 0;/*读指针*/ u_ofile3083a0 = i;/*读端*/ break; for(i = 0; i < NFILE; i+) if(filei.f_count != 1) filei.f_flag='w'/*写标志*/ /filei.f_inode = 0;/*写对应pipecb3083控制块下标*/ filei.f_count = 1;/*file1这个表项在被使用,不可再分配*/ fi

9、lei.f_offset = 0;/*写指针*/ u_ofile3083a1 = i;/*写端*/ break; if(i >= NFILE) printf("file is false,creat pipe filurenn"); return -1; for(i = 0; i < NPIPE; i+) if(pipecb3083i.p_count = 0) pipecb3083i.p_addr = pfile;/*给管道文件基地址赋值*/ pipecb3083i.p_size = PSIZE;/*管道文件大小*/ pipecb3083i.p_count =

10、2;/*读写都在进行,此pipecb3083表项不可再分*/ fileu_ofile3083a0.f_inode = i; fileu_ofile3083a1.f_inode = i; break; if(i >= NPIPE) printf("pipe is false,creat pipe filuren"); return -1; printf("end creat pipe,succen");printf("Creat pipe ends at %d usn", GetTickCount(); time = GetTic

11、kCount() - time; printf("Creat pipe cost %d usn", time); return 0;/*分配成功*/*关闭管道*/int close_simulate3083(int a) printf("close pipe is start.n"); char *p; int i; for(i = 0; i< 2; i+) p=pipecb3083fileu_ofile3083ai.f_inode.p_addr; /* if(p != NULL) free(p); /释放管道内存 */ pipecb3083fil

12、eu_ofile3083ai.f_inode.p_count = 0; /*管道控制块计数清零*/ fileu_ofile3083ai.f_count = 0; /*file表项计数清零*/ u_ofile3083ai = -1; /*u_ofile3575表项清空*/ ai = -1; /*fdp清空?*/ printf("close pipe is end.n"); return 0;/*写管道*/static int numwrite_simulate3083;int writep3083(int fd, char *ca, int n)/将内存ca内容写入n个字符到

13、管道fd里 printf("writep(memory->pipe)put data from memory to pipen"); numwrite_simulate3083+; long offr,offw;/*读写指针,实际是读写字符个数*/ int r;/*管道文件读端*/ int m;/*若ca中的字符不能一次写完,m用来表示一次可写入的字符的最大数*/ int w = u_ofile3083fd;/*管道文件写端*/ int pf = filew.f_inode;/*读管道对应的pipecb3083表的下标*/ int n1 = n;/*一次应该写入的字符

14、个数*/ int wstart=0;/*计数器,写入字符个数*/ int i = 0; for(i; i < NFILE; i+)/*寻找写管道对应的读管道的读端*/ if(filei.f_flag = 'r') && (filei.f_inode = pf) r=i; break; else continue; printf("-lockn"); pthread_mutex_lock(&lock);/*互斥锁,相当于进入临界区*/ offr=filer.f_offset;/*赋值读指针*/ offw= filew.f_offs

15、et;/*赋值写指针*/ if(offw + n1 - PSIZE) > offr)/*不能一次写完*/ if(pipecb3083pf.p_count = 0)/*对文件的复制操作已进行结束,管道文件被释放*/ printf("copy file is over,pipe file releasn"); return 0; else m= PSIZE + offr - offw;/*最多可写入数*/ for(wstart = 0; wstart < m; wstart+) *(pipecb3083pf.p_addr+offw%PSIZE) = *ca; ca+

16、; offw+; filew.f_offset = offw;/*重定位写指针位置*/ n1 = n1 - m;/*剩余需要读的字符个数*/ printf("awake readers,pipe_read is ok.n"); pthread_cond_signal(&rflag);/*唤醒读线程,管道可读*/ printf("Write threads blocked waitedn"); pthread_cond_wait(&wflag,&lock);/*写线程封锁等待*/ /*一次性可将ca中内容全部写入管道*/ offr

17、= filer.f_offset; offw = filew.f_offset; for(wstart = 0; wstart < n1; wstart+) /*printf("%dn",pipecb3083pf.p_addr);*/ *(pipecb3083pf.p_addr + offw % PSIZE) = *ca; /*printf("%dn",wstart);*/ ca+; offw+; filew.f_offset = offw; pthread_cond_signal(&rflag); printf("-unlock

18、n"); pthread_mutex_unlock(&lock); printf("writep(memory->pipe)success put the data from memory to pipen");printf("%d-Write_pipe ends at %d usn", numwrite_simulate3083, GetTickCount();Write_time = GetTickCount() - Write_time;printf("%d-Write_pipe cost %d usn"

19、, numwrite_simulate3083, Write_time); /printf("Secceed (memory->pipe)inputn"); return n;/*返回写入字符个数*/*读管道*/int numread_simulate3083;/read_simulate3083 diaoyongshuint readp3083(int fd,char *ca,int n)/将管道fd内容读取n个字符到内存ca里 printf("read(pipe->memory)put the data from pipe to memoryn&q

20、uot;); numread_simulate3083+; longoffr,offw;/*读写指针,实际是读写字符个数*/ int w;/*管道文件写端*/ int m;/*若ca中的字符不能一次读完,m用来表示一次可读出的字符的最大数*/ int r = u_ofile3083fd;/*管道文件读端*/ int pf = filer.f_inode;/*读管道对应的pipecb3083表的下标*/ int rstart = 0;/*计数器,读出字符个数*/ int i = 0; for(i; i < NFILE; i+)/*寻找读管道对应的读管道的端*/ if(filei.f_fla

21、g = 'w') && (filei.f_inode = pf) w = i; break; else continue; printf("-lockn"); pthread_mutex_lock(&lock);/*互斥锁,相当于进入临界区*/ offr = filer.f_offset;/*赋值读指针*/ offw = filew.f_offset;/*赋值写指针*/ if(offr = offw)/*管道空,无内容可读*/ printf("pipe is null,nothing can be readn");

22、 if(pipecb3083pf.p_count = 1)/*写端关闭*/ pipecb3083pf.p_count-;/*文件的复制以完成,释放管道文件的空间*/ printf("copy file is successn"); return 0; else printf("awake writers,pipe_write is ok.n"); pthread_cond_signal(&wflag);/*唤醒写线程,管道可写*/ printf("Read threads blocked waitedn"); pthread_

23、cond_wait(&rflag,&lock);/*读线程封锁等待*/ offr = filer.f_offset; offw = filew.f_offset; m = n <= (offw-offr)?n:(offw-offr);/*得到可读字符个数*/ for(rstart = 0; rstart < m; rstart+) *ca=*(pipecb3083pf.p_addr+offr%PSIZE); ca+; offr+; filer.f_offset=offr;/*重定位读指针位置*/ printf("awake writers,Read thr

24、eads blocked waitedn"); pthread_cond_signal(&wflag); printf("unlockn"); pthread_mutex_unlock(&lock); printf("readp(pipe->memory)success put the data from pipe to memoryn");printf("%d-Read_pipe ends at %d usn", numread_simulate3083, GetTickCount();Read_ti

25、me = GetTickCount() - Read_time;printf("%d-Read_pipe cost %d usn", numread_simulate3083, Read_time); return m;/*线程调用,读源文件,写管道*/void *write_simulate3083(void *a)/将源文件a内容写入管道中 printf("write_simulate start(file->pipe)put the data from file to pipen"); charabuf1BSIZE; struct arg_s

26、et *args=(struct arg_set *)a;/*需要传入多个参数时,用结构体传*/ int fdr; int n_r;/*管道文件写入字符数*/ if(fdr=open(args->fname, O_RDONLY) != -1) Write_time = GetTickCount();printf("0-Write_pipe starts at %dn", Write_time); printf("(file->memory)put the data from file to memoryn"); while(n_r = rea

27、d(fdr,abuf1,BSIZE) > 0)/*读文件,写管道*/ printf("(file->memory)success put the data from file to memoryn"); /printf("(memory->pipe)input data in memory *ca into pipen"); writep3083(args->f,abuf1,n_r);Write_time = GetTickCount();printf("%d-Write_pipe starts at %dn"

28、, numwrite_simulate3083 + 1, Write_time);/下一次写管道执行开始时间 pipecb3083fileu_ofile3083args->f.f_inode.p_count-;/*文件已读完,关闭管道写端*/ else perror(args->fname);/*打开源文件可能不成功*/ return NULL; printf("end(file->pipe)put the data from file to pipen"); return NULL;/*线程调用,写目标文件,读管道*/void *read_simulat

29、e3083(void *a)/读取管道,将其中内容写入新建文件中 printf("read_simulate3083(pipe->file)put the data from pipe to filen"); char abuf2BSIZE;/*缓冲区*/ struct arg_set *args=(struct arg_set *)a;/*需要传入多个参数时,用结构体传*/ int fdw; int n_w;/*管道文件读出字符数*/ if(fdw=open(args->fname,O_CREAT|O_RDWR,0777)!=-1) Read_time = G

30、etTickCount();printf("0-Read_pipe starts at %dn", Read_time); while(n_w=readp3083(args->f,abuf2,BSIZE)>0)/*读管道,写文件*/ printf("(memory->file)put data from memory to filen"); write(fdw,abuf2,n_w); printf("(memory->file)success put data from memory to filen");Read_time = GetTickCount();printf("%d-Read_pipe starts at %dn", numread_simulate3083 + 1, Read_time);/下一次读管道执行开始时间 else perror(args->fname);/*打开目标文件可能出错*/ return NULL; printf("(pipe->file)put the

温馨提示

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

评论

0/150

提交评论