作业 :使用两个线程完成文件的拷贝写入,分线程1写入前半段,分线程2写入后半段,主线程用来回收资源
#include <head.h>
void *sork(void *arg);
void *sork2(void *arg);
int file_copy(int start,int len) //拷贝的函数
{
int rd = -1;
int wd = -1;
if((rd = open("./01_test.c",O_RDONLY)) == -1)
{
printf("open_r error\n");
}
if((wd = open("./text.txt",O_WRONLY)) == -1)
{
printf("open_w error\n");
}
lseek(rd,start,SEEK_SET);
lseek(wd,start,SEEK_SET);
char buf[128]="";
int count = 0;
while(1)
{
int res = read(rd,buf,sizeof(buf));
count += res;
if(count >= len || res == 0)
{
write(wd,buf,res-(count-len));
break;
}
write(wd,buf,res);
}
close(rd);
close(wd);
}
int length()
{
int rd = -1;
int wd = -1;
if((rd = open("./01_test.c",O_RDONLY)) == -1)
{
perror("open_r error\n");
return -1;
}
if((wd = open("./text.txt",O_WRONLY|O_CREAT|O_TRUNC,0664) == -1))
{
perror("open_w error\n");
return -1;
}
int len = lseek(rd,0,SEEK_END);
close(rd);
close(wd);
return len;
}
int main(int argc, const char *argv[])
{
pthread_t tid = -1;
pthread_t tid2 = -1;
int len = length(); //获取文件大小
if(pthread_create(&tid, NULL, sork, &len) != 0) //创建线程1
{
printf("创建线程失败\n");
return -1;
}
if(pthread_create(&tid2, NULL, sork2, &len) != 0) //创建线程2
{
printf("创建线程失败\n");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid2,NULL);
printf("已回收分线程资源\n");
return 0;
}
void *sork(void *arg) //分支线程1
{
int len = *(int *)arg;
file_copy(0,len/2); //调用拷贝的函数
printf("分支线程1退出\n");
pthread_exit(NULL);
}
void *sork2(void *arg) //分支线程2
{
int len = *(int *)arg;
file_copy(len/2,len-len/2); //调用拷贝的函数
printf("分支线程2退出\n");
pthread_exit(NULL);
}
结果
不调函数,一样
#include <head.h>
void *sork(void *arg);
void *sork2(void *arg);
int length()
{
int rd = -1;
int wd = -1;
if((rd = open("./01_test.c",O_RDONLY)) == -1)
{
perror("open_r error\n");
return -1;
}
if((wd = open("./text.txt",O_WRONLY|O_CREAT|O_TRUNC,0664) == -1))
{
perror("open_w error\n");
return -1;
}
int len = lseek(rd,0,SEEK_END);
close(rd);
close(wd);
return len;
}
int main(int argc, const char *argv[])
{
pthread_t tid = -1;
pthread_t tid2 = -1;
int len = length();
if(pthread_create(&tid, NULL, sork, &len) != 0) //创建线程1
{
printf("创建线程失败\n");
return -1;
}
if(pthread_create(&tid2, NULL, sork2, &len) != 0) //创建线程2
{
printf("创建线程失败\n");
return -1;
}
pthread_join(tid,NULL);
pthread_join(tid2,NULL);
return 0;
}
void *sork(void *arg) //分支线程1
{
int len = *(int *)arg;
int rd = -1;
int wd = -1;
printf("进入分支线程1\n");
if((rd = open("./01_test.c",O_RDONLY)) == -1)
{
printf("open_r error\n");
}
if((wd = open("./text.txt",O_WRONLY)) == -1)
{
printf("open_w error\n");
}
lseek(rd,0,SEEK_SET);
lseek(wd,0,SEEK_SET);
char buf[128]="";
int count = 0;
while(1)
{
int res = read(rd,buf,sizeof(buf));
count += res;
if(count >= len/2 || res == 0)
{
write(wd,buf,res-(count-len/2));
break;
}
write(wd,buf,res);
}
close(rd);
close(wd);
pthread_exit(NULL);
}
void *sork2(void *arg) //分支线程2
{
int len = *(int *)arg;
int rd = -1;
int wd = -1;
printf("进入分支线程2\n");
if((rd = open("./01_test.c",O_RDONLY)) == -1)
{
printf("open_r error\n");
}
if((wd = open("./text.txt",O_WRONLY)) == -1)
{
printf("open_w error\n");
}
lseek(rd,len/2,SEEK_SET);
lseek(wd,len/2,SEEK_SET);
char buf[128]="";
int count = 0;
while(1)
{
int res = read(rd,buf,sizeof(buf));
if(res == 0)
{
break;
}
write(wd,buf,res);
}
close(rd);
close(wd);
pthread_exit(NULL);
}
思维导图