fork implementation

R

ramu

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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)

iD8DBQFD6XIYagVFX4UWr64RAh0mAKC4EvbxujznOuD34nIsFpAyqQdLWgCgwF4w
mYIGfRtyjAhmUM8LaQ158MY=
=hIYb
-----END PGP SIGNATURE-----
 
J

Jordan Abel

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.
 
N

Nelu

ramu said:
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.
 
S

santosh

ramu said:
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."
 
C

Chris Dollin

ramu said:
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.
 
P

P.J. Plauger


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
 
P

pemo

P.J. Plauger said:
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
 
K

Kenneth Brody

ramu said:
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:[email protected]>
 
J

Jordan Abel

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linux: using "clone3" and "waitid" 0
I'm trying to display HTML from a function 3
C Programming functions 2
fork() 5
Tasks 1
HELP:function at c returning (null) 4
fork() 27
Help with array 4

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top