• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Linux程序設(shè)計入門--線程操作


            前言:Linux下線程的創(chuàng)建
            介紹在Linux下線程的創(chuàng)建和基本的使用. Linux下的線程是一個非常復(fù)雜的問題,由
            于我對線程的學(xué)習(xí)不時很好,我在這里只是簡單的介紹線程的創(chuàng)建和基本的使用,關(guān)于線
            程的高級使用(如線程的屬性,線程的互斥,線程的同步等等問題)可以參考我后面給出的
            資料. 現(xiàn)在關(guān)于線程的資料在網(wǎng)絡(luò)上可以找到許多英文資料,后面我羅列了許多鏈接,對
            線程的高級屬性感興趣的話可以參考一下. 等到我對線程的了解比較深刻的時候,我回來
            完成這篇文章.如果您對線程了解的詳盡我也非常高興能夠由您來完善.
            先介紹什么是線程.我們編寫的程序大多數(shù)可以看成是單線程的.就是程序是按照一定的
            順序來執(zhí)行.如果我們使用線程的話,程序就會在我們創(chuàng)建線成的地方分叉,變成兩個"程
            序"在執(zhí)行.粗略的看來好象和子進(jìn)程差不多的,其實不然.子進(jìn)程是通過拷貝父進(jìn)程的地
            址空間來執(zhí)行的.而線程是通過共享程序代碼來執(zhí)行的,講的通俗一點就是線程的相同的
            代碼會被執(zhí)行幾次.使用線程的好處是可以節(jié)省資源,由于線程是通過共享代碼的,所以沒
            有進(jìn)程調(diào)度那么復(fù)雜.

            線程的創(chuàng)建和使用
            線程的創(chuàng)建是用下面的幾個函數(shù)來實現(xiàn)的.
            1. #include <pthread.h>    
            2. int pthread_create(pthread_t *thread,pthread_attr_t *attr,    
            3. void *(*start_routine)(void *),void *arg);    
            4. void pthread_exit(void *retval);    
            5. int pthread_join(pthread *thread,void **thread_return);    

            pthread_create創(chuàng)建一個線程,thread是用來表明創(chuàng)建線程的ID,attr指出線程創(chuàng)建時候
            的屬性,我們用NULL來表明使用缺省屬性.start_routine函數(shù)指針是線程創(chuàng)建成功后開始
            執(zhí)行的函數(shù),arg是這個函數(shù)的唯一一個參數(shù).表明傳遞給start_routine的參數(shù). pthrea
            d_exit函數(shù)和exit函數(shù)類似用來退出線程.這個函數(shù)結(jié)束線程,釋放函數(shù)的資源,并在最后
            阻塞,直到其他線程使用pthread_join函數(shù)等待它.然后將*retval的值傳遞給**thread_
            return.由于這個函數(shù)釋放所以的函數(shù)資源,所以retval不能夠指向函數(shù)的局部變量. pt
            hread_join和wait調(diào)用一樣用來等待指定的線程. 下面我們使用一個實例來解釋一下使
            用方法.在實踐中,我們經(jīng)常要備份一些文件.下面這個程序可以實現(xiàn)當(dāng)前目錄下的所有文
            件備份.備份后的后綴名為bak
            1. #include <stdio.h>    
            2. #include <unistd.h>    
            3. #include <stdlib.h>    
            4. #include <string.h>    
            5. #include <errno.h>    
            6. #include <pthread.h>    
            7. #include <dirent.h>    
            8. #include <fcntl.h>    
            9. #include <sys/types.h>    
            10. #include <sys/stat.h>    
            11. #include <sys/time.h>    
            12. #define BUFFER 512    
            13. struct copy_file {    
            14. int infile;    
            15. int outfile;    
            16. };    
            17. void *copy(void *arg)    
            18. {    
            19. int infile,outfile;    
            20. int bytes_read,bytes_write,*bytes_copy_p;    
            21. char buffer[BUFFER],*buffer_p;    
            22. struct copy_file *file=(struct copy_file *)arg;    
            23. infile=file->infile;    
            24. outfile=file->outfile;    
            25. /* 因為線程退出時,所有的變量空間都要被釋放,所以我們只好自己分配內(nèi)存了 */    
            26. if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL);    
            27. bytes_read=bytes_write=0;    
            28. *bytes_copy_p=0;    
            29. /* 還記得怎么拷貝文件嗎 */    
            30. while((bytes_read=read(infile,buffer,BUFFER))!=0)    
            31. {    
            32. if((bytes_read==-1)&&(errno!=EINTR))break;    
            33. else if(bytes_read>0)    
            34. {    
            35. buffer_p=buffer;    
            36. while((bytes_write=write(outfile,buffer_p,bytes_read))!=0)    
            37. {    
            38. if((bytes_write==-1)&&(errno!=EINTR))break;    
            39. else if(bytes_write==bytes_read)break;    
            40. else if(bytes_write>0)    
            41. {    
            42. buffer_p+=bytes_write;    
            43. bytes_read-=bytes_write;    
            44. }    
            45. }    
            46. if(bytes_write==-1)break;    
            47. *bytes_copy_p+=bytes_read;    
            48. }    
            49. }    
            50. close(infile);    
            51. close(outfile);    
            52. pthread_exit(bytes_copy_p);    
            53. }    
            54. int main(int argc,char **argv)    
            55. {    
            56. pthread_t *thread;    
            57. struct copy_file *file;    
            58. int byte_copy,*byte_copy_p,num,i,j;    
            59. char filename[BUFFER];    
            60. struct dirent **namelist;    
            61. struct stat filestat;    
            62. /* 得到當(dāng)前路徑下面所有的文件(包含目錄)的個數(shù) */    
            63. if((num=scandir(".",&namelist,0,alphasort))<0)    
            64. {    
            65. fprintf(stderr,"Get File Num Error:%s\n\a",strerror(errno));    
            66. exit(1);    
            67. }    
            68. /* 給線程分配空間,其實沒有必要這么多的 */    
            69. if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)||    
            70. ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL)    
            71. )    
            72. {    
            73. fprintf(stderr,"Out Of Memory!\n\a");    
            74. exit(1);    
            75. }    
            76.   
            77. for(i=0,j=0;i<num;i++)    
            78. {    
            79. memset(filename,'\0',BUFFER);    
            80. strcpy(filename,namelist[i]->d_name);    
            81. if(stat(filename,&filestat)==-1)    
            82. {    
            83. fprintf(stderr,"Get File Information:%s\n\a",strerror(errno));    
            84. exit(1);    
            85. }    
            86. /* 我們忽略目錄 */    
            87. if(!S_ISREG(filestat.st_mode))continue;    
            88. if((file[j].infile=open(filename,O_RDONLY))<0)    
            89. {    
            90. fprintf(stderr,"Open %s Error:%s\n\a",filename,strerror(errno));    
            91. continue;    
            92. }    
            93. strcat(filename,".bak");    
            94. if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))    
            95. <0)    
            96. {    
            97. fprintf(stderr,"Creat %s Error:%s\n\a",filename,strerror(errno    
            98. ));    
            99. continue;    
            100. }    
            101. /* 創(chuàng)建線程,進(jìn)行文件拷貝 */    
            102. if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0)    
            103. fprintf(stderr,"Create Thread[%d] Error:%s\n\a",i,strerror(errno));    
            104. j++;    
            105. }    
            106. byte_copy=0;    
            107. for(i=0;i<j;i++)    
            108. {    
            109. /* 等待線程結(jié)束 */    
            110. if(pthread_join(thread[i],(void **)&byte_copy_p)!=0)    
            111. fprintf(stderr,"Thread[%d] Join Error:%s\n\a",    
            112. i,strerror(errno));    
            113. else    
            114. {    
            115. if(bytes_copy_p==NULL)continue;    
            116. printf("Thread[%d] Copy %d bytes\n\a",i,*byte_copy_p);    
            117. byte_copy+=*byte_copy_p;    
            118. /* 釋放我們在copy函數(shù)里面創(chuàng)建的內(nèi)存 */    
            119. free(byte_copy_p);    
            120. }    
            121. }    
            122. printf("Total Copy Bytes %d\n\a",byte_copy);    
            123. free(thread);    
            124. free(file);    
            125. exit(0);    
            126. }    

            線程的介紹就到這里了,關(guān)于線程的其他資料可以查看下面這寫鏈接.
            Getting Started With POSIX Threads
            The LinuxThreads library

            posted on 2008-04-16 09:10 RedLight 閱讀(358) 評論(0)  編輯 收藏 引用 所屬分類: Linux(C++開發(fā)) (rss)

            <2008年9月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導(dǎo)航

            統(tǒng)計

            公告


            Name: Galen
            QQ: 88104725

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            相冊

            My Friend

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            WWW婷婷AV久久久影片| 中文字幕久久波多野结衣av| 99久久这里只精品国产免费| 一本综合久久国产二区| 中文字幕精品久久久久人妻| 久久久久亚洲AV成人网| 精品无码久久久久久午夜| 狠狠精品久久久无码中文字幕| 人人妻久久人人澡人人爽人人精品 | 狠狠色综合网站久久久久久久| 国产精品午夜久久| 少妇熟女久久综合网色欲| 久久亚洲精品国产亚洲老地址| 麻豆成人久久精品二区三区免费| 亚洲国产精品久久久久婷婷软件| 久久久久综合中文字幕| 日韩精品无码久久一区二区三| 久久久久人妻精品一区| 亚洲欧洲精品成人久久奇米网| 成人综合久久精品色婷婷| 精品国产乱码久久久久软件| 中文字幕久久波多野结衣av| 久久精品亚洲一区二区三区浴池| 久久精品国产99国产精偷| 久久久久久久波多野结衣高潮| 国产成人精品久久亚洲| 欧美日韩中文字幕久久久不卡| 久久亚洲AV成人无码| 久久ww精品w免费人成| 一级做a爰片久久毛片人呢| 亚洲欧美成人久久综合中文网| 中文字幕日本人妻久久久免费 | 无码日韩人妻精品久久蜜桃| 欧美久久久久久午夜精品| 伊人久久大香线蕉综合5g| 性欧美丰满熟妇XXXX性久久久 | 国内精品久久久久影院薰衣草 | 性欧美丰满熟妇XXXX性久久久 | 欧美色综合久久久久久| 久久精品国产99久久无毒不卡 | 久久久久青草线蕉综合超碰|