How to kill the whole subprocess created with pthread_create after the thread was canceled?

I have the following code, and I do ps aux | grep myprogramat each stage of the main () code myprogram (name of the application being created).

At the beginning of the execution of myprogram , it ps aux | grep myprogramshows only 1 time myprogram in the list

after canceling the thread that I created during begging main(), it ps aux | grep myprogramshows myprogram twice , and I expected to get only 1.

Can someone explain this behavior? and how to return to the original situation (only 1 myprogram)

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

pthread_t test_thread;

void *thread_test_run (void *v)
{
    int i=1;
    while(1)
    {
       printf("into thread %d\r\n",i);
       i++; 
       sleep(1);
    }
    return NULL
}

int main()
{
    // ps aux | grep myprogram  ---> show only 1 myprogram

    pthread_create(&test_thread, NULL, &thread_test_run, NULL);

    // ps aux | grep myprogram  ---> show  3 myprogram

    sleep (20);  


    pthread_cancel(test_thread);

    // ps aux | grep myprogram  ---> show 2 myprogram and I expected only 1 !!??

   // other function are called here...

    return 0;
}

EDIT

libc used by linux libc-0.9.30.1.so

# ls -l /lib/| grep libc
-rwxr-xr-x    1 root     root        16390 Jul 11 14:04 ld-uClibc-0.9.30.1.so
lrwxrwxrwx    1 root     root           21 Jul 30 10:16 ld-uClibc.so.0 -> ld-uClibc-0.9.30.1.so
lrwxrwxrwx    1 root     root           21 Jul 30 10:16 libc.so.0 -> libuClibc-0.9.30.1.so
-rw-r--r--    1 root     root         8218 Jul 11 14:04 libcrypt-0.9.30.1.so
lrwxrwxrwx    1 root     root           20 Jul 30 10:16 libcrypt.so.0 -> libcrypt-0.9.30.1.so
-rw-r--r--    1 root     root       291983 Jul 11 14:04 libuClibc-0.9.30.1.so
0
source share
3

, glibc ( 2.2 2.3), linuxthreads pthread.

. pthread_create; .

Linux glibc NPTL ( "Native posix thread library" ). , ps axu; ps axum ( m), . NPTL .

PS http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html D.5 answer:

D.5: , N , ps N + 2 , . ?

- " " N , pthread_create. . " ", , LinuxThreads . .

PPS: , ; , mux: libc-0.9.30.1 uClibc , linuxthreads (, , posix). : http://web.archive.org/web/20070609171609/http://www.uclibc.org/downloads/Changelog

0,9.10 21 2002

:     o pthreads ( glibc 2.1.3 linuxthreads)         

+3

, pthreads, , , glibc 2.4 , NPTL, , ps, , t , :

ps -AL

pthread_cancel():

$ ps -AL | grep tst
  983   983 pts/2    00:00:00 tst
  983   984 pts/2    00:00:00 tst
$ ps -AL | grep tst
  983   983 pts/2    00:00:00 tst

uclibc, NPTL, ps aux .

Native POSIX Threads Library (NPTL) . , ps, ps ( procps) , , "-L".

0

() Linux-, , ps, Linux-.

, , , , , / .

For this to happen, you must call pthread_detach(pthread_self())in your thread or create a thread in a separate state: A separate thread will destroy the resources of the thread when this thread ends, and a separate thread cannot be joined later pthread_join ().

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1);
pthread_create(&test_thread, &attr, &thread_test_run, NULL);

Or you should call pthread_join () on it, for example.

pthread_create(&test_thread, NULL, &thread_test_run, NULL);

sleep (20);  
pthread_cancel(test_thread);
pthread_join(test_thread);

pthread_join () will release thread resources.

The concept is roughly similar to zombie processes, where the process is not in full possession until the parent calls wait () or the like in this process.

0
source

Source: https://habr.com/ru/post/1659483/


All Articles