Linux threading and thread control concept

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/DX_Jone/article/details/101022777

This article focuses on knowledge:

1. The concept of threads:
2. The resources shared between threads and unique:
3. Multi-threaded processing tasks of the advantages and disadvantages:
4. Thread control:

A thread concept:

1. What is a thread? ?

Pcb use in the operating system to run a program description, so pcb is a process; it is not designed as a pcb thread in Linux to control the running thread, so threads at this time is to simulate the process of realization of the pcb; next to Linux pcb simulation to achieve thread, the thread under Linux is actually a lightweight process; lightweight process because this process most common resources; more lightweight compared to conventional processes;

2. The relationship between processes and threads:
  • Process is the basic unit of resource allocation ---- resource allocation because the program is running is to give the entire thread group (processes);
  • A thread is the basic unit of CPU scheduling - because Linux pcb is the thread;
  • The same process group pcb share the same virtual address space, shared process group most of the resources;
3. Thread the works: a unique and sharing of resources between threads;

A thread is in the process entity, a process can have multiple threads, a thread must have a parent process. The thread does not have the system resources required to run only some of the data structure; it shares all of the resources owned by the process with the other threads of the parent process. You can create threads and undo the threads in order to achieve concurrent execution of the program. Generally, the thread has a ready state three basic blocking and run;

Thread the unique resources:

  • Stack area:
  • Register: Register is thread unique data, it has become the thread's context, mainly to reflect the status of the last run register, which should be part of a unique thread;
  • errno: error code unique data, we assume that an error in a thread will appear this time we errno in other threads, so we need to errno unique;
  • Signal mask character: possible logic code is not the same signal will appear in each set of thread shield;
  • Priority Dispatch: computer operating system to the task assigned priority it decided to prioritize tasks in the use of resources;
  • Thread IO: file descriptor, because open file in one thread in other threads should not be used, so our IO threads should be unique;

Threads share resources:

  • Shared virtual address space:
  • File descriptor table:
  • The current working directory:
  • User ID and group ID:
  • Signal processing:
  • Data segment and code segment:
4. thread processing tasks advantages and disadvantages:
  • The advantages of multi-threaded processing tasks:
    (1) communication between threads in addition to the way inter-process as well as global data is simpler - and therefore more convenient communication between threads;
    (2) create or destroy a thread than the cost a little less costly process will be;
    scheduling between (3) with respect to process threads are lower;

  • Multi-threaded processing tasks shortcomings:
    lack of between (1) threads access control, and some system calls or abnormal for the entire process;
    (2) a lower stability compared to the process; (a thread exits, the whole process had to retire );

Second thread control:

Linux operating system does not provide control system threads call interface; therefore bigwigs encapsulates a thread control interface library; use the library functions achieve thread creation of what we call user mode thread, the user mode thread in the kernel a lightweight process to achieve scheduling;

Thread under Linux: user mode thread + lightweight processes; user to control the scheduler via the user mode thread scheduler is actually going to turn this lightweight control process by describing the information the user mode thread to complete;

Thread control interface library functions are implemented: to create a user mode thread allows users to control, but the amount of program scheduling process is achieved through lightweight processes pcb;

1. Thread creation:

#include <pthread.h>
       int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
       void *(*start_routine) (void *), void *arg);
       

thread:用于获取线程ID----线程地址空间在整个虚拟地址空间的首地址;
attr:设置线程属性,通常置NULL;
start_routine:线程的入口函数;
arg:传递给线程函数的参数;

Returns: 0 on success; nonzero if creation failed;

Id of the concept of threads: each thread is a pcb, task_struct has a pid, but when users use the ps command to view the thread missing only one thread, which is only a process-pid
but contains LWP in the task_struct, PID , TID, which the LWP is task_strcut points pid, is our lightweight process id, PID is task_struct in tgid, is the thread group threads pid, we often say that the process id. We also threads containing thread information, there is a description of the information stored in the memory, shared memory, and so is the stack and data, TID memory is what we describe the first address information, that is our thread id (pthread_t);

2. Thread Termination:
thread termination is the thread exits in this program, which out of the way include active and passive exit
(1) voluntary withdrawal:

  • return Exit:
    But the thread storage function and the main thread is not the same, the main exit in return is the whole process, thread storage function is the exit of the thread we;
  • Exit the thread itself, who calls who quit;
void pthread_exit(void* retval)         
            
retval:线程的退出返回值;

Note: We will form a thread exit zombie thread, the main thread exits the process and will not quit, because the thread exits but also save your own exit return value;

(2) Passive Exit:

  • Cancel other threads; let other thread exit;
int pthread_cancel(pthread_t thread);  
		  
thread:要取消的线程的ID;

Note: After the thread exits the default does not automatically release resources, (save the results of their exit ye ​​thread unique address space); therefore also cause resource leakage; the main thread exits, other threads can still function properly;

3. Thread waiting:
waiting for thread to exit, exit thread returns the results to obtain the release of exit thread resources;

Creating a thread out, there is a property called joinable by default; After the thread is joinable attributes exit, does not automatically release the resources, the need to wait for the other thread to release; thread is jonable property must be waiting, or cause resource leaks;

 #include <pthread.h>
 
 int pthread_join(pthread_t thread,void **retval); 

pthread_t thread:回收的线程
void** retval:线程退出返回值

Return Value: returns 0 on success, failure to return errno;
Function: blocked waiting for a specified thread exits; get the return value retval;

4. Thread Separation:
The property joinable thread modified to detach property; if the thread is detach property, the thread exits automatically after resource recovery; and this thread does not need to be waiting, waiting is meaningless, because the thread exit return value occupied space has been recovered;


#include <pthread.h>

pthread_detach(pthread_t tid);

pthread_t thread:被设置的线程id;

After being set EINVAL thread is in state, this state is the thread can not be waiting, or have been separated from the other thread

Thread isolated applicable scene: do not care about the return value of a thread; thread separation can be implemented in any thread;

Guess you like

Origin blog.csdn.net/DX_Jone/article/details/101022777