LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-10-2004, 01:30 AM   #1
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
about fork


Hi!
i have a doubt regarding fork. fork returns 2 values right. How come it return 2 values? ( C function can return only one value). Does it get executed again in the context of child or just it places a value in childs memory?
please reply me
bye,
Eshwar.
 
Old 02-10-2004, 01:50 AM   #2
hakuin
LQ Newbie
 
Registered: Sep 2003
Posts: 4

Rep: Reputation: 0
fork creates a child process and the parent process remains running.

The child is a copy of the parent, and is created at the point of the fork call. Therefore the parent and the child both return to the same point in the program (of their own images), but fork returns 0 to the child process and the PID of the child to the parent (unless there is an error).

Knowing that the child will have 0 returned to it, you can now setup the code so that the parent takes one path of execution and the child another.
 
Old 02-10-2004, 01:57 AM   #3
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
Thanks, can you please tell me how the fork code looks at the end of its execution? ( two returns at single shot).
please reply me.
 
Old 02-10-2004, 09:16 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
fork is a system call, the code for it can be found in the kernel.

I believe it can be found in arch/<yourarch>/process.c
 
Old 02-10-2004, 03:57 PM   #5
schmack
LQ Newbie
 
Registered: Feb 2004
Posts: 12

Rep: Reputation: 0
Here's an example:

<code>

int runChild()
{
int pid = fork();
if (pid == 0)
{
/* i am the child, run a program */
execl("/bin/cp", "file1", "file2", 0);
/* should not return... if it does, call _exit() */
_exit(1);
}
printf("started process ... pid is %d\n", pid);
/* you could do a waitpid() if you want to get the return status */
return pid;
}

</code>
 
Old 02-11-2004, 03:38 AM   #6
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Original Poster
Rep: Reputation: 15
thanks jtshaw & schmack. I am looking for the fork code jtshaw you gave me that address.
thanks
 
  


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
more on fork() feetyouwell Programming 6 09-17-2004 11:18 AM
fork() vibhory2j Linux - Software 1 05-24-2004 04:11 AM
Fork again Avatar33 Programming 13 08-22-2003 01:41 PM
fork() lowlifeish Programming 3 11-04-2002 10:50 AM
Fork Ztyx Linux - General 1 08-31-2002 11:25 AM

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

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