Getting Started with Linux Programming - Threading

  

Computer shop news first introduces what is thread. Most of the programs we write can be regarded as single-threaded. The program is executed in a certain order. If we use threads, the program will create a line in our line. The place forks and turns into two "programs" in execution. Roughly it seems to be similar to the child process, but it is not. The child process is executed by copying the address space of the parent process. The thread is shared by The program code to execute, the popular point is that the same code of the thread will be executed several times. The advantage of using the thread is that it can save resources, because the thread is shared by the code, so no process scheduling is so complicated. Thread creation and The use of thread creation is implemented by the following functions. #include <pthread.h> 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 creates a thread, thread is used to indicate the ID of the created thread, attr Point out the attributes when the thread is created, we use NULL to indicate the use of the default attribute. The start_routine function pointer is the function that starts executing after the thread is created successfully, arg is the only parameter of this function. It indicates the parameter passed to start_routine. pthrea d_exit function and The exit function is similar to exiting the thread. This function ends the thread, releases the function's resources, and blocks at the end until other threads wait for it using the pthread_join function. Then passes the value of *retval to **thread_return. Since this function is released, Function resources, so retval can't point to a function's local variable. pthread_join is used to wait for the specified thread just like the wait call. Let's use an example to explain the usage. In practice, we often have to back up some files. All files in the current directory can be backed up. The backup suffix is ​​named bak #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> # Include <errno.h> #include <pthread.h> #include <dirent.h> #include <fcn Tl.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #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; /* Since the thread exits, all variable space is released, so we have to allocate memory ourselves*/if((bytes_copy_p=(int *)malloc(sizeof(int))) ==NULL) pthread_exit(NULL); bytes_read=bytes_write=0; *bytes_copy_p=0; /* Remember how to copy files*/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_wr Ie; } } 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; /* Get all the files (including directories) under the current path Number*/if((num=scandir(".",&namelist,0,alphasort))<0) { fprintf(stderr,"Get File Num Error:%s\ \\a", Strerror(errno)); exit(1)

Copyright © Windows knowledge All Rights Reserved