Pages

Sabtu, 12 November 2011

Linux下线程的操作 Thread under Linux operating

线程的创建和使用线程的创建是用下面的几个函数来实现的. Thread creation and thread creation is to use the following several functions to achieve.
#include # Include
int pthread_create(pthread_t *thread,pthread_attr_t *attr, int pthread_create (pthread_t * thread, pthread_attr_t * attr,
void *(*start_routine)(void *),void *arg); void * (* start_routine) (void *), void * arg);
void pthread_exit(void *retval); void pthread_exit (void * retval);
int pthread_join(pthread *thread,void **thread_return); int pthread_join (pthread * thread, void ** thread_return);
pthread_create创建一个线程,thread是用来表明创建线程的ID,attr指出线程创建时候的属性,我们用NULL pthread_create creates a thread, thread is used to create a thread that's ID, attr attributes that a thread is created when we use NULL
来表明使用缺省属性.start_routine函数指针是线程创建成功后开始执行的函数,arg是这个函数的唯一一个参数.表明传递给 start_routine的参数. pthread_exit函数和exit函数类似用来退出线程.这个函数结束线程,释放函数的资源,并在最后阻塞,直到其他线程使用 pthread_join函数等待它.然后将*retval的值传递给**thread_return.由于这个函数释放所以的函数资源,所以 retval不能够指向函数的局部变量. To show that the use of default properties. Start_routine function pointer is a thread is created after successful execution of the function, arg is the only parameter of this function. That the parameters passed to the start_routine. Pthread_exit similar functions, and function exit to exit the thread and this function end of the thread, the release function of resources, and in the final block until another thread using pthread_join function to wait for it, then the value will be passed to * retval ** thread_return. So because of this function is a function of the release of resources, it can not point to a function retval local variables.
pthread_join和wait调用一样用来等待指定的线程. 下面我们使用一个实例来解释一下使用方法.在实践中 pthread_join to wait and wait call specified the same thread. Here we use an example to explain the methods used in practice
,我们经常要备份一些文件.下面这个程序可以实现当前目录下的所有文件备份.备份后的后缀名为bak We always want to back up some files. The following procedure can be achieved under the current directory of all files backed up. Bak backup named after the suffix
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#include # Include
#define BUFFER 512 # Define BUFFER 512
struct copy_file { struct copy_file {
int infile; int infile;
int outfile; int outfile;
}; };
void *copy(void *arg) void * copy (void * arg)
{ {
int infile,outfile; int infile, outfile;
int bytes_read,bytes_write,*bytes_copy_p; int bytes_read, bytes_write, * bytes_copy_p;
char buffer[BUFFER],*buffer_p; char buffer [BUFFER], * buffer_p;
struct copy_file *file=(struct copy_file *)arg; struct copy_file * file = (struct copy_file *) arg;
infile=file->infile; infile = file-> infile;
outfile=file->outfile; outfile = file-> outfile;
/* 因为线程退出时,所有的变量空间都要被释放,所以我们只好自己分配内存了*/ / * Because the thread exits, all variables must be free space, so we had to allocate their own memory of the * /
if((bytes_copy_p=(int *)malloc(sizeof(int)))==NULL) pthread_exit(NULL); if ((bytes_copy_p = (int *) malloc (sizeof (int )))== NULL) pthread_exit (NULL);
bytes_read=bytes_write=0; bytes_read = bytes_write = 0;
*bytes_copy_p=0; * Bytes_copy_p = 0;
/* 还记得怎么拷贝文件吗*/ / * Remember how you copy the file * /
while((bytes_read=read(infile,buffer,BUFFER))!=0) while ((bytes_read = read (infile, buffer, BUFFER))! = 0)
{ {
if((bytes_read==-1)&&(errno!=EINTR))break; if ((bytes_read ==- 1) & & (errno! = EINTR)) break;
else if(bytes_read>0) else if (bytes_read> 0)
{ {
buffer_p=buffer; buffer_p = buffer;
while((bytes_write=write(outfile,buffer_p,bytes_read))!=0) while ((bytes_write = write (outfile, buffer_p, bytes_read))! = 0)
{ {
if((bytes_write==-1)&&(errno!=EINTR))break; if ((bytes_write ==- 1) & & (errno! = EINTR)) break;
else if(bytes_write==bytes_read)break; else if (bytes_write == bytes_read) break;
else if(bytes_write>0) else if (bytes_write> 0)
{ {
buffer_p+=bytes_write; buffer_p + = bytes_write;
bytes_read-=bytes_write; bytes_read-= bytes_write;
} }
} }
if(bytes_write==-1)break; if (bytes_write ==- 1) break;
*bytes_copy_p+=bytes_read; * Bytes_copy_p + = bytes_read;
} }
} }
close(infile); close (infile);
close(outfile); close (outfile);
pthread_exit(bytes_copy_p); pthread_exit (bytes_copy_p);
} }
int main(int argc,char **argv) int main (int argc, char ** argv)
{ {
pthread_t *thread; pthread_t * thread;
struct copy_file *file; struct copy_file * file;
int byte_copy,*byte_copy_p,num,i,j; int byte_copy, * byte_copy_p, num, i, j;
char filename[BUFFER]; char filename [BUFFER];
struct dirent **namelist; struct dirent ** namelist;
struct stat filestat; struct stat filestat;
/* 得到当前路径下面所有的文件(包含目录)的个数*/ / * Get the current path of all of the following file (including directory) number * /
if((num=scandir(".",&namelist,0,alphasort))<0) if ((num = scandir (".",& namelist, 0, alphasort)) <0) { { fprintf(stderr,"Get File Num Error:%s\n\a",strerror(errno)); fprintf (stderr, "Get File Num Error:% s \ n \ a", strerror (errno)); exit(1); exit (1); } } /* 给线程分配空间,其实没有必要这么多的*/ / * Allocate space to the thread, it did not need so many * / if(((thread=(pthread_t *)malloc(sizeof(pthread_t)*num))==NULL)|| if (((thread = (pthread_t *) malloc (sizeof (pthread_t) * num)) == NULL) | | ((file=(struct copy_file *)malloc(sizeof(struct copy_file)*num))==NULL)) ((File = (struct copy_file *) malloc (sizeof (struct copy_file) * num)) == NULL)) { { fprintf(stderr,"Out Of Memory!\n\a"); fprintf (stderr, "Out Of Memory! \ n \ a"); exit(1); exit (1); } } for(i=0,j=0;i { for (i = 0, j = 0; i { memset(filename,'\0',BUFFER); memset (filename, '\ 0', BUFFER); strcpy(filename,namelist[i]->d_name); strcpy (filename, namelist [i] -> d_name);
if(stat(filename,&filestat)==-1) if (stat (filename, & filestat) ==- 1)
{ {
fprintf(stderr,"Get File Information:%s\n\a",strerror(errno)); fprintf (stderr, "Get File Information:% s \ n \ a", strerror (errno));
exit(1); exit (1);
} }
/* 我们忽略目录*/ / * We ignore the directory * /
if(!S_ISREG(filestat.st_mode))continue; if (! S_ISREG (filestat.st_mode)) continue;
if((file[j].infile=open(filename,O_RDONLY))<0) if ((file [j]. infile = open (filename, O_RDONLY)) <0)
{ {
fprintf(stderr,"Open %s Error:%s\n\a",filename,strerror(errno)); fprintf (stderr, "Open% s Error:% s \ n \ a", filename, strerror (errno));
continue; continue;
} }
strcat(filename,".bak"); strcat (filename, ". bak");
if((file[j].outfile=open(filename,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))<0) 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)); fprintf (stderr, "Creat% s Error:% s \ n \ a", filename, strerror (errno));
continue; continue;
} }
/* 创建线程,进行文件拷贝*/ / * Create the thread, the file copy * /
if(pthread_create(&thread[j],NULL,copy,(void *)&file[j])!=0) if (pthread_create (& thread [j], NULL, copy, (void *) & file [j])! = 0)
fprintf(stderr,"Create Thread[%d] Error:%s\n\a",i,strerror(errno)); fprintf (stderr, "Create Thread [% d] Error:% s \ n \ a", i, strerror (errno));
j++; j + +;
} }
byte_copy=0; byte_copy = 0;
for(i=0;i { for (i = 0; i {
/* 等待线程结束*/ / * Wait for threads to finish * /
if(pthread_join(thread[i],(void **)&byte_copy_p)!=0) if (pthread_join (thread [i], (void **) & byte_copy_p)! = 0)
fprintf(stderr,"Thread[%d] Join Error:%s\n\a", fprintf (stderr, "Thread [% d] Join Error:% s \ n \ a",
i,strerror(errno)); i, strerror (errno));
else else
{ {
if(bytes_copy_p==NULL)continue; if (bytes_copy_p == NULL) continue;
printf("Thread[%d] Copy %d bytes\n\a",i,*byte_copy_p); printf ("Thread [% d] Copy% d bytes \ n \ a", i, * byte_copy_p);
byte_copy+=*byte_copy_p; byte_copy + =* byte_copy_p;
/* 释放我们在copy函数里面创建的内存*/ / * Release the copy function, which we created in the memory * /
free(byte_copy_p); free (byte_copy_p);
} }
} }
printf("Total Copy Bytes %d\n\a",byte_copy); printf ("Total Copy Bytes% d \ n \ a", byte_copy);
free(thread); free (thread);
free(file); free (file);
exit(0); exit (0);
} }

Tidak ada komentar:

Posting Komentar