On , Neil Heller (1:161/204@fidonet) wrote:
> Just a simple question:
Neil...
Fair enough...
> What exactly IS fork()
The fork() function does pretty much what it says - it creates a new
process identical to the one which invoked it. Since it creates a new
process, it's obviously not going to appear in any single-tasking OS, such as
DOS. Typically the code will look something like this:
/* Do stuff here */
switch (fork())
{
case -1:
/* Handle the error condition */
break;
case 0:
/* This is now the child process running - handle it */
break;
default:
/* We've returned to the parent process - continue */
}
The key thing to remember is that fork() creates an *exact* duplicate of
the process which called it. The only way for the next line of executable
code to tell whether it's the parent or child is to test the return value
from fork(). Also note that both the child and parent will be running
simultaneously in a multitasking environment.
Having said that, I should note that there are some exceptions, some
obvious, some not so obvious:
1. The child process has a new unique PID.
2. The child process has the PID of the caller as its parent PID.
3. The child has a copy of its parent's file descriptors, referring to the
same open files.
4. The child has its own copy of the parent's open directory stream.
5. The timers returned by the times() function (Posix.1 process times) are
reset for the child process.
6. File locks are not inherited by the child process.
7. Pending alarms are cleared for the child process.
8. There are no pending signals for the child process.
> and why haven't you (or anybody else) mentioned spawn() and the related
> family of functions (including spawno by Ralph Whathisname)?
The spawn family of calls are unique to PC-based single-tasking operting
systems, although they're also present in Win32 and OS/2 systems for
backwards code compatibility. Ralf Brown's spawno() and other similar
swapping spawn functions exist to overcome some of the inherent shortcomings
of a 16-bit single-tasking OS (i.e. DOS).
--- QM v1.00
---------------
* Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
|