fork function is the only way in Unix to create a new process:
pid_t fork(void);
fork is called once but it returns twice. It returns once in the calling process with a return value that is the process ID of the newly created process. It also returns once in the child, with a return value of 0.
All descriptors open in the parent before the call to fork are shared with the child after fork returns.
Two typical use of fork:
1) A process make a copy of itself so that one copy can handle one operation while the other copy does another task.
2) A process wants to execute another program. And then one of the copies calls exec to replace itself with the new program. This is typical for program such as shells.
The only way in which an executable program file on the disk can be executed by Unix is for an existing process to call one of the six exec functions. exec replace the current process image with the new program file, and this new program normally starts at the main function.
exec functions return to the caller only if an error occurs. Otherwise, control pass to the start of the new program, normally the main function.
Friday, February 15, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment