Linux threads and multithreading

After understanding the process under linux, you will want to know how the thread under linux is implemented. Process is for resource scheduling. Under linux is the basic unit of resource scheduling. Since there is already a process and multi-process concept, then Why are threads used and born?
1. Thread concept;
thread is an execution flow in a process, and it is the basic unit of CPU scheduling. It is a lightweight process
under liunx . Thread under Linux is realized by pcb, which is a dynamic description of program operation. Description, the system implements the scheduling of program running.
A process can have multiple threads to perform different task events. These threads will share most of the process’s resources, which is more lightweight than traditional PCBs (ps: If you have any questions about light weight, it will be described in detail later), so it is also called a light weight process.
For linux, we need to know that in the essence of linux, there is no special concept of setting threads. The thread that I think belongs to the scope of the process to a certain extent, but it is smaller. Although the implementation is all pcb, there is a clear difference between the two when creating pcb; mainly reflected in the difference between memory and virtual memory.
Pay attention to our use In the process of linux, we are not using system calls, but library functions encapsulated by the elders!

2. Thread uniqueness and sharing;
thread uniqueness; thread stack, register, priority, signal mask, identifier, erron;
sharing: virtual address space, signal processing method, file description table, working path

3. Analysis of the advantages and disadvantages of multi-threading and multi-process multi-tasking;
advantages:
(1). The communication between threads is more flexible, and the processing process can have communication methods. Because of the shared virtual memory, it can also communicate directly.
(2) The cost of thread creation and destruction is lower; (compared to the process, the concept of lightweight words, only need to create a struct (file descriptor pcb), pointing to the virtual memory space)
(3) The same process Switching between threads is more convenient (resource sharing)
Disadvantages
(1) Robustness is low, thread crashing will cause the process to crash, and process crashing will not be the program crashing, and the significance of maintaining threads is not great
(2) When the thread is called, some system calls will have an effect on the process; in
general, multi-execution flow multitasking will effectively improve processing efficiency, for cpu-intensive programs, the use of cpu is more fully; for io-intensive programs In other words, io operations can be initiated at the same time to reduce io blocking time and improve efficiency
. 4. Thread control;
thread control can be subdivided into: Create
an interface for terminating waiting for separation of thread control. The operating system does not provide it directly, and the thread interface is encapsulated by the boss Library Functions.
Create:
`int pthread_create(pthread_t
tid,pthread_attr_t attr)`;
void
( thread_routine)(void arg), void* arg ;;
Each thread corresponds to a pcb lightweight process in the kernel;
pcb->pid - Lightweight process ID-LWP;
pcb->tgid-thread group ID, which is equal to the main thread ID by default;
we need to pay special attention that tid is an address that points to resources such as the thread user mode description stack;

Thread termination: the way to exit the thread-how to exit the thread
1. Thread entry function executes return, (for the main control thread, return will exit the entire process), the thread entry function is executed, the thread will exit
2. Use library functions:
pthread_exit void (void retval)
retval: exit thread as the return value of
this library function can be anywhere, there are only thread calls, it will exit
3. pthread_cancel library function int (pthread_t tid);
tid: a specified thread ID ; The return value is PTHREAD_CANCELED to
cancel a thread, pass in tid to exit this thread; only when all threads exit, the process will exit, the
main thread exit will not cause the process to exit (process exit means the release of resources)
thread Waiting:
By default, resources will not be reclaimed after a thread exits. It needs to wait for other threads to be released, get the return value of the exited thread, and release resources.
int pthread_jion(pthread_t tid,void **retval)Blocking the interface
Of course, it does not mean that the thread has to be waited
in the thread Among the attributes of, there is a separate attribute. The default value is -joinable. A thread in this state will not automatically release resources after exiting. It needs to be waited.
This also gives us inspiration. In actual use, we need to set all Created thread attributes

Separation of threads:
Set the detach attribute of the thread to the detach attribute, and the resource will be automatically released after the thread with the detach attribute exits, without waiting.
Note: Only when the program does not care about the return value of the thread, and does not want to wait for the thread to exit, will it detach Thread
safety: multiple threads are safe to access critical resources

Realization of thread safety: synchronization and mutual exclusion
Synchronization: make multiple threads follow a certain rule sequence to achieve reasonable access to critical resources.
Mutual exclusion: one thread can access critical resources at the same time to achieve access security

The realization of mutual exclusion: the realization of mutual exclusion lock
synchronization: semaphore condition variable

Guess you like

Origin blog.51cto.com/14632688/2555065