Linux Line Program control system

Source: Internet
Author: User

Resources shared by each thread:

1, File descriptor table.

2, the processing of each signal (sig_ign, SIG_DFL or custom signal processing function)

3, the current working directory.

4. User ID and group ID.

Separate resources for each thread:

1, thread ID.

2. Context, including the values of various registers, program calculators and stack pointers.

3, Stack space.

4, errno variable.

5, the signal volume shielding word.

6, scheduling priority.

If either of the threads calls exit or _exit, all threads of the entire thread are terminated.

To terminate a thread:

1, from the thread function return, this method does not apply to the main thread, from the main function return is equivalent to call exit.

2. One thread can call Pthread_cancel to terminate another thread in the same process.

3. Threads can call Pthread_exit to terminate themselves.

Thread Waits:

1, if the thread thread is returned by return, Value_ptr points to the cell where the thread function return value is stored.

2. If the thread thread is called by another thread, the pthread_cancel exception terminates, and value_ptr points to the cell that holds the constant pthread_canceled.

3. If thread threads are terminated by their own invocation of Pthread_exit, Value_ptr points to a unit that holds the parameters passed to Pthread_exit. If you are not interested in the terminating state of thread threads, you can pass NULL to the VALUE_PTR parameter.

Thread Separation:

At any point in time, threads are either associative or detached. A binding thread can be recalled by other threads and killed by its resources. Its memory resource is not freed until it is reclaimed by another thread. Instead, a disconnected thread cannot be recycled or killed by other threads, and its memory resources are automatically freed by the system when it terminates.

By default, threads are created to be combined. To avoid memory leaks, each of the bonded threads should be shown to be recycled, called pthread_join, or separated by calling the Pthread_detach function.

If a binding thread ends up running but not being join, its state is similar to a zombie process in progress, that is, some of the resources are not recycled, so the creator thread should call Pthrea_join to wait for the thread to run to the end and get the thread's exit code to reclaim its resources.

Since the call to Pthread_join, if the thread does not end up running, the caller will be blocked, in some cases we do not want this. For example, in a Web server, when the main thread creates a sub-thread for each new connection request, the main thread does not want to block because it calls Pthread_join (because the incoming connection request continues to be processed), then the code can be added to the child thread Pthread_ Detach (Pathread_self () or parent thread calls Pthread_detach (thread_id) (non-blocking, can be returned immediately)

This sets the state of the child thread to detached, so that all resources are freed automatically when the thread finishes running.


This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1826996

Linux Line Program control system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.