linux下c语言多线程拷贝文件出现问题,有时候会拷贝成功,有时候会失败,求原因 40
//用多线程拷贝一个文件#include<stdio.h>#include<pthread.h>#include<unistd.h>#include<sys/stat.h...
//用多线程拷贝一个文件
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define NUM 10
char w[20];
char r[20];
void *funcpy(void *arg)
{
int i = (int)arg;
int ret;
int fdr,fdw;
if((fdr = open(r, O_RDONLY)) == -1)
{
perror("open r");
return;
}
if(-1 == (fdw = open(w, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)))
{
perror("open w");
return;
}
int allsize = lseek(fdr, 0, SEEK_END);
//偏移
int lseek1, lseek2;
lseek1 = lseek(fdr, allsize/NUM * i , SEEK_SET);
lseek2 = lseek(fdw, allsize/NUM * i , SEEK_SET);
printf("%d,%d\n",lseek1,lseek2);
//计算该线程要复制文件的大小
int writesize = allsize/NUM;
if(i+1 == NUM)
writesize += allsize % NUM;
int nread,nwrite;
char buf[20];
while(1)
{
//读取文件的数据到buf
nread = read(fdr, buf, 20);
writesize -= nread;
if(writesize <0)
nread += writesize;
//把buf中的数据写入文件
nwrite = write(fdw, buf, nread);
if(nwrite != nread)
{
perror("write");
return;
}
// printf("[%d]nread:%d,nwrite:%d,writesize:%d\n",i,nread,nwrite,writesize);
if(nread < 20)
break;
}
close(fdr);
close(fdw);
return (void *)0;
}
int main(int argc, char **argv)
{
if(argc != 3)
{
printf("Usage:%s <src> <dst>", argv[0]);
return -1;
}
strcpy(w, argv[2]);
strcpy(r, argv[1]);
pthread_t tid[NUM];
int i;
for(i=0; i<NUM; i++)
{
pthread_create(&tid[i], 0, funcpy, (void *)i);
}
for(i=0; i<NUM; i++)
{
pthread_join(tid[i], NULL);
}
return 0;
}
还是自己解决了,1每次打开写的文件都清空了,2循环读取的最后一个判断有点问题。ps:出现问题还是要靠自己。 展开
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define NUM 10
char w[20];
char r[20];
void *funcpy(void *arg)
{
int i = (int)arg;
int ret;
int fdr,fdw;
if((fdr = open(r, O_RDONLY)) == -1)
{
perror("open r");
return;
}
if(-1 == (fdw = open(w, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)))
{
perror("open w");
return;
}
int allsize = lseek(fdr, 0, SEEK_END);
//偏移
int lseek1, lseek2;
lseek1 = lseek(fdr, allsize/NUM * i , SEEK_SET);
lseek2 = lseek(fdw, allsize/NUM * i , SEEK_SET);
printf("%d,%d\n",lseek1,lseek2);
//计算该线程要复制文件的大小
int writesize = allsize/NUM;
if(i+1 == NUM)
writesize += allsize % NUM;
int nread,nwrite;
char buf[20];
while(1)
{
//读取文件的数据到buf
nread = read(fdr, buf, 20);
writesize -= nread;
if(writesize <0)
nread += writesize;
//把buf中的数据写入文件
nwrite = write(fdw, buf, nread);
if(nwrite != nread)
{
perror("write");
return;
}
// printf("[%d]nread:%d,nwrite:%d,writesize:%d\n",i,nread,nwrite,writesize);
if(nread < 20)
break;
}
close(fdr);
close(fdw);
return (void *)0;
}
int main(int argc, char **argv)
{
if(argc != 3)
{
printf("Usage:%s <src> <dst>", argv[0]);
return -1;
}
strcpy(w, argv[2]);
strcpy(r, argv[1]);
pthread_t tid[NUM];
int i;
for(i=0; i<NUM; i++)
{
pthread_create(&tid[i], 0, funcpy, (void *)i);
}
for(i=0; i<NUM; i++)
{
pthread_join(tid[i], NULL);
}
return 0;
}
还是自己解决了,1每次打开写的文件都清空了,2循环读取的最后一个判断有点问题。ps:出现问题还是要靠自己。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询