Linux下線程的操作

來源:互聯網
上載者:User
Linux下線程的操作
01-7-27 上午 10:39:13

介紹在Linux下線程的建立和基本的使用。Linux下的線程是一個非常複雜的問題,由於我對線程的學習不時很好,我在這裡只是簡單的介紹線程的建立和基本的使用,關於線程的進階使用(如線程的屬性,線程的互斥,線程的同步等等問題)可以參考我後面給出的資料。現在關於線程的資料在網路上可以找到許多英文資料,後面我羅列了許多連結,對線程的進階屬性感興趣的話可以參考一下。等到我對線程的瞭解比較深刻的時候,我回來完成這篇文章。如果您對線程瞭解的詳盡我也非常高興能夠由您來完善。
先介紹什麼是線程。我們編寫的程式大多數可以看成是單線程的。就是程式是按照一定的順序來執行。如果我們使用線程的話,程式就會在我們建立線成的地方分叉,變成兩個"程式"在執行。粗略的看來好象和子進程差不多的,其實不然。子進程是通過拷貝父進程的地址空間來執行的。而線程是通過共用程式碼來執行的,講的通俗一點就是線程的相同的代碼會被執行幾次。使用線程的好處是可以節省資源,由於線程是通過共用代碼的,所以沒有進程調度那麼複雜。
 
線程的建立和使用
線程的建立是用下面的幾個函數來實現的。
 
#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
 
#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:%s/n/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!/n/a");
exit(1);
}
 
for(i=0,j=0;i
{
memset(filename,'/0',BUFFER);
strcpy(filename,namelist[i]->d_name);
if(stat(filename,&filestat)==-1)
{
fprintf(stderr,"Get File Information:%s/n/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/n/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/n/a",filename,strerror(errno));
continue;
}
 
/* 建立線程,進行檔案拷貝 */
if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0)
fprintf(stderr,"Create Thread[%d] Error:%s/n/a",i,strerror(errno));
j++;
}
 
byte_copy=0;
for(i=0;i
{
/* 等待線程結束 */
if(pthread_join(thread[i],(void **)&byte_copy_p)!=0)
fprintf(stderr,"Thread[%d] Join Error:%s/n/a",
i,strerror(errno));
else
{
if(bytes_copy_p==NULL)continue;
printf("Thread[%d] Copy %d bytes/n/a",i,*byte_copy_p);
byte_copy+=*byte_copy_p;
/* 釋放我們在copy函數裡面建立的記憶體 */
free(byte_copy_p);
}
}
printf("Total Copy Bytes %d/n/a",byte_copy);
free(thread);
free(file);
exit(0);
)
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.