Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

One call to pthread_create implies 3 processes with ps

Posted on 2001-09-13
2
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 600 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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 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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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