#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <my_head.h>
void*copy(void* arg)
{
int fd=open("./1.jpg",O_RDWR);
int fd2=open("./2.jpg",O_RDWR|O_APPEND);
ssize_t res=0;
char buf[20]="";
lseek(fd2,*(int*)arg,SEEK_SET);
while(1){
res=read(fd,buf,sizeof(buf));
if(res==0){
break;
}
write(fd2,buf,res);
}
close(fd2);
}
int main(int argc, const char *argv[])
{
int fd=open("./2.jpg",O_RDWR|O_CREAT|O_APPEND,0664);
close(fd);
int fd1=open("./1.jpg",O_RDWR);
int fd2=open("./2.jpg",O_RDWR|O_APPEND);
ssize_t res=0;
off_t len=lseek(fd1,0,SEEK_END);
lseek(fd1,0,SEEK_SET);
ssize_t sum=0;
char buf[20]="";
while(1){
res=read(fd1,buf,sizeof(buf));
sum+=res;
if(sum<=(len/2))
{
write(fd2,buf,res);
}
else{
write(fd2,buf,res);
break;
}
}
pthread_t tid;//创建分支线程
int len1=lseek(fd1,0,SEEK_CUR);
if(pthread_create(&tid,NULL,copy,(void*)&len1)!=0){
fprintf(stderr,"fail _%d_\n",__LINE__);
}
pthread_join(tid,NULL);
close(fd1);
close(fd2);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <my_head.h>
char buf[]="1234567";
void* daozhi(void* arg)
{
char *p = buf;
int size = strlen(buf);
while(1){
int j = size-1;
for(int i = 0; i < j; i++){
char temp = *(p+i);
*(p+i) = *(p+j);
*(p+j) = temp;
j--;
}
}
}
int main(int argc, const char *argv[])
{
pthread_t tid;
if(pthread_create(&tid,NULL,daozhi,NULL)!=0)
{
fprintf(stderr,"fault__%d__\n",__LINE__);
return -1;
}
//char buf[]="1234567";
while(1)
{
if(strcmp("1234567",buf)==0||strcmp("7654321",buf))
printf("%s\n",buf);
}
return 0;
}