• <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程序設計入門--線程操作


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

            線程的創建和使用
            線程的創建是用下面的幾個函數來實現的.
            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創建一個線程,thread是用來表明創建線程的ID,attr指出線程創建時候
            的屬性,我們用NULL來表明使用缺省屬性.start_routine函數指針是線程創建成功后開始
            執行的函數,arg是這個函數的唯一一個參數.表明傳遞給start_routine的參數. pthrea
            d_exit函數和exit函數類似用來退出線程.這個函數結束線程,釋放函數的資源,并在最后
            阻塞,直到其他線程使用pthread_join函數等待它.然后將*retval的值傳遞給**thread_
            return.由于這個函數釋放所以的函數資源,所以retval不能夠指向函數的局部變量. pt
            hread_join和wait調用一樣用來等待指定的線程. 下面我們使用一個實例來解釋一下使
            用方法.在實踐中,我們經常要備份一些文件.下面這個程序可以實現當前目錄下的所有文
            件備份.備份后的后綴名為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. /* 因為線程退出時,所有的變量空間都要被釋放,所以我們只好自己分配內存了 */    
            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. /* 得到當前路徑下面所有的文件(包含目錄)的個數 */    
            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. /* 創建線程,進行文件拷貝 */    
            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. /* 等待線程結束 */    
            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函數里面創建的內存 */    
            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. }    

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

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

            <2013年4月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            導航

            統計

            公告


            Name: Galen
            QQ: 88104725

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            相冊

            My Friend

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            四虎影视久久久免费| 一本一道久久综合狠狠老| 久久精品国产99久久久| 久久99国产精品尤物| 久久免费精品视频| 久久国产免费观看精品3| 久久精品国产99国产精偷| 国内精品久久久久影院免费| 久久精品无码一区二区三区日韩| 性做久久久久久免费观看| 亚洲AV无一区二区三区久久| 久久久久夜夜夜精品国产| 国产香蕉久久精品综合网| 国产欧美一区二区久久| 欧美国产精品久久高清| 99久久人妻无码精品系列蜜桃| 国产精品无码久久四虎| 久久久久亚洲AV无码专区首JN | 国产91久久精品一区二区| 国产精品亚洲综合专区片高清久久久 | 伊人久久免费视频| 亚洲va久久久噜噜噜久久男同| 久久99精品国产麻豆宅宅| 亚洲精品乱码久久久久久| 人妻丰满?V无码久久不卡| 久久精品国产91久久综合麻豆自制| 久久天天躁狠狠躁夜夜不卡| 狠狠色伊人久久精品综合网| 狠狠色噜噜狠狠狠狠狠色综合久久| 亚洲精品tv久久久久久久久久| 久久这里只有精品首页| 婷婷久久久亚洲欧洲日产国码AV | 久久亚洲日韩看片无码| 久久久久久国产精品无码下载 | 久久久久久国产精品无码下载| a高清免费毛片久久| 一本久久a久久精品综合香蕉 | 国产精品美女久久久久网| 久久精品国产国产精品四凭| 国产一久久香蕉国产线看观看| 亚洲伊人久久大香线蕉苏妲己|