The fork function is called once and returns 2 times
It is returned once in the calling process, and the return value is the process ID number of the newly derived process
When the child process returns again, the return value is 0
Therefore, the return value itself tells the current process's child process or parent process
Fork returns 0 in the child process and the process ID is returned in the parent process because:
Any child process has only one parent process, and the child process can always get the process ID of the parent process by calling Getppid
Instead, the parent process can have many child processes and cannot get the IDs of the individual child processes
If the parent process wants to track the process ID of all child processes, it must record the return value of each call to fork
All descriptors opened before calling Fork in the parent process are shared by the child process after the fork is returned
We can see that the Web server takes advantage of this feature:
Call Fork After the parent process calls accept
The accepted connected sockets are then shared between the parent and child processes
Typically, the child process then reads and writes the connected socket, and the parent process closes the connected socket
There are 2 classic uses of fork:
1) A process creates a copy of itself so that each copy can handle one of its actions while another copy performs other tasks
This is a typical use of a Web server
2) One process wants to execute another program
Since the only way to create a new process is to call fork, the process then calls fork to create a copy of itself, and then one of the replicas calls exec to replace itself with a new program
This is a typical use of programs such as Shelll
The only way the executable files stored on the hard disk can be executed by UNIX are:
One of the 6 exec functions called by an existing process, Exec replaces the current process image with a new program file, and the new program typically starts with the Mian function
Process ID does not change
We call the process called Exec the calling process, which says the executed program is the new program
The difference between the 6 exec functions is:
1) Whether the program file to be executed is specified by file name or by path name
2) Whether the parameters of the new program are listed or referenced by an array of pointers
3) Pass the environment of the calling process to the new program or create a new environment for the new program
int execl (const char * pathname, const char* arg0, ...);
int execv (const char* Pathname, char* const argv[]);
int execle (const char* Pathname, const char* arg0);
int Execve (const char* pathname,char* const argv[],char* Const envp[]);
int EXECLP (const char* filename, const char *arg0, ...);
int EXECVP (const char* filename,char* const argv[]);
These functions are returned to the caller only in case of an error
Otherwise, the control is passed to the starting point of the new program, which is usually the main function
In general, only EXECVE is a system call in the kernel, and the other 5 are called Execve
Fork and EXEC functions