If a parent process finishes execution normally before the threads it has spawned, the threads can be terminated prematurely.
Threads spawned by calling pthread_create()
from the main()
function of the parent process will be terminated prematurely if the parent process finishes execution before any the threads it has spawned without calling pthread_exit()
. Calling pthread_exit()
guarantees that the parent process will be kept alive until all of its threads have finished execution. Alternatively, the parent process can call pthread_join
on all of the child threads and ensure they will complete before the process concludes.
Example 1: The following code uses pthread_create()
to create a thread and then exits normally. If the child thread has not completed its execution by the time the main()
function returns, then it will be terminated prematurely.
void *Simple(void *threadid)
{
...
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
int rc;
pthread_t pt;
rc = pthread_create(&pt, NULL, Simple, (void *)t);
if (rc){
exit(-1);
}
}
[1] Standards Mapping - OWASP Top 10 2004 - (OWASP 2004) A9 Application Denial of Service
[2] Standards Mapping - Security Technical Implementation Guide Version 3 - (STIG 3) APP6080 CAT II
[3] Standards Mapping - Common Weakness Enumeration - (CWE) CWE ID 730
[4] Standards Mapping - Payment Card Industry Data Security Standard Version 1.1 - (PCI 1.1) Requirement 6.5.9