스 레 드 변수 pthreadt*thread 동적 할당 공간

5891 단어 pthread
스 레 드 생 성 및 사용
 
스 레 드 의 생 성 은 아래 의 몇 가지 함수 로 이 루어 집 니 다.
 

      
        #include <pthread.h>

            int pthread_create(pthread_t *thread,pthread_attr_t *attr,

            void *(*start_routine)(void *),void *arg);

            void pthread_exit(void *retval);

            int pthread_join(pthread *thread,void **thread_return);

 
pthread_create 는 스 레 드 를 만 듭 니 다.thread 는 스 레 드 를 만 드 는 ID 를 표시 하 는 데 사 용 됩 니 다.attr 는 스 레 드 를 만 들 때의 속성 을 지적 합 니 다.저 희 는 NULL 로 결 성 된 속성 을 표시 합 니 다.startroutine 함수 포인 터 는 스 레 드 생 성 성공 후 실행 되 는 함수 입 니 다.arg 는 이 함수 의 유일한 매개 변수 입 니 다.start 에 전달 되 는 것 을 나타 냅 니 다.routine 의 인자.pthreadexit 함수 와 exit 함 수 는 라인 을 종료 하 는 데 사 용 됩 니 다.이 함 수 는 라인 을 끝내 고 함수 의 자원 을 방출 하 며 다른 라인 이 pthread 를 사용 할 때 까지 마지막 에 막 습 니 다.join 함수 가 기다 리 고 있 습 니 다.그리고*retval 의 값 을**thread 에 전달 합 니 다.return.이 함수 가 함수 자원 을 방출 하기 때문에 retval 은 함수 의 부분 변 수 를 가리 킬 수 없습니다.pthreadjoin 은 wait 호출 과 마찬가지 로 지정 한 스 레 드 를 기다 리 는 데 사 용 됩 니 다.아래 에 서 는 하나의 인 스 턴 스 를 사용 하여 사용 방법 을 설명 합 니 다.실제 적 으로 파일 을 백업 해 야 합 니 다.아래 프로그램 은 현재 디 렉 터 리 에 있 는 모든 파일 백업 을 실현 할 수 있 습 니 다.백업 한 접 두 사 는 bak 입 니 다.
 

      
        #include <stdio.h>

            #include <unistd.h>

            #include <stdlib.h>

            #include <string.h>

            #include <errno.h>

            #include <pthread.h>

            #include <dirent.h>

            #include <fcntl.h>

            #include <sys/types.h>

            #include <sys/stat.h>

            #include <sys/time.h>

            #define BUFFER 512

            struct copy_file {

            int infile;

            int outfile;

            };

            void *copy(void *arg)

            {

            int infile,outfile;

            int bytes_read,bytes_write,*bytes_copy_p;

            char buffer[BUFFER],*buffer_p;

            struct copy_file *file=(struct copy_file *)arg;

            infile=file->infile;

            outfile=file->outfile;

            /*        ,            ,              */

            if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL);

            bytes_read=bytes_write=0;

            *bytes_copy_p=0;

            while((bytes_read=read(infile,buffer,BUFFER))!=0)

            {

            if((bytes_read==-1)&&(errno!=EINTR))break;

            else if(bytes_read>0)

            {

            buffer_p=buffer;

            while((bytes_write=write(outfile,buffer_p,bytes_read))!=0)

            {

            if((bytes_write==-1)&&(errno!=EINTR))break;

            else if(bytes_write==bytes_read)break;

            else if(bytes_write>0)

            {

            buffer_p+=bytes_write;

            bytes_read-=bytes_write;

            }

            }

            if(bytes_write==-1)break;

            *bytes_copy_p+=bytes_read;

            }

            }

            close(infile);

            close(outfile);

            pthread_exit(bytes_copy_p);

            }

            int main(int argc,char **argv)

            {

            pthread_t *thread;

            struct copy_file *file;

            int byte_copy,*byte_copy_p,num,i,j;

            char filename[BUFFER];

            struct dirent **namelist;

            struct stat filestat;

            /*              (    )    */

            if((num=scandir(".",&namelist,0,alphasort))<0)

            {

            fprintf(stderr,"Get File Num Error:%s
\a",strerror(errno)); exit(1); } /* , */ if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)|| ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL) ) { fprintf(stderr,"Out Of Memory!
\a"); exit(1); } for(i=0,j=0;i<num;i++) { memset(filename,'\0',BUFFER); strcpy(filename,namelist[i]->d_name); if(stat(filename,&filestat)==-1) { fprintf(stderr,"Get File Information:%s
\a",strerror(errno)); exit(1); } /* */ if(!S_ISREG(filestat.st_mode))continue; if((file[j].infile=open(filename,O_RDONLY))<0) { fprintf(stderr,"Open %s Error:%s
\a",filename,strerror(errno)); continue; } strcat(filename,".bak"); if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR)) <0) { fprintf(stderr,"Creat %s Error:%s
\a",filename,strerror(errno )); continue; } /* , */ if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0) fprintf(stderr,"Create Thread[%d] Error:%s
\a",i,strerror(errno)); j++; } byte_copy=0; for(i=0;i<j;i++) { /* */ if(pthread_join(thread[i],(void **)&byte_copy_p)!=0) fprintf(stderr,"Thread[%d] Join Error:%s
\a", i,strerror(errno)); else { if(bytes_copy_p==NULL)continue; printf("Thread[%d] Copy %d bytes
\a",i,*byte_copy_p); byte_copy+=*byte_copy_p; /* copy */ free(byte_copy_p); } } printf("Total Copy Bytes %d
\a",byte_copy); free(thread); free(file); exit(0); }

좋은 웹페이지 즐겨찾기