国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

Linux系統下C語言編程:線程的創建和使用

2024-08-27 23:57:45
字體:
來源:轉載
供稿:網友

線程的創建是用下面的幾個函數來實現的.

  1. #include 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來表明使用缺省屬性。start_routine函數指針是線程創建成功后開始執行的函數,arg是這個函數的唯一一個參數。表明傳遞給start_routine的參數。

pthread_exit函數和exit函數類似用來退出線程.這個函數結束線程,釋放函數的資源,并在最后阻塞,直到其他線程使用pthread_join函數等待它。然后將*retval的值傳遞給**thread_return.由于這個函數釋放所以的函數資源,所以retval不能夠指向函數的局部變量。

pthread_join和wait調用一樣用來等待指定的線程。下面我們使用一個實例來解釋一下使用方法.在實踐中,我們經常要備份一些文件。下面這個程序可以實現當前目錄下的所有文件備份.備份后的后綴名為bak

  1. #include     #include     #include     #include     #include     #include     #include     #include     #include     #include     #include     #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:%sna",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!na");    exit(1);    }    for(i=0,j=0;id_name);    if(stat(filename,&filestat)==-1)    {    fprintf(stderr,"Get File Information:%sna",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:%sna",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:%sna",filename,strerror(errno    ));    continue;    }    /* 創建線程,進行文件拷貝 */    if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0)    fprintf(stderr,"Create Thread[%d] Error:%sna",i,strerror(errno));    j++;    }    byte_copy=0;    for(i=0;i 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梅河口市| 德兴市| 南丹县| 茌平县| 凤山县| 卫辉市| 巫山县| 遂川县| 视频| 芜湖县| 剑阁县| 安吉县| 林周县| 合阳县| 深州市| 常熟市| 广平县| 安多县| 铅山县| 加查县| 长白| 潜山县| 长武县| 涟水县| 江阴市| 大悟县| 子长县| 河池市| 濉溪县| 浑源县| 汽车| 桦甸市| 大同县| 高台县| 彭山县| 宁国市| 海南省| 开远市| 昭苏县| 襄垣县| 莎车县|