Summary of linux multi-process (fork usage)-Linux general technology-Linux programming and kernel information. The following is a detailed description. Original: lobbve223
Simply put, a process represents a State in one execution process of an executable program. The management of processes by the operating system is typically completed through the process table. Each table entry in the table records a process in the current operating system. For a single CPU, only one process occupies the CPU at each specific time, but there may be multiple active (waiting for execution or continuing execution) processes in the system at the same time.
Fork () is used to create a new process from a stored process. The new process is a sub-process, and the old process is a parent process. you can check the "fork ()" returned value to know which is a child process and which is a parent process. The Return Value of the parent process is the process number of the child process, and the return value of the child process is 0.
The basic mode is:
# Include
# Include
# Include
# Include
# Include
# Include
Main ()
{
Pid_t pid;
Int rv;
Pid = fork (); // create a process
Switch (pid)
{
Case-1: // If-1 is returned, the process is not created successfully.
Perror ("fork ");
Exit (1 );
Case 0:
Printf ("CHILD: This is the child process! \ N ");
Printf ("CHILD: My PID is % d \ n", getpid (); // call getpid to get your own PID
Printf ("CHILD: My parent's PID is % d \ n", getppid (); // getppid () gets the PID of the parent process
Printf ("CHILD: Enter my exit status (make it small ):");
Scanf ("% d", & rv );
Printf ("CHILD: I'm outta here! \ N ");
Exit (rv );
Default:
Printf ("PARENT: This is the parent process! \ N ");
Printf ("PARENT: My PID is % d \ n", getpid ());
Printf ("PARENT: My child's PID is % d \ n", pid); // The default value returned by fork () means that you are in the PARENT process, the return value is the PID of the sub-process. This is the only way to obtain the sub-process PID.
Printf ("PARENT: I'm now waiting for my child to exit ()... \ n ");
Wait (& rv); // The parent process must wait until the child process finishes cleaning up the residual child process before continuing.
Printf ("PARENT: My child's exit status is: % d \ n", WEXITSTATUS (rv); that is, the PARENT process cannot die before the child process.
Printf ("PARENT: I'm outta here! \ N ");
}
}
// EXITSTATUS () is a macro that extracts the actual return value from the wait () return value.
// How does wait () know which process to wait? I mean, because the parent process can have multiple sub-processes, which is the actual waiting place for wait? The answer is very simple. It waits for the one that exits first. You can call waitpid () with the PID of the child process as the parameter to specify the child process.
If (! Fork ())
{
Printf ("I'm the child! \ N ");
Exit (0 );
} Else
{
Printf ("I'm the parent! \ N ");
Wait (NULL );
}
Linux is a multi-user and multi-process operating system. When a process is created in the operating system, a process description block is generated to describe all information about the current process, including, the address of the Data Segment, code segment, stack segment, environment variable of the current process, and file descriptor.
Fork function process: the operating system first creates a process description block, and then accurately copies the information of all process descriptors of the parent process, which is the same as that of the parent process (except for the process ID ), code segment sharing, data segment and stack segment replication, all register values are precisely copied, and file descriptors may be precisely copied.
The return value of fork. fork returns the PID of the child process in the parent process space, and returns 0 in the child process space.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.