3

Absolutely strange situation on thread creation , here the test code :

#include <stdint.h>         /* C99 types */
#include <stdbool.h>        /* bool type */
#include <stdio.h>          /* printf, fprintf, snprintf, fopen, fputs */

#include <string.h>         /* memset */
#include <signal.h>         /* sigaction */
#include <time.h>           /* time, clock_gettime, strftime, gmtime */
#include <sys/time.h>       /* timeval */
#include <unistd.h>         /* getopt, access */
#include <stdlib.h>         /* atoi, exit */
#include <errno.h>          /* error messages */
#include <math.h>           /* modf */
#include <assert.h>

#include <sys/socket.h>     /* socket specific definitions */
#include <netinet/in.h>     /* INET constants and stuff */
#include <arpa/inet.h>      /* IP address conversion stuff */
#include <netdb.h>          /* gai_strerror */

#include <pthread.h>
void thread_up(void) {
        printf("thread ....\n");
        int loop=0;
        while(loop<=7)
        {
                printf("loop...\n");
                sleep(10);
                loop++;
        }
        printf("Exit loop\n");
}
int main()
{
pthread_t thrid_up=0;

        int i = pthread_create( &thrid_up, NULL, (void * (*)(void *))thread_up, NULL);
        if (i != 0) {
                printf("ERROR: [main] impossible to create upstream thread\n");
                exit(1);
        }
        while(1)
        {
                sleep(10);
        }

}

that code produce the following output on ps command (after ./test& launching):

30214 root        704 S   ./test
30215 root        704 S   ./test
30216 root        704 S   ./test

Any hint ?

Hasturkun
  • 35,395
  • 6
  • 71
  • 104
queifaro
  • 41
  • 3
  • 1
    `ps` doesn't show threads by default, AFAIK. Are you running it with any special flags? Are you sure that you didn't just run three instances of your program? What happens if you drop the call to `pthread_create()`? – Hasturkun Aug 02 '16 at 12:57
  • I see no reason this question was closed. This is a perfectly valid question. Someone owes this guy an up-vote back. – Jonathon Reinhart Aug 02 '16 at 17:46

3 Answers3

4

Just a note. man 7 pthreads

LinuxThreads

The notable features of this implementation are the following:

  • In addition to the main (initial) thread, and the threads that the program creates using pthread_create(3), the implementation creates a "manager" thread. This thread handles thread creation and termination. (Problems can result if this thread is inadvertently killed.)
  • Threads do not share process IDs. (In effect, LinuxThreads threads are implemented as processes which share more information than usual, but which do not share a common process ID.) LinuxThreads threads (including the manager thread) are visible as separate processes using ps(1).

Regardless that this doc is about Glibc your version uClibc may use LinuxThreads as pthread "backend". So technically your code may create three scheduling entities.

But first of all you really must ensure that ps prints threads, not only processes and double check that there is only one launched instance of your program during the probing.

P.S. Also you should check that your uClibc really uses LinuxThreads not NPTL. So IMO the chance to observe three threads exists, but it is very small.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Sergio
  • 8,099
  • 2
  • 26
  • 52
  • Probably you're right, I updateted my answer with the link to sources. +1 – LPs Aug 02 '16 at 13:58
  • 2
    Why didn't you keep reading (and copy/pasting) from that page?! *Threads do not share process IDs. (In effect, LinuxThreads threads are implemented as processes which share more information than usual, but which do not share a common process ID.) LinuxThreads threads (including the manager thread) are visible as separate processes using ps(1).* – Jonathon Reinhart Aug 02 '16 at 17:47
2

ps shows you the process, not threads.

Probably you only started 3 times your app using ./test &

To test it kill all processes using: killall test

So start again your process and see the output of: ps -A | grep test It will show you 1 instance of test

Then using: ps -eLf | grep test you'll see threads of test.


EDIT

Related to @Serhio answer, that is probably correct, looking into uClibc source at this link you can find, into libpthread directory, that LinuxThreads are used, so a thread manager is added.

LPs
  • 16,045
  • 8
  • 30
  • 61
  • *"`ps` shows you the process, not threads."* Nowdays, yes. That wasn't the case with LinuxThreads. Each thread had its own PID (yes, PID), which is why the OP is seeing the output he is. – Jonathon Reinhart Aug 02 '16 at 17:49
  • @JonathonReinhart good to know. Thanks. I learn something today ;) – LPs Aug 02 '16 at 19:17
0

yes... right (@Serhio and @LPs)... my uClibc use the manager i've just checked.

Unfortunately my ps is very poor due to tyny linux embedded enviroment.

queifaro
  • 41
  • 3