473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

fork implementation

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
11 8240
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
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
-----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
just system call.

Feb 8 '06 #5
"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

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
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
"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.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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
6
by: shellcode | last post by:
the code: ------fork.c------ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> int main() {
4
by: lasek | last post by:
Hi all, i have made a simple script that use fork, waitpid and sigaction for SIGCHLD. #include<sys/types.h> #include<sys/wait.h> #include<stdio.h> #include<errno.h> #include<unistd.h>...
27
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork();...
1
by: vduber6er | last post by:
Hi I want to have a wait page while the rest of the cgi does its process, but it seems like the wait page waits till everything is complete and never appears. I've tried forking twice already as...
3
by: thrillseekersforever | last post by:
The questions(A&B) are to fine no# of process running from the below codes. However, I couldn't decipher the solution. Will someone please throw some light on this? Thanks a lot!! A] void...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
3
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child...
4
by: AK | last post by:
Hi everyone, Please check out this query that I have in : http://groups.google.com/group/progav/browse_thread/thread/29057ec587dd46b3 (its a bit long, thats why I am not typing it again;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.