
- #include
- #include
- #include
- #include
- char buf[]="1234567";
-
- void* func(void* arg){
- int len,i=0;
- char temp;
- while(1){
- len=strlen(buf)-1;
- i=0;
- while(buf[i]!='\0'&&i
2){ - temp=buf[i];
- buf[i]=buf[len];
- buf[len]=temp;
- i++;
- len--;
- }
- }
- }
-
- int main(int argc, const char *argv[])
- {
- pthread_t tid;
- pthread_create(&tid,NULL,func,NULL);
- while(1){
- int i=strcmp(buf,"1234567");
- int j=strcmp(buf,"7654321");
- if(!i||!j){
- printf("主线程: %s\n",buf);
- }
- }
- return 0;
- }
-

- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- 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;
- //lseek(mode.fd,(mode.size)/2,SEEK_SET);
- //lseek(mode.fd2,(mode.size)/2,SEEK_SET);
- for(int i=(mode.size)/2;i
- read(mode.fd,&c,1);
- write(mode.fd2,&c,1);
- }
- printf("全部拷贝完成\n");
- return 0;
- }