Solved

One call to pthread_create implies 3 processes with ps

Posted on 2001-09-13
2
432 Views
Last Modified: 2010-04-21
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
Comment
Question by:scn
2 Comments
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 6480817
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
 
LVL 5

Accepted Solution

by:
bryanh earned 200 total points
ID: 6501756
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now