#include <myhead.h>
typedef struct
{
int leng;
}length;
void copy(int start,int a)
{
int fd1=-1;
int fd2=-1;
if((fd1=open("./1.txt",O_RDONLY))==-1)
{
perror("first error!");
}
if((fd2=open("./2.txt",O_RDWR|O_CREAT|O_APPEND,0664))==-1)
{
perror("second error!");
}
lseek(fd1,start,SEEK_SET);
char str[128];
int res=read(fd1,str,(a-a/2));
write(fd2,str,res);
close(fd1);
close(fd2);
}
void *task1(void *arg)
{
copy(0,(*((int*)arg)));
}
void *task2(void *arg)
{
copy(*((int*)arg)/2,*((int*)arg));
}
int main(int argc, const char *argv[])
{
int fd=-1;
if((fd=open("./1.txt",O_RDWR|O_CREAT|O_TRUNC,0664))==-1)
{
perror("first error!");
return -1;
}
char str[128]="hello world!";
write(fd,str,strlen(str));
write(1,str,strlen(str));
puts("");
int len=lseek(fd,0,SEEK_END);
lseek(fd,0,SEEK_SET);
// length.leng=len;
pthread_t tid=-1;
if(pthread_create(&tid,NULL,task1,&len)!=0)
{
printf("tid create error");
return -1;
}
pthread_t tid1=-1;
if(pthread_create(&tid1,NULL,task2,&len)!=0)
{
printf("tid create error");
return -1;
}
close(fd);
return 0;
}
12-07
于 2023-12-07 21:22:55 首次发布