The first two talk about the fork () system call and the EXECVE () kernel function (note: All library functions exec* are EXECVE encapsulation routines).
Briefly review the fork () system call (Portal: fork () What did you do? ):
The process calls fork () to create a new process, and the new process replicates the parent process's task_struct (pcb,process control block, process controller blocks), and each submodule in task_struct, such as the kernel stack, Then the various sub-modules were modified. The system call saves the return value through the EAX register, and the fork () returns two times from the kernel state after the end of the system call, once the parent process returns, once the child process is returned, the way to differentiate the parent-child process is to see if the return value is 0, or 0 to return a new process and not 0 for the parent process.
Briefly review the role of the EXECVE () kernel function (Portal: Execve () What the hell? ):
Parse elf file, load elf file into memory, modify data segment code segment of process, modify user state stack of process (mainly add command line parameter and shell context to user state stack). Modifies the process kernel stack (especially the IP pointer of the kernel stack), and the process returns from Execve to the user state after the IP points to the main function address of the Elf file, the user-state stack contains command-line arguments and the shell context
We were surprised to find that the process was created through the fork () function, the execution code of the process is Execve () loaded, if the fork () and the Execve () group and together ~ ~ ~ Yes, it is a complete process to start the story!
#include <stdio.h> #include <stdlib.h> #include <unistd.h>int main (int argc, char * argv[]) { int PID; /* Fork Another process * /pid = fork (); if (pid<0)//PID is greater than or equal to 0 of the { /* error occurred */ fprintf (stderr, "Fork failed!"); Exit ( -1); } else if (pid==0)//The return value of the subprocess (EAX register Save) is 0, so the child process enters the else if condition branch { /* child process * /EXECLP ("/ Bin/ls "," ls ", NULL);//load the specified executable in the child process } else //The return value of the parent process (EAX register Save) > 0, so the parent process enters the else condition . /* Parent Process * //* Parent would wait for the complete*/ Wait (NULL); printf ("Child complete!"); Exit (0);} }
6.fork + Execve: The birth of a process