RE: [LIP] fork()...
"Uday K Verma" <udayv-sA5OA4KG+12fsxuIvKNZIgC/[email protected]>
| Newsgroups | gmane.user-groups.linux.india.programmers |
|---|---|
| Message-ID | <[email protected]> |
First, It was man 2 fork :)
Well, a 0 returned wouldn't indicate success that a child process has been
spawned, it would just tell that you are the newly created child. fork
returns the child process's id in the parents execution thread and 0 in the
child's execution thread. In case of a failure (EAGAIN or ENOMEM) fork
simply returns -1 to indicate error and no child process is created.
You will have to understand that right after a successful call to fork there
are now two processes running the same code. So to diverge them to different
paths you need some way to determine which one is the new process created.
Therefore, fork returns a 0 in the child's thread just to tell that it is a
child process. If there was a failure the child would never be created. In
the parents thread fork doesn't just return a positive number on success,
but the process id of the child spawned.
Like to spawn the ls command as a child you could do something like
int pid ;
If (!(pid = fork ())) {
// this is in the child's execution thread, at this time two process
are running. And this one is the child.
execlv ("/usr/bin/ls", "/usr/bin/ls") ;
}
// this is still in parents execution thread
printf ("ls was spawned with pid: %i\n", pid) ;
-Uday
-----Original Message-----
From: linux-india-programmers-admin-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
[mailto:linux-india-programmers-admin-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org] On Behalf Of
Sameer Oak
Sent: Wednesday, April 21, 2004 11:27 AM
To: linux-india-programmers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
Subject: RE: [LIP] fork()...
thanks Uday.
> So that you could determine whether you are in the child or the parent
> process.
> if (!fork ()) {
> /* this is the code that the child is supposed to run, use getpid to
> get the process id */
> /* you would proabably want to exec something here */ }
> /* parent code runs here */
> At fork a new child process is created with exactly the same process
> image as the parent, now the only way to tell whether you are a child
> or parent process is by examining the return code from fork.
...
this reason is known to me.
i think there're few more.
these are the reasons what i think:
1> fork() returns -1 with errno set to EAGAIN(violating the upper limit
1> of
processes user can create, if UID is not 0) or ENOMEM(if not sufficient
memory for the new process to have it's bss, text and other data)if it was
not successful.
0 or some +ve value are returned if fork() was successful. 0 also indicates
a success as almost all system calls do.
i guess this's one of the reasons.
2> also, the other reason is there're processes with PID starting from 1.
almost all UNIX systems
have 1 for init(some systems have 2 for page-daemon). the scheduler is not
given any no, since it's initially init 0 and after spawning init(what is
also known as init 1), it becomes scheduler.
since there's no process with PID 0 existing, 0 can safely be used to
indicate the success status of the fork() system call along with the PID of
the newly created child.
any comments???
*********************************************************
Disclaimer:
This message (including any attachments) contains confidential information
intended for a specific individual and purpose, and is protected by law.
If you are not the intended recipient, you should delete this message and
are hereby notified that any disclosure, copying, or distribution of this
message, or the taking of any action based on it, is strictly prohibited.
*********************************************************
Visit us at http://www.mahindrabt.com
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial
presented by Daniel Robbins, President and CEO of GenToo technologies. Learn
everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
_______________________________________________
linux-india-programmers mailing list
linux-india-programmers-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/linux-india-programmers
-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click