#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
void *callback(void* arg)
{
int fd=open("1.png",O_RDONLY);
if(fd<0)
{
perror("open");
}
int fd1=open("2.png",O_RDWR|O_CREAT|O_TRUNC,0777);
if(fd1<0)
{
perror("open");
}
struct stat p;
if(stat("1.png",&p)<0)
{
perror("stat");
}
int i;
off_t size=p.st_size;
int flag=size%2;
off_t halfsize;
halfsize=size/2;
ssize_t n;
char c;
while(1)
{
lseek(fd,halfsize,SEEK_SET);
lseek(fd1,halfsize,SEEK_SET);
for(i=0;i<halfsize+flag;i++)
{
n=read(fd,&c,1);
if(n==0)
{
printf("打印完毕");
break;
}
if(n<0)
{
perror("read");
break;
}
write(fd1,&c,1);
}
sleep(1);
}
}
int main(int argc, const char *argv[])
{
int fd=open("1.png",O_RDONLY);
if(fd<0)
{
perror("open");
}
int fd1=open("2.png",O_RDWR|O_CREAT|O_TRUNC,0777);
if(fd1<0)
{
perror("open");
}
struct stat p;
if(stat(argv[1],&p)<0)
{
perror("stat");
}
int i;
off_t size=p.st_size;
int flag=size%2;
off_t halfsize;
halfsize=size/2;
ssize_t n;
char c;
pthread_t sid;
if(pthread_create(&sid,NULL,callback,NULL)!=0)
{
perror("pthread_create");
return -1;
}
while(1)
{
lseek(fd,0,SEEK_SET);
lseek(fd1,0,SEEK_SET);
for(i=0;i<halfsize;i++)
{
n=read(fd,&c,1);
if(n==0)
{
printf("打印完毕");
break;
}
if(n<0)
{
perror("read");
break;
}
write(fd1,&c,1);
}
sleep(1);
}
return 0;
}