LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 03:49 AM   #1
elico
Member
 
Registered: Dec 2011
Posts: 145

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, 09:11 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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, 09:15 AM   #3
elico
Member
 
Registered: Dec 2011
Posts: 145

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, 09:26 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

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


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:23 AM.

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