I have the following code and I make ps aux | grep myprogram in each step of the main() code of myprogram (name of the application I build).
At the beggining of the execution of myprogram, the ps aux | grep myprogram show only 1 time the myprogram in the list
after cancelling a thread that I created in the begging of the main(), the ps aux | grep myprogram show the myprogram twice and I expected to get only 1.
Could some one explain this behaviour? and how to return to the initial 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
the libc used by the linux is 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
I’ll assume you have some outdated glibc (version 2.2 or 2.3), which used the “linuxthreads” implementation of pthread.
In this older library one additional thread is created by the library for thread management; it can be created after first call to pthread_create; but it will sleep most time.
In newer linuxes there is glibc with NPTL (“Native posix thread library”) implementation. When it is used, you will not see threads in
ps axu; useps axum(withm) to see native threads. And NPTL not uses a management thread.PS Check http://pauillac.inria.fr/~xleroy/linuxthreads/faq.html D.5 answer:
PPS: Thanks, Mohamed KALLEL; thanks, mux: libc-0.9.30.1 is uClibc and seems that it uses same outdated linuxthreads implementation (which is known to be not fully posix-compatible). Here is changelog: http://web.archive.org/web/20070609171609/http://www.uclibc.org/downloads/Changelog