[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

One call to pthread_create implies 3 processes with ps

Hi all,
I begin using pthreads on a linux platform (RH7). My program creates 1 thread which is accepting lines from the standard input. It works but when I execute "ps -af", I see 3 instances of my process. I thought it would be at most 2.
Why are there 3 processes ?
Is there other commands than ps to list threads of a process on linux ?

Program:

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
void *start(void *arg) {
char line[128];
  printf("thread pid %d\n", getpid()) ;
  while (1)     {
    printf("Data>");
    if (fgets(line, sizeof(line), stdin) == NULL) break ;
    if (strlen(line) <= 1) break ;
    printf("line [%s]\n", line) ;
  }
  return NULL ;
}
int main (int argc, char ** argv) {
  pthread_t mThreadId ;
  int status ;
  status = pthread_create(&mThreadId, NULL, start, NULL) ;
  if (status != 0)
  {
    printf("pthread_create failed, error %d", status) ;
  }
  printf("main pid %d\n", getpid()) ;
  status = pthread_join(mThreadId, NULL) ;
  if (status != 0)
  {
    printf("pthread_join failed, error %d", status) ;
  }
}

Compilation and execution:
# gcc -o thc -lpthread -g thread.c
# thc                    
main pid 14430
thread pid 14432
Data>
Data>sdgqg
line [sdgqg
]
Data>
#

Process list:
# ps -af |grep thc                          
root     14388 13912  0 13:58 pts/3    00:00:00 thc
root     14389 14388  0 13:58 pts/3    00:00:00 thc
root     14390 14389  0 13:58 pts/3    00:00:00 thc

With gdb, it also seems to have 3 threads/processes:
# gdb thc
...
This GDB was configured as "i386-redhat-linux"...
(gdb) r
Starting program: /home/sylvie/essais/thread/thc
[New Thread 1024 (LWP 14415)]
[New Thread 2049 (LWP 14416)]
[New Thread 1026 (LWP 14417)]
thread pid 14417
Data>main pid 14415

Thanks for help,
Sylvie
0
scn
Asked:
scn
1 Solution
 
BlackDiamondCommented:
scn,
This is because you are seeing the system-level threads in the process list.  You are creating a single user-level thread (with pthread_create), but the system is creating a second system-level thread, probably for the process management and to provide a non-blocked system thread for preemptive multi-tasking.  I found a page here that gives some good detail on this.

http://www.cs.utk.edu/~rich/classes/cs560/lecture-2/lecture.html

This quote from the same page pretty well sums it up....
 
"Now, here's the tricky part. If a thread makes a blocking system call, then if there are other user-level threads bound to the same system-level thread, a new system-level
thread is created and the blocking thread is bound to it. What this does is let the other user-level threads run while the thread is blocked. This state of affairs is true for both the Solaris and Linux implementations."
0
 
bryanhCommented:
Actually, Linux pthreads are a little less sophisticated than most.  There are no user-level threads (there are other libraries that let you do user-level threads, though).  So all threads, whether they make blocking system calls or not, show up in "ps" as a process.

But there is one additional process.  I don't know exactly why it's necessary, but the first time you do a pthread_create() a thread manager process gets created.  That process is the parent of the thread you create, and of any future threads you create with pthread_create().

Hence, there is a total of 3 processes in the present example.  If you add a second pthread_create() to it, you'll see 4 processes.  Do a ps f (f, not -f) to see the family tree relationship of these 4 processes.

This also shows you another weirdness of Linux pthreads: the main thread is special.  In most systems, all the threads are peers.  The original thread can even exit and the other threads continue.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now