Solved

One call to pthread_create implies 3 processes with ps

Posted on 2001-09-13
2
425 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
what does this command mean: "bash -o vi" 2 271
Debian 3.16.7 3 124
install command in linux 1 43
Set-Top-Box hardware and Open Source! 8 92
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

20 Experts available now in Live!

Get 1:1 Help Now