LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-30-2013, 02:21 AM   #1
CypherManiac
LQ Newbie
 
Registered: Jul 2013
Posts: 4

Rep: Reputation: Disabled
Using fork() and pthreads: Can anyone tell me what is happenning in the "for-loop"?


#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *run_thread(void *param);
int main()
{
int i;
pthread_t tid;
pid_t pid;
for(i=0; i<4; i++) {
pid = fork();
if( pid == 0 ) {
pthread_create(&tid,NULL,run_thread,NULL);
pthread_join(tid,NULL);
}
}

}
 
Old 07-30-2013, 05:31 AM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
What part is causing you a problem? The for loop itself only does for iterations...

When a process does a fork a new process is created - in the new process the value of fork is 0, The parent process gets the process id of the new process so that it can wait for it when it exits (and can then release any resources used).

The pthread_create is only done by the child process (which creates a new thread), the join waits for that thread to exit...


So what is the problem?
 
Old 07-31-2013, 10:35 PM   #3
CypherManiac
LQ Newbie
 
Registered: Jul 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks ipollard for the clarification. Its quite clear now what is happenning in the for loop.

I have two more questions here that I would like your help on:

1. How many process (including the parent process) are created in the code.
My Ans: 9. 1 parent process, 4 child processes, 4 single thread processes.

2. How many unique threads are created in the code?
My Ans: 4. These are created bypthread_create in each of the child processes.


Please correct me if I'm wrong. Thanks a million!!
 
Old 07-31-2013, 10:53 PM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,284

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
The threads aren't really separate processes, though. Even though the Linux kernel schedules them separately, they share the PID and memory of the process that creates them.
 
Old 07-31-2013, 11:16 PM   #5
CypherManiac
LQ Newbie
 
Registered: Jul 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks btmiller.

I have a beginners understanding of the Linux kernel and how it deals with processes and threads, so this clears a fair bit.
So based on my analysis of the code;

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void *run_thread(void *param);
int main()
{
int i;
pthread_t tid;
pid_t pid;
for(i=0; i<4; i++) {
pid = fork();
if( pid == 0 ) {
pthread_create(&tid,NULL,run_thread,NULL);
pthread_join(tid,NULL);
}
}
}

Is it correct to say that the total number of processes created will include the threads as "individual thread processes"? If not, then how many processes do you see being created?

Much appreciate the help.

Regards!
 
Old 08-01-2013, 02:14 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
It depends on your definitions...
Under the 2.4 kernel, threads appeared as 'processes', with their own pids.
Under the 2.6 kernel, they are LightWeight Processes (LWPs) and do not appear in eg 'top' as separate processes, as explained by btmiller.
I would classify them as different entity types.
PS please use code tags https://www.linuxquestions.org/quest...do=bbcode#code when posting code/data, to preserve formatting.
 
Old 08-01-2013, 05:01 AM   #7
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,654

Rep: Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255Reputation: 1255
Top can show the thread processes - one it is running use the "H" option to turn them on.

Or run it as "top -H".

They also show up if you do "ls /proc". They really are full processes, but the memory map used for all threads is the same one used by the parent process, and top just adds them up on the default display.

This was one of the things different about how linux implements the "fork". The actual system call is "clone", and is used for all forks by the kernel - but with different flags used between a "thread" and a "fork". This is done in libc where the fork function resides and libpthread where the pthread function resides.

Andddd I'm not sufficiently clear. There is still a fork system call, but as I understand it this is for backwards compatibility. In the kernel it ends up using clone with a specific set of flags that defines the fork actions.

references:
http://www.makelinux.net/books/lkd2/ch03lev1sec2
http://unixhelp.ed.ac.uk/CGI/man-cgi?clone+2
http://unixhelp.ed.ac.uk/CGI/man-cgi?fork+2

Last edited by jpollard; 08-01-2013 at 05:30 AM.
 
Old 08-01-2013, 07:24 AM   #8
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
16 processes (start with 1 and double 4 times).

15 threads created. 1 first time round the loop + 2 + 4 + 8.
 
Old 08-02-2013, 01:45 PM   #9
CypherManiac
LQ Newbie
 
Registered: Jul 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01
It depends on your definitions...
Under the 2.4 kernel, threads appeared as 'processes', with their own pids.
Under the 2.6 kernel, they are LightWeight Processes (LWPs) and do not appear in eg 'top' as separate processes, as explained by btmiller.
I would classify them as different entity types.
PS please use code tags https://www.linuxquestions.org/quest...do=bbcode#code when posting code/data, to preserve formatting.
chrism01, thanks for the note on tagging. I'm quite new to this, so this really helps. For the linux kernel, I use the kernel version:
3.5.0-17-generic

Quote:
Originally Posted by linosaurusroot
16 processes (start with 1 and double 4 times).

15 threads created. 1 first time round the loop + 2 + 4 + 8.
linosaurusroot, thanks for the analysis of the number of processes and threads created.

Thanks once again everyone and thanks to this awesome forum. The sky is much clearer now on this topic. I am still a newbie to linux so you will surely see a lot more of me in the future, or whenever I need help on raising this amazing "penguin".

Cheers!

Last edited by CypherManiac; 08-02-2013 at 03:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need solution to "failed to fork PTY" and "error creating the child process" problems Gnusboy Linux - Newbie 4 01-02-2010 08:54 AM
"checking for the pthreads library -lpthreads... no" in Debian Etch cstrieder Linux - Software 7 09-19-2008 07:48 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 08:38 AM
"All of your loopback devices are in use." "could not fork" hedpe Linux - Software 2 05-24-2006 01:39 PM


All times are GMT -5. The time now is 03:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration