临沂房产和房建设局网站双和,关键词排名怎么查,电子工程建设,贵阳网站制作免费1、使用多线程完成两个文件的拷贝#xff0c;第一个线程拷贝前一半#xff0c;第二个线程拷贝后一半#xff0c;主线程回收两个线程的资源
#include myhead.h
//定义分支线程1
void *task1(void *arg)
{int fdr-1;//只读打开被复制文件if((fdropen(./111.txt…1、使用多线程完成两个文件的拷贝第一个线程拷贝前一半第二个线程拷贝后一半主线程回收两个线程的资源
#include myhead.h
//定义分支线程1
void *task1(void *arg)
{int fdr-1;//只读打开被复制文件if((fdropen(./111.txt,O_RDONLY))-1){perror(open_r error);return NULL;}int fdw-1;//只写打开目标文件文件if((fdwopen(./222.txt,O_WRONLY|O_CREAT,0664))-1){perror(open_w error);return NULL;}//文件字节大小off_t sizelseek(fdr,0,SEEK_END);//光标lseek(fdr,0,SEEK_SET);lseek(fdw,0,SEEK_SET);//定义数组读取字符char strR[10];int sum0;while(1){int resread(fdr,strR,sizeof(strR));sumres;if(sumsize/2||res0){write(fdw,strR,res-(sum-size/2));break;}write(fdw,strR,res);// printf(111\n);}//关闭文件close(fdr);close(fdw);pthread_exit;
}
//定义分支线程2
void *task2(void *arg)
{int fdr-1;//只读打开被复制文件if((fdropen(./111.txt,O_RDONLY))-1){perror(open_r error);return NULL;}int fdw-1;//只写打开目标文件文件if((fdwopen(./222.txt,O_WRONLY|O_CREAT,0664))-1){perror(open_w error);return NULL;}//文件字节大小off_t sizelseek(fdr,0,SEEK_END);//光标lseek(fdr,size/2,SEEK_SET);lseek(fdw,size/2,SEEK_SET);//定义数组读取字符char strR[10];int sum0;while(1){int resread(fdr,strR,sizeof(strR));sumres;if(sumsize/2||res0){write(fdw,strR,res-(sum-(size-size/2)));break;}write(fdw,strR,res);// printf(111\n);}//关闭文件close(fdr);close(fdw);pthread_exit;
}
int main(int argc, const char *argv[])
{//定义线程号变量pthread_t tid1-1;pthread_t tid2-1;//创建线程1if(pthread_create(tid1,NULL,task1,NULL)!0){printf(tid1 create error\n);return -1;}//创建线程if(pthread_create(tid2,NULL,task2,NULL)!0){printf(tid2 create error\n);return -1;}//回收线程资源pthread_join(tid1,NULL);pthread_join(tid2,NULL);return 0;
} 2、思维导图