Link to home
Start Free TrialLog in
Avatar of scn
scn

asked on

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
Avatar of BlackDiamond
BlackDiamond

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."
ASKER CERTIFIED SOLUTION
Avatar of bryanh
bryanh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial