LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2006, 03:32 AM   #1
blackhole123
LQ Newbie
 
Registered: Nov 2005
Posts: 8

Rep: Reputation: 0
Threads are created as seperate processes


Hi,
I have a small prog which creates threads,
but on Redhat AS 2.1 kernel : 2.4.9-e.24
===============================================================
pid_t pid=fork();
if (pid == 0)
{
//create another child
pid_t newchild = fork();
if (newchild == 0)
{
//child of child

/* Create independant threads each of which will execute function */

iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
sleep(50);

pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
else
{
p1= pthread_create( &thread1, NULL, print_message_function, (void*) message1);
p2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
sleep (50); //just to check
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}

}
else{
waitpid(pid,NULL,0);
}

return 0;
}

void *print_message_function( void *ptr )
{
char *message;
message = (char *) ptr;
printf("%s \n", message);
}
================================================================
this progs actually creates 2 processes and 4 threads,
but when i check the with ps -e i see many process instead of threads,
here is the sample output of pstree
pstree -p 22203
threads(22203)---threads(22204)-+-threads(22205)---threads(22207)
`-threads(22206)

My questions is i should see only 2 processes but here i am seeing many processes,
the threads are not getting created as threads but as processes...
is this is a known problem , if so is there any fix avaliable...

Thanks in advance.
 
Old 03-16-2006, 03:14 PM   #2
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 03-16-2006, 03:42 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
This thread might help answer your question:
http://www.linuxquestions.org/questi...37#post2014737
 
Old 03-16-2006, 09:57 PM   #4
blackhole123
LQ Newbie
 
Registered: Nov 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks for the reply. it answered my question about processes and threads.

But i still have one question in Fedora (Kernel 2.6) we have something known as NPTL is it Native Pthread Libaray and on such kernel will the threads will be threads and not processess(LWP).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Show processes, not threads Martin Strand Linux - Newbie 1 06-14-2005 08:46 AM
How do I monitor JVM threads being created in RH7.3? yalcttocs Linux - General 0 08-27-2004 11:49 AM
Threads showing up as processes on ps command eDubster Linux - General 1 04-28-2003 04:51 PM
Amount of Threads Created X11 LQ Suggestions & Feedback 6 06-19-2002 02:42 PM
Amount of Threads Created X11 LQ Suggestions & Feedback 0 06-07-2002 12:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration