1.
void *fun()
{
int fd1 = open("1.png", O_RDONLY);
int fd2 = open("3.png", O_WRONLY | O_CREAT | O_TRUNC, 0666);
const off_t size = lseek(fd1, 0, SEEK_END);
lseek(fd1, 0, SEEK_SET);
char str[10];
ssize_t res;
while (1) {
if (lseek(fd1, 0, SEEK_CUR) > size / 2) {
puts("拷贝结束!");
break;
}
res = read(fd1, str, sizeof(str));
write(fd2, str, res);
}
close(fd2);
close(fd1);
}
int main(int argc, const char *argv[])
{
pthread_t tid ;
if(pthread_create(&tid,NULL,fun,NULL)!=0)
{
printf("create wrong\n");
}
sleep(1);
int fd1 = open("./1.png", O_RDONLY);
int fd2 = open("./3.png", O_WRONLY);
char str[10];
ssize_t res;
const off_t size = lseek(fd1, 0, SEEK_END);
//置位光标到文件的一半处
lseek(fd1, size / 2, SEEK_SET);
lseek(fd2, size / 2, SEEK_SET);
while (1) {
res = read(fd1, str, sizeof(str));
if (res <= 0) {
puts("拷贝结束!");
break;
}
write(fd2, str, res);
}
close(fd2);
close(fd1);
return 0;
}
2.
char buf[]="1234567";
void *fun()
{
while(1){
int i=0,j=6;
while(i<j)
{
char t=buf[i];
buf[i]=buf[j];
buf[j]=t;
i++;
j--;
}
}
}
int main(int argc, const char *argv[])
{
pthread_t tid ;
if(pthread_create(&tid,NULL,fun,NULL)!=0)
{
printf("create wrong\n");
}
while(1){
if(strcmp(buf,"1234567")==0||strcmp(buf,"7654321")==0)
printf("%s\n",buf);
}
return 0;
}