Creation of threads under Linux

xiaoxiao2021-03-06  63

Preface: The creation of threads under Linux introduces the creation and basic use of the thread under Linux. The thread under Linux is a very complex problem. Because I have a good study of threads, I am just a simple introduction of threads. And basic use, advanced use of threads (such as thread properties, thread mutual exclusion, thread synchronization, etc.) can refer to the information given later. Now there are many English materials on the network. Later, I have listed many links. If I am interested in the advanced attributes of threads. Wait until I have a deep understanding of threads, I will come back to complete this article. If you know the thread, I am very happy. It is perfect to be perfect. Let's first introduce what is thread. Most of our programming can be seen as a single thread. It is the program to be executed in a certain order. If we use threads, the program will create a line Local bifurcation, becoming two "programs" in execution. Rough appearance seems to be similar to the sub-process, is thereafter, it is not possible. The child process is executed by copying the address space of the parent process. Thread is through the shared program code The popularity of the thread is to be executed several times. The advantage of using the thread is to save resources. Since the thread is through the shared code, there is no process schedule. Thread creation and use thread Create is implemented in the following functions. #Include

INT pthread_create (pthread_t * thread, pthread_attr_t * attr,

Void * (* st_routine) (void *), void * arg);

Void pthread_exit (void * retval);

INT pthread_join (pthread * thread, void ** thread_return);

PTHREAD_CREATE Create a thread, thread is used to indicate the ID of the creation thread. Attr point out the properties of the thread creation, we use null to indicate the use of the default property. The start_routine function pointer is a function that starts executing after the thread is successfully created, and the arg is this function. The only parameter. Indicates the parameters passed to Start_Routine. The pthread_exit function is similar to using to exit the thread. This function ends the thread, releases the resources of the function, until the other thread uses the pthread_join function Waiting it. Then * The value of retval is passed to ** thread_return. Since this function is released, the RETVAL cannot point to the local variable of the function. Pthread_join and Wait call are used to wait for the specified thread. Let's use an instance to explain how to use the method In practice, we often back up some files. The following program can implement all file backups in the current directory. The suffix name after the backup is named 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;

/ * Because the thread exits, all variable space must be released, so we have to assign memory yourself * /

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 the file? * /

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;

/ * Get all the files (including the directory) below the current path * /

IF ((Num = Scandir (".", & nameList, 0, alphasort) <0)

{

FPrintf (stderr, "get file num error:% s / n / a", strrror (errno));

Exit (1);

}

/ * Distribute space to thread, there is no need to have so much * /

IF (((thread = (pthread_t *) malloc (sizeof (pthread_t) * Num) == null) ||

((File = (Struct Copy_File *) Malloc (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", strrror (errno));

Exit (1);

}

/ * We ignore the directory * /

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, strrror (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, strrror (errno));

CONTINUE;

}

/ * Create a thread, perform file copy * /

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

{

/ * Wait for thread end * /

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;

/ * Release our memory created in the COPY function * /

Free (byte_copy_p);

}

}

Printf ("Total Copy Bytes% D / N / A", BYTE_COPY;

FREE (Thread);

Free (file);

exit (0);

}

The introduction of the thread is here, and other information about threads can check the link below.

转载请注明原文地址:https://www.9cbs.com/read-82274.html

New Post(0)