Pthread end of Thread Exit | A large number of threads created

Source: Internet
Author: User
Tags posix signal handler
Today Test yourself write multithreaded program, according to the actual situation, should create more than 3,000 lines Cheng Yes, but, when created to 378, the error came, Pthread_create () failed, just started to think this is an exception, so, restart, and even run to another computer test.          PS: Please forgive my ignorance, thank you ....      Later, the problem is no longer here, I search data from the Internet, the original is limited, so, to find a better blog, deliberately turned here to fill their ignorance on this aspect. PS: This has a lot of knowledge, because is studying, so I am not very clear, if see, please correct me, thank you ...
Pthread end of Thread Exit | A large number of threads created

Three ways for a thread to terminate normally:

1. The thread returns from the boot routine, and the return value is the exit code of the thread;

2. The thread calls the Pthread_exit function;

3. Threads can be canceled by other threads in the same process.

**************************************************************

Reproduced from: http://hi.baidu.com/ganss/blog/item/ff7799f97a87de58242df24c.html

1 Definition of thread cancellation

In general, a thread terminates automatically when its principal function exits, but it can also be forced to terminate because it receives a termination (cancel) request from another thread.

2 Semantics for thread cancellation

1. The method of threading cancellation is to Cheng the cancel signal to the target line, but how to process the cancel signal is determined by the target thread itself, either by ignoring (when the cancellation is forbidden), or by terminating immediately (when in the point of cancellation or asynchronous mode), or by continuing to Cancelation-point (cancel point , which is described below), in summary by different cancelation states.

2. The default processing that a thread receives a cancel signal (that is, the default state of the Pthread_create () to create the thread) is to continue running to the cancellation point to process (exit), or to exit directly in an asynchronous manner. The exit operation of a thread handling a cancel request is equivalent to Pthread_exit (pthread_canceled). Of course, a thread can refuse to process a CANCEL request by setting it to pthread_cancel_disable, which will be mentioned later.

3. The cancellation of a thread has nothing to do with how the thread works (joinable or detached).

3 Cancellation Point

