A linux/unix process is created in a UNIX system. The only way to create a new process is to call the fork system. The name of the process that calls fork.
It is the parent process, and the newly created process is called a child process. Syntax format of the System Call: pid = fork (); when the fork is returned from the system call, the two processes have the same user-level context except the pid returned.
In the sub-process, the pid value is zero. When the system starts, the process 0 created by the core internal and external locations is the only process that does not pass the system call.
The process created by fork. The core is the system call fork to complete the following operations: www.2cto.com 1. assign an empty entry to the new process in the progress table. 2. assign a unique process ID (PID) to the sub-process ). 3. Make a logical copy of the context of the parent process. Some parts of a process, such as the body area, may be shared by several processes.
So the core sometimes only needs to increase the number of references in a zone, instead of copying the zone to a new zone.
Physical zone. 4. Increase the number of file tables and index node tables associated with the process. 5. Return the process number of the child process to the parent process, and return zero to the child process. It is very important to understand the implementation of the System Call fork, because the sub-process starts its execution sequence as it drops from the sky. The following is the fork algorithm called by the system. The core is confident that there are enough resources to successfully complete the fork. If the resource does not meet
Required, the system fails to call fork. If the resource meets the requirements, the core finds an empty entry in the Process Table and starts to construct
Context of the sub-process. Www.2cto.com algorithm: fork input: No output: the PID of the parent process is the child process. The child process is 0 {check the available core resources to obtain an idle table entry and a unique PID Number. Check that the user does not run too many processes the status is set to "CREATE". copy the data in the parent process's table to the index node of the current directory in the child table and the changed root directory (if possible) reference shujia 1 file table open file reference shujia 1 copy the parent process context in the memory in the sub-process system-level context pressure into the virtual system-level context Layer/* virtual context Layer which allows sub-processes to * recognize their own data, * start from here */if when the sub-process is scheduled (the process being executed is a parent process) {set the sub-process status to "ready" status return (sub-process PID) // from the system to the user} else {initialization Time Zone return 0 ;}} let's take a look at the example below. This program indicates the shared access to files after fork is called by the system. User
When calling this program, there should be two parameters: an existing file name and a new file name to be created. Enter
Open an existing file, create a new file, and then, if there is no error, it calls fork to create
Sub-process. Sub-processes can inherit the files of the parent process by using the same file descriptor (that is, the parent process has
Files ). Of course, the Parent and Child processes must independently call the rdwrt function and execute a loop, that is, read one from the source file.
And then write a byte to the central part of the target file. When the system calls read to the end of a file
Rdwrt returns immediately. # Include <fcntl. h> int fdrd, fdwt; char c; main (int argc, char * argv []) {if (argc! = 3) {exit (1);} if (fdrd = open (argv [1], O_RDONLY) =-1) {exit (1 );} if (fdwt = creat (argv [2], 0666) =-1) {exit (1) ;}fork (); // The two processes run the same code rdwrt (); exit (0) ;}rdwrt () {for (;) {if (read (fdrd, & c, 1 )! = 1) {return;} write (fdwt, & c, 1) ;}} in this example, the file descriptors of both processes point to the same file table item. These two processes will never read
Or write to the same file offset, because the core increases the file offset after each read and write call. Although the two processes are similar
The source file is copied twice, but because they share the task, the content of the target file depends on the order of the two processes that the core schedules. If the core schedules two processes in this way: enable them to execute their system calls alternately, or even make them Alternate
After each pair of read and write operations is performed, the content of the target file is exactly the same as that of the source file. But consider this situation: the two processes are about to read two consecutive characters in the source file "AB ". Assume that the parent process has read
The character "a". At this time, the core performs context switching before writing the parent process to execute the child process. If the sub-process reads the character "B" and writes it to the target file before the parent process is scheduled
It does not contain the string "AB", but "ba. The core does not guarantee the relative speed of process execution. Let's take another example: # include <string. h> char string [] = "Hello, world"; main () {int count, I; int to_par [2], to_chil [2]; // pipeline to parent and child processes char buf [256]; pipe (to_par); pipe (to_chil); if (fork () = 0) {// The sub-process executes close (0) here; // closes the old standard input dup (to_child [0]); // copy the read of the MPs queue to the standard input close (1); // close the old standard output dup (to_par [1]); // copy the write of the pipeline to the standard output close (to_par [1]); // close the unnecessary pipeline descriptor close (to_chil [0]); close (to_par [0]); close (to_chil [1]); fo R (;) {if (count = read (0, buf, sizeof (buf) = 0) exit (); write (1, buf, count) ;}}// the parent process executes close (1) here; // reset the standard input and output dup (to_chil [1]); close (0 ); dup (to_par [0]); close (to_chil [1]); close (to_par [0]); close (to_chil [0]); close (to_par [1]); for (I = 0; I <15; I ++) {write (1, string, strlen (string); read (0, buf, sizeof (buf);} the child process inherits the file descriptors 0 and 1 (standard input and standard output) from the parent process ). The two execution system calls pipe are assigned two file descriptors in the array to_par and to_chil respectively. Then
The process execution system calls fork and copies the process context: like in the previous example, each process accesses its own
Private data. The parent process closes his standard output file (file descriptor 1), and copies the write text returned by (dup) from the to_chil Pipeline
File descriptor. Because the first empty slot in the parent process file descriptor table was just removed from the shutdown, the core
Copy the pipeline line write file descriptor to the first item in the file descriptor table.
The operator is the write file descriptor of the pipe line to_chil. The parent process of www.2cto.com replaces the standard input file descriptor with the read file descriptor of the pipe line to_par with a similar operation. And
Similarly, the sub-process closes his standard input file (file descriptor 0) and then copies the (dup) pipe line to_chil's
Read file descriptor. Because the first null item in the file descriptor table is the original standard input item, the standard input of the sub-process becomes a pipe.
The read file descriptor of the line to_chil. A sub-process performs a set of similar operations to convert its standard output to the pipe line to_par.
Write file descriptor. Then the two processes close the file descriptor returned from pipe. The result of the above operation is: when the parent process is output to the standard
When writing something, it is actually writing data to to_chil -- to send data to the child process, while the child process sends data from its standard input
Read pipeline line. When a child process writes data to its standard output, it actually writes data to to_par, which is sent to the parent process.
Data, while the parent process receives data from the MPs queue line from its standard input. Two processes use two pipelines
Exchange messages. Regardless of the execution sequence of the two processes, the execution results of this program remain unchanged. They may perform sleep and
Wake up to wait for the other party. The parent process exits after 15 cycles. Then the sub-process is read because the pipeline does not write the process.
To the end of the file and exit.