By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,665 Members | 1,761 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,665 IT Pros & Developers. It's quick & easy.

fork implementation

P: n/a
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?

Regards

Feb 8 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On 2006-02-08, ramu <ra******@gmail.com> wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?


magic.
Feb 8 '06 #2

P: n/a
ramu wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?

Regards

Your question is off-topic here as fork() is POSIX.1.

I will give you an answer but you should also ask
the question in a UNIX group to get a validation:

If you read the manual for fork (man fork) you'll see that
fork() causes the creation of a new process. Read the rest
of the stuff and when you get to RETURN VALUES it says:
Upon successful completion fork() returns a value of 0 to the
child process and returns the process ID of the child process
to the parent process. So fork creates a clone of your project
and returns with the ID to the calling process. The cloned
fork call in the child returns 0. Both processes continue after
the call like the call was executed on both processes. The
function returns only one value for every return.

Again, check the appropriate group because you may be able
to get a clearer explanation and it's likely to be more
correct than mine.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 8 '06 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ramu wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?


It isn't implemented as a C function. It is implemented as an operating system
call wrapped in a C function. It is the operating system that performs the
magic of causing fork()s return value to differ depending on the process.

As such, this magic is off-topic for comp.lang.c and should be discussed in
some other group.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD6XIYagVFX4UWr64RAh0mAKC4EvbxujznOuD34nIsFp AyqQdLWgCgwF4w
mYIGfRtyjAhmUM8LaQ158MY=
=hIYb
-----END PGP SIGNATURE-----
Feb 8 '06 #4

P: n/a
just system call.

Feb 8 '06 #5

P: n/a
"dukguru" <du*****@gmail.com> writes:
just system call.


Please read <http://cfaj.freeshell.org/google/>. Thanks.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 8 '06 #6

P: n/a

ramu wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?

Regards


C function returns only once for each process. Please note that fork
does not return twice.
"For each parent and child process, fork returns only once."

Feb 8 '06 #7

P: n/a
ramu wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values?


It doesn't. It's also not a Standard C function, so it's off-topic here.

--
Chris "try, or try not -- there is no do" Dollin
Feb 8 '06 #8

P: n/a
"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
On 2006-02-08, ramu <ra******@gmail.com> wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?


magic.


Indeed. I wrote several versions of fork, over the years, and none
were easy. My favorite one used setjmp and longjmp in the kernel,
so we wouldn't have to change so much magic code every time we did
a port. (That was Whitesmiths' Idris operating system.)

But the final (and best) word on fork is the initial one: The
critical magic in the Unix V6 C code for the kernel has a
remarkably long (for Thompson and Ritchie) comment trying to
describe how the double return actually works. It ends with the
laconic:

"You are not expected to understand this."

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Feb 8 '06 #9

P: n/a
P.J. Plauger wrote:
"Jordan Abel" <ra*******@gmail.com> wrote in message
news:sl***********************@random.yi.org...
On 2006-02-08, ramu <ra******@gmail.com> wrote:
Hi All,
We know that a c function never returns more than one
value. Then how come the fork() function returns two values? How it
is implemented?


magic.


Indeed. I wrote several versions of fork, over the years, and none
were easy. My favorite one used setjmp and longjmp in the kernel,
so we wouldn't have to change so much magic code every time we did
a port. (That was Whitesmiths' Idris operating system.)

But the final (and best) word on fork is the initial one: The
critical magic in the Unix V6 C code for the kernel has a
remarkably long (for Thompson and Ritchie) comment trying to
describe how the double return actually works. It ends with the
laconic:

"You are not expected to understand this."

http://cm.bell-labs.com/cm/cs/who/dmr/odd.html
--
==============
Not a pedant
==============
Feb 8 '06 #10

P: n/a
ramu wrote:

Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?


Well, fork() is not standard C, but...

The short answer is "it doesn't return two values".

The longer answer is "it causes the current process to become two separate
and distinct processes, each of which gets returned a separate and distinct
single value".

How this is done is system-specific, and is considered "magic" as far as
your program is concerned.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Feb 8 '06 #11

P: n/a
On 2006-02-08, santosh <sa***********@gmail.com> wrote:

ramu wrote:
Hi All,
We know that a c function never returns more than one value.
Then how come the fork() function returns two values? How it is
implemented?

Regards


C function returns only once for each process. Please note that fork
does not return twice.
"For each parent and child process, fork returns only once."


Note, however, that "A function only returns once" is still a
generalization that is not always true [though it is 99.9% of the time]
even for standard C. setjmp(), for example, returns a second time when
longjmp() [which doesn't return at all] is called. exit() doesn't return
either. Neither does abort(), and whether raise() does depends on what
is done inside the signal handler.
Feb 8 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.