#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
int i;
char c;
void* callBack(void* arg)
{
int fd_r=open("./3.png",O_RDONLY);
if(fd_r<0)
{
perror("open");
return NULL;
}
int fd_w=open("./4.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd_w<0)
{
perror("open");
return NULL;
}
struct stat pstat;
if(stat("./3.png",&pstat)<0)
{
perror("stat");
return NULL;
}
off_t size=pstat.st_size;
printf("%ld\n",size);
off_t half_size=size/2;
int flag=size%2;
lseek(fd_r,0,SEEK_SET);
lseek(fd_w,0,SEEK_SET);
for(i=0;i<half_size;i++)
{
read(fd_r,&c,1);
write(fd_w,&c,1);
}
printf("前半部分拷贝完毕\n");
}
int main(int argc, const char *argv[])
{
int fd_r=open("./3.png",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
int fd_w=open("./4.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd_w<0)
{
perror("open");
return -1;
}
struct stat pstat;
if(stat("./3.png",&pstat)<0)
{
perror("stat");
return -1;
}
off_t size=pstat.st_size;
printf("%ld\n",size);
off_t half_size=size/2;
int flag=size%2;
pthread_t tid;
if(pthread_create(&tid,NULL,callBack,NULL))
{
perror("pthread_create");
return -1;
}
printf("线程创建成功\n");
sleep(10);
lseek(fd_r,half_size,SEEK_SET);
lseek(fd_w,half_size,SEEK_SET);
for(i=0;i<half_size+flag;i++)
{
read(fd_r,&c,1);
write(fd_w,&c,1);
}
printf("后半部分拷贝完毕\n");
return 0;
}