According to the POSIX standard, Pthread_join (), Pthread_testcancel (), pthread_cond_wait (), pthread_cond_timedwait (), sem_wait (), sigwait ( Functions, such as read (), write (), and so on, can cause blocking system calls to be cancelation-point, while other pthread functions do not cause cancelation actions. But Pthread_cancel's hand album claims that because the Linuxthread library is not well combined with the C library, the C library function is not cancelation-point at the moment, but the cancel signal exits the thread from the blocked system call and resets the EINTR error code , you can call Pthread_testcancel () before and after a system call that needs to be cancelation-point to achieve the desired goal of the POSIX standard, which is the following code snippet:
Pthread_testcancel ();
Retcode = Read (fd, buffer,length);
Pthread_testcancel ();

The validity of the thread ID must be determined before use. That is to judge and guarantee: THRD!= 0 Otherwise there may be a "segment error" exception.

4 Program Design Considerations

1. If the thread is in an infinite loop and the loop body does not have the necessary path to the cancellation point, the thread cannot be terminated by the cancellation request of an external thread. Therefore, the Pthread_testcancel () call should be added to the necessary path of such a loop body.

2. When Pthread_cancel () returns, the thread may not have been canceled, the request might simply be sent to the target thread, and the target thread does not currently reach the cancellation point, and if you want to know when the thread is aborted, you need to call Pthread_join () after canceling it. One exception is that when a thread is detach, it cannot be handled like this:
(a) returns EINVAL when an already detached thread is join;
b If the thread is set to detached after the join, detach will not work.
Therefore, if you know that a thread might run in a separate way, you do not need to call Pthread_join () after Pthread_cancel ().

5 The Pthread function associated with the thread cancellation

int Pthread_cancel (pthread_t thread)
Sends an abort signal to the thread thread and returns 0 if successful, otherwise it is not a 0 value. Sending success does not mean that thread will terminate.

int pthread_setcancelstate (int state, int *oldstate)
Set the response of this thread to the CANCEL signal, state has two values: pthread_cancel_enable (default) and Pthread_cancel_disable, respectively, the signal is set to cancled state and the cancel signal is ignored, and the old_state is deposited in the original cancel state if it is not null for recovery.

int pthread_setcanceltype (int type, int *oldtype)
Sets the timing for this thread to cancel the action, and the type has two values: Pthread_cancel_deffered and pthread_cancel_asychronous, which are valid only if the cancel state is enabled. Each indicates that the signal continues to run to the next cancellation point and then exits and immediately performs the Cancel action (exit), and oldtype the canceled action type value if it is not null.

void Pthread_testcancel (void)
Check whether this thread is in the Canceld state, if it is, cancel the action, or return directly.

6 Pthread function to detect whether a thread is still alive

int Pthread_kill (pthread_t thread, int sig)
Sends a SIG signal to the thread that specifies the ID, and if no signal processing is made within the thread's code, the entire process is affected by the signal default behavior. In other words, if you send a sigquit to a thread, but the thread does not implement the signal handler, the entire process exits.
Pthread_kill (ThreadID, SIGKILL) Also, he would kill the whole process.
If you want to get the right behavior, you need to implement signal (Sigkill,sig_handler) within the thread.
So, if the int sig parameter is not 0, then it must be clear exactly what to do, and must implement the thread signal processing function, otherwise, it will affect the entire process.
So, if the int sig parameter is 0, this is a retention signal, and one effect is to determine if the thread is still alive.
Let's take a look at the return value of Pthread_kill:
The thread is still alive: 0
Thread no longer exists: Esrch
Signal is illegal: einval

***************************************************************************

First look at the following procedure:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void* func (void *)
{
Pthread_setcancelstate (pthread_cancel_enable, NULL); Allow Exit threads
Pthread_setcanceltype (pthread_cancel_asynchronous, NULL); Set to cancel immediately
while (1)
{
Operation
}
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t THRD;
pthread_attr_t attr;
Pthread_attr_init (&AMP;ATTR);
Pthread_attr_setdetachstate (&attr, pthread_create_detached);
if (Pthread_create (&AMP;THRD, &attr, Func, NULL))
{
Perror ("Pthread_create error");
Exit (Exit_failure);
}
if (!pthread_cancel (THRD))
{
printf ("Pthread_cancel ok\n");
}
Sleep (10);
return 0;
}
Does the above program not remove child threads from why?

-------

Because you didn't meet or set a cancellation point that caused the cancelation action,

You can call the Pthread_join function to get the cancellation point after you call the Pthread_cancel function to cause the thread to exit.

***************************************************************************

Look at the following procedure:

Pthread_setcancelstate (pthread_cancel_enable, NULL);
Pthread_testcancel ();/*the thread can be killed only here*/
Pthread_setcancelstate (pthread_cancel_disable, NULL);

This is the code extracted from the code, what happens when the thread can be killed.
Because this code is in the loop of the thread, why is it not killed each time the execution comes to this section?

-------

The simple understanding is that two threads T1 and T2, and if T1 sends a cancel signal to T2, T2 exits at the cancellation point by default. The cancellation point is a fixed place, as long as Pthread_join (), Pthread_testcancel (), pthread_cond_wait (), pthread_cond_timedwait (), sem_wait (), Functions such as sigwait () and read (), write (), and so on, can cause blocking system calls that are cancelation-point.

Suppose T2 is in a loop, there is no cancellation point in this loop, so what to do. There is no way to get out of the cancel signal. Then use Pthread_testcancel () to create a cancellation point, if there is a cancel signal to quit, do not continue to run.

Then pthread_setcancelstate better understand, is to set the state of T2, pthread_cancel_enable is normal processing CANCEL signal, PTHREAD_CANCEL_ Disable is simply ignoring the cancel signal.

So why is it not killed every time the loop is executed into this section?
The killing here means that another thread sends the cancel signal to him, and maybe it hasn't been sent here, so it hasn't been killed.

====================================================================================

When creating a large number of threads using pthread_create, creating a thread fails, Resource temporarily unavailable solution

Reproduced from: http://blog.csdn.net/lifuxianoa/article/details/6796813 http://blog.csdn.net/lifuxianoa/article/details/6797104

A large number of threads were created today using Pthread_create in a test program, but if the number of threads increases to 400, a thread creation failure occurs and the perror prints out the wrong reason: Resource temporarily unavailable.

Checked on the Internet, for the following reasons, each thread in the Linux system has a separate stack space, and my system calls ulimit-a see the following results:

Ulimit-a
Core file size (blocks,-c) 0
Data seg Size (Kbytes,-D) Unlimited
Scheduling Priority (-e) 20
File size (blocks,-f) Unlimited
Pending Signals (I.) 16382
Max locked Memory (Kbytes, L) 64
Max memory Size (Kbytes, M) Unlimited
Open files (-N) 1024
Pipe Size (bytes, p) 8
POSIX message queues (bytes,-Q) 819200
Real-time priority (-R) 0
Stack size (Kbytes,-s) 8192
CPU time (seconds,-t) unlimited
MAX User Processes (-u) Unlimited
Virtual Memory (Kbytes,-V) Unlimited
File locks (-X) Unlimited

You can see that the stack size is 8M, 400 threads require 8*400=3200m, and virtual memory is not enough.

There are two ways to solve this problem:

1. Using the ulimit-s 1024*1024 command, the thread stack size is temporarily set to 1 m, which is tested to create 2000 threads at the same time.

2. Use pthread_attr_setstacksize to change the thread stack size in the program.

====================================================================================
Processes and Threads

Reproduced from: http://blog.sina.com.cn/s/blog_68f262210100j61q.html

A popular explanation

A system running a lot of processes, can be likened to a road with a lot of carriages

Different processes can be understood as different carriages

And the same carriage can have a lot of horses to pull----these horses are threads.

Suppose the width of the road is just right through a carriage

The road can be considered a critical resource.

Then the wagon becomes the smallest unit (process) for allocating resources

And the same carriage was driven by many horses (threads)----that is, the smallest operating unit

Number of horses per carriage >=1

So the number of horses = 1 when there is no strict boundary between process and thread, there is only one degree of conceptual differentiation

When horses are >1, processes and threads can be strictly differentiated

Professional explanations:

In short, a program has at least one process, and a process has at least one thread.

The thread partitioning scale is smaller than the process, which makes the multithreaded procedure more concurrent. In addition, the process has a separate memory unit during execution, while multiple threads share memory, which greatly improves the efficiency of the program.

Threads are still different from processes during execution. Each independent thread has a program to run the entry, sequential execution sequence, and exit of the program. However, threads cannot be executed independently, and must be dependent on the application, which provides multiple thread execution control by the application.

From a logical point of view, multithreading means that multiple parts of an application can be executed concurrently. However, the operating system does not consider multiple threads as multiple independent applications to implement process scheduling and management and resource allocation. This is the important difference between process and thread.

A process is a program with a certain independent function about a running activity on a data set, and a process is an independent unit of resource allocation and scheduling for a system.

A thread is an entity of a process, the basic unit of CPU scheduling and dispatching, and it is a unit that can operate independently, smaller than a process. The thread itself has essentially no system resources, only a few of the necessary resources in operation (such as program counters, a set of registers and stacks), However, it can share all the resources owned by the process with other threads belonging to one process.

One thread can create and revoke another thread, and multiple threads in the same process can concurrently execute

The main difference between processes and threads is that they are different ways of managing operating system resources. Processes have separate address spaces, and when a process crashes, it does not affect other processes in protected mode, and threads are just different execution paths in a process. Thread has its own stack and local variables, but there is no separate address space between the threads, one thread dead is equal to the entire process dead, so the process of multiple processes than multithreaded program is robust, but in the process of switching, the resource consumption is greater, the efficiency is some worse. However, for concurrent operations that require simultaneous and share certain variables, only threads can be used, and processes cannot be used. If you are interested, I suggest you take a look at the modern operating system or the design and implementation of the operating system. Speak more clearly on a question.

+++++++++++++++++++++++++++++++++++++++++++++++

Process Concepts

Process is the basic unit of resource allocation and the basic unit of dispatch operation. For example, when a user runs his or her own program, the system creates a process and assigns IT resources, including various tables, memory space, disk space, I/O devices, and so on. Then, put the process in the ready queue of the process. The process Scheduler selects it, allocates CPU and other related resources to it, and the process does not actually run. Therefore, the process is the unit of concurrent execution in the system.

In a micro-kernel-structured operating system such as Mac, Windows NT, and so on, the functionality of the process has changed: it is only a unit of resource allocation, not a unit of dispatch operations. In the microkernel system, the basic unit of the real dispatch operation is the thread. Therefore, the unit that implements the concurrency function is the thread.

Threading Concepts

A thread is the smallest unit of execution in a process, that is, the basic unit of a processor dispatch. If you understand a process as a task that is done logically by the operating system, the thread represents one of the many possible subtasks to complete the task. For example, if a user starts a database application in a window, the operating system represents a call to the database as a process. Suppose a user wants to generate a payroll report from a database and upload it to a file, which is a subtask; in the process of generating a payroll report, the user can also lose a database query request, which is another subtask. In this way, the operating system represents each request-the payroll report and the data query for the new loser-as a separate thread in the database process. Threads can schedule execution independently on the processor, allowing several threads to be on a separate processor in a multiprocessor environment. The operating system provides threads for the convenience and effectiveness of this concurrency

Benefits of introducing threads

(1) Easy to dispatch.

(2) Improve concurrency. It is easy and efficient to implement concurrency through threads. A process can create multiple threads to perform different parts of the same program.

(3) less cost. Creating a line turndown the creation process is fast and requires very little overhead.

(4) Facilitate the full use of multiprocessor functions. By creating a multithreaded process (that is, a process can have two or more threads), each thread runs on one processor, enabling the concurrency of the application so that each processor is fully operational.

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.