char buf[] = "1234567";
void* test_a(void* arg)
{
int i = 0;
for(i = 0; buf[i] != '\0'; i++)
{
printf("buf = %s\n", buf);
}
}
void* test_b(void* arg)
{
int i;
int j = strlen(buf)-1;
while(1)
{
for(i=0;i<=j;i++,j--)
{
int temp = ((char*) arg)[i];
((char*)arg)[i] = ((char*)arg)[j];
((char*)arg)[j] = temp;
}
pthread_exit(NULL);
}
return arg;
}
int main(int argc, const char *argv[])
{
pthread_t a;
pthread_create(&a, NULL, test_a, NULL);
printf("abuf = %s\n", buf);
pthread_t b;
pthread_create(&bb, NULL, test_b, (void*)buf);
pthread_join(bb,NULL);
printf("bbuf = %s\n", buf);
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
typedef struct open{
int fd;
int fd2;
off_t size;
}FUN;
void* func(void* arg){
FUN* mode=(FUN*)arg;
lseek(mode->fd,0,SEEK_SET);
lseek(mode->fd2,0,SEEK_SET);
char c;
for(int i=0;i<(mode->size)/2;i++){
read(mode->fd,&c,1);
write(mode->fd2,&c,1);
}
printf("前半部分拷贝完成\n");
pthread_exit(NULL);
return NULL;
}
int main(int argc, const char *argv[])
{
FUN mode;
mode.fd=open("./1.png",O_RDONLY);
if(mode.fd<0){
perror("open");
return -1;
}
mode.fd2=open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0664);
if(mode.fd2<0){
perror("open_fd2");
return -1;
}
mode.size=lseek(mode.fd,0,SEEK_END);
pthread_t tid;
pthread_create(&tid,NULL,func,(void*)&mode);
pthread_join(tid,NULL);
char c;
for(int i=(mode.size)/2;i<mode.size;i++){
read(mode.fd,&c,1);
write(mode.fd2,&c,1);
}
printf("全部拷贝完成\n");
return 0;
}