#include <myhead.h>
void*copy(void* arg)
{
int fd=open("./g.gif",O_RDWR);
int fd2=open("./1.gif",O_RDWR|O_APPEND);
ssize_t res=0;
char buf[20]="";
lseek(fd2,*(int*)arg,SEEK_SET);
sleep(1);
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("./1.gif",O_RDWR|O_CREAT|O_APPEND,0664);
close(fd);
int fd1=open("./g.gif",O_RDWR);
int fd2=open("./1.gif",O_RDWR|O_APPEND);
ssize_t res=0;
off_t len=lseek(fd,0,SEEK_END);
ssize_t sum=0;
char buf[20]="";
lseek(fd,0,SEEK_SET);
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<myhead.h>
char buf[] = "1234567";
void* change(void* arg)
{
while(1)
{
if(*(int*)arg == 1)
{
int len = strlen(buf);
int j = len-1;
char temp = 0;
for (int i = 0; i < len/2; i++, j--)
{
temp = buf[j];
buf[j] = buf[i];
buf[i] = temp;
}
*(int*)arg = 0;
}
}
}
int main(int argc, const char *argv[])
{
int t = 0;
pthread_t tid;
if (pthread_create(&tid, NULL, change, (void*)&t) != 0)
{
printf("pthread_create failed __%d__\n", __LINE__);
return -1;
}
while (1)
{
if (t == 0)
{
if (strcmp(buf, "1234567") == 0 || strcmp(buf, "7654321") == 0)
{
printf("%s\n", buf);
}
t = 1;
}
}
return 0;
}