LinuxQuestions.org
Register a domain and help support LQ
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 05-23-2012, 04:49 AM   #1
elico
Member
 
Registered: Dec 2011
Posts: 137

Rep: Reputation: Disabled
Fork basic question


Hi all

I hope this is the proper sub forum to ask this .
In the following code a process is created with father and child .

19 pid_t pID = fork();
20 if (pID == 0) // child
21 {
22 // Code only executed by child process
23
24 sIdentifier = "Child Process: ";
25 globalVariable++;
26 iStackVariable++;
27 }
28 else if (pID < 0) // failed to fork
29 {
30 cerr << "Failed to fork" << endl;
31 exit(1);
32 // Throw exception
33 }
34 else // parent
35 {
36 // Code only executed by parent process
37

What is hard to understand is the fact that the fork() function in line 19 can and will return only ONE value : 0 , <0 or >0

in case of 0 the child code will be done in case of >0 the father code else an error message .

So how father and child can come to execution from one single
fork() function?


Thanks
Elico
 
Old 05-23-2012, 10:11 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
fork means you start a new process. The new process is [almost] exactly the same as the original one. To find out which one is the current process you can use the return value of fork (0 means it is the child process, >0 means the parent process and the returned value is the pid of the child, <0 means error and also no child was created)
 
Old 05-23-2012, 10:15 AM   #3
elico
Member
 
Registered: Dec 2011
Posts: 137

Original Poster
Rep: Reputation: Disabled
"The new process is [almost] exactly the same as the original one"

Which is the original process ?

Elico
 
Old 05-23-2012, 10:26 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,121

Rep: Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270Reputation: 2270
http://linux.die.net/man/2/fork
 
  


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
[SOLVED] FORK question elico Linux - Newbie 9 05-07-2012 02:30 PM
Question about fork htmin Programming 5 01-21-2011 06:09 AM
fork question djgerbavore Programming 2 04-20-2006 03:27 AM
question about fork() hubabuba Programming 2 03-31-2006 11:00 AM
fork() question elmafiacs Programming 3 01-08-2005 01:42 PM


All times are GMT -5. The time now is 12:19 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