Home>Article>Operation and Maintenance> What is the linux process creation command?
Linux process creation commands: 1. The fork command can create a new process from an existing process. The new process is a child process, and the original process is the parent process; the child process completely copies the resources of the parent process. . 2. With the vfork command, the child process created shares the address space with the parent process, which means that the child process completely runs in the address space of the parent process. 3. The clone command can selectively copy the resources of the parent process to the child process, and the data structures that are not copied are shared by the child process through pointer copying.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
There are three commands available for creating a process in Linux system: fork, vfork, and clone.
fork
When fork creates a process, the child process only completely copies the resources of the parent process. The copied child process has its own task_struct structure and pid, but Copies all other resources of the parent process. For example, if the parent process has five files open, then the child process also has five files open, and the current read and write pointers of these files are also stopped at the same place. So, what this step does is copy. The child process obtained in this way is independent of the parent process and has good concurrency. However, the communication between the two requires special communication mechanisms, such as pipe, shared memory and other mechanisms. In addition, to create a child process through fork, you need to use the above description Make a copy of each resource. From this point of view, fork is a very expensive system call. These overheads are not necessary in all cases. For example, after a process forks a child process, its child process is just to call exec to execute another executable. file, then copying the virtual memory space during the fork process will be a redundant process. But since Linux now adopts copy-on-write (COW copy-on-write) technology, in order to reduce overhead, fork will not actually produce two different copies initially, because at that time, a large amount of data is actually completely the same. Copy-on-write defers the actual copy of the data. If writing does occur later, it means that the data of parent and child are inconsistent, so a copy action occurs, and each process gets its own copy, which can reduce the overhead of system calls. So with copy-on-write, the implementation of vfork is of little significance.
The fork() call returns two values once executed. For the parent process, the fork function returns the process number of the subprogram, while for the subprogram, the fork function returns zero. This is the essence of a function returning twice. .
After fork, both the child process and the parent process will continue to execute the instructions after the fork call. The child process is a copy of the parent process. It will obtain a copy of the parent process's data space, heap and stack. These are copies. The parent and child processes do not share this part of memory. In other words, modification of a variable with the same name in the parent process by the child process will not affect its value in the parent process. But the father and son processes share something, which is simply the text section of the program. The text segment stores machine instructions executed by the CPU and is usually read-only.
vfork
The vfork system call is different from fork. The child process created with vfork shares the address space with the parent process, which means that the child process completely runs in the parent process. In the address space, if the child process modifies a variable at this time, this will affect the parent process.
Therefore, if vfork() is used in the above example, the values of a and b printed twice will be the same, and the address will be the same.
But one thing to note here is that the child process created with vfork() must explicitly call exit() to end, otherwise the child process will not be able to end, and this situation does not exist with fork().
Vfork also returns the process number of the child process in the parent process, and returns 0 in the child process.
After using vfork to create a child process, the parent process will be blocked until the child process calls exec (exec, loads a new executable file into the address space and executes it.) or exit. The advantage of vfork is that after the child process is created, it is often just to call exec to execute another program, because it will not have any reference to the address space of the parent process, so the copy of the address space is redundant, so it is shared through vfork Memory can reduce unnecessary overhead.
clone
The system calls fork() and vfork() have no parameters, while clone() has parameters. fork() is to copy all, vfork() is to share memory, and clone() can selectively copy the parent process resources to the child process, and the data structure that is not copied is shared by the child process through pointer copying. Specifically Which resources to copy to the child process are determined by clone_flags in the parameter list. In addition, clone() returns the pid of the child process.
Learn more about the fork command (process creation) below.
In-depth fork function
The fork function is a very important function in Linux. It creates a process from an existing process. new process. The new process is the child process, and the original process is the parent process.
The return value of fork function:
- Return the pid of the child process to the parent process
- Return 0 to the child process
Next, let’s use the fork function () as an example
Let’s compile and run it:
General usage of fork
- A parent process wants to copy itself so that the parent and child processes execute different code segments at the same time. For example, a parent process waits for a client request and spawns a child process to handle the request.
- A process wants to execute a different program. For example, after the child process returns from fork, it calls the exec function.
The reason why the fork call failed
- There are too many processes in the system
- Actual users The number of processes exceeds the limit
After reviewing the use of the fork function, let’s study a topic:
fork() creation Child process, what operations does the operating system do?
The process calls fork. When control is transferred to the fork code in the kernel, the kernel does the following operations:
Allocate new memory blocks and kernel data structures to child processes.
Copy some data structure contents of the parent process to the child process.
Add the child process to the system process list.
fork returns and starts scheduler scheduling.
After the parent process executes the code before fork (before), it calls fork to create the child process, and the two execution streams of the father and child are executed separately. . Note: Afterfork,who executes first is completely determined by the scheduler.
There is another question here. After fork, is the code sharing between the parent and child processes after shared, or is all code shared? Why does the child process always accurately execute the corresponding code after the fork?
#Answer: All code is shared because the CPU keeps track of where the process is executing.
- After the code is assembled, there will be many lines of code, and each line of code will have a corresponding address after it is loaded into the memory.
- Because the process may be interrupted at any time (may not be completed), the next time it continues to execute, it must continue from the previous position (not the beginning of the program or the main function), which requires The CPU must record the execution position of the current process in real time.
- So, there is corresponding register data in the CPU to record the execution position of the current process. This register is called EIP, also known as pc (point code program counter), which is used to record the next line of the executing code. The address of the code (contextual data).
- When the child process is created, its EIP will be modified. At this time, the child process will think that the data saved in the EIP is the code to be executed.
#When a child process is created, the operating system allocates the corresponding data structure to the child process, and the child process runs independently because the process is independent.
Theoretically, the child process must also have its own code and data, but generally speaking, there is no loading process when creating a child process, and the child process itself does not have its own code and data.
So, the child process can only "use" the code and data of the parent process, and the code is read-only, and parent-child sharing will not conflict; while the data may be modified and must be separated.
At this time, the operating system adopts the copy-on-write strategy.
Copy-on-write
Why does OS use copy-on-write technology to separate parent and child processes
Copying when writing is a manifestation of efficient use of memory.
Improves the operating efficiency of the system.
The OS cannot predict which spaces will be accessed before the code is executed.
##Process termination
When the process terminates, the operating system releases the relevant kernel data structure and corresponding code data requested by the process. Its essence is to release system resources.1. Process exit code
Common ways of process termination:We can clearly distinguish between the first case and the second case through the exit code of the process. About the return value of the main function in the C language learned, where the return value of the main function is the exit code of the process. Its meaning is to return to the upper-level process to evaluate the execution results of the process.After the code is run, the result is correct.
- After the code is run, the result is incorrect.
- The code did not finish running and the program crashed.
Now we write a simple C program.
Then we can get the exit code of the latest process throughecho $?.
The return value of the process has two situations: 0 and non-0. 0 means that the program runs successfully and the result is correct, and non-0 means that the program runs successfully but the result is incorrect. There are countless zero values, and different non-zero values can represent different errors, making it easier for us to define the cause of the error.
What are the common error messages?
We can use strerror to print it out
The result is as follows:
Found that under Linux, there are a total of 133 error codes
Of course, when the program crashes, the exit code is meaningless.
As we all know, Linux is written in C language, and the commands are essentially C language programs, so we can simply use the ls command as an example
The error message corresponding to exit code No. 2:
2. exit and _exit
can be used to terminate a process. return statement, you can also call the exit and _exit functions
exit function:
## _exit function: There are many differences between these two functions. Let’s give a small example first: We will then use printf to print a message, then sleep for three seconds, then use exit to exit, and observe the results Because we brought \n, adding \n will refresh the buffer, and the content we printed will appear on the screen. If we do not bring \n, we will observe the result: Found: Because there is no \n, Therefore, the contents in printf are not printed out before sleeping, but after calling exit, the contents of the buffer are refreshed and output to the screen. Next we use the _exit function Run executable file b: Discover Nothing is printed. Use echo $? to print the recent process exit code and find that file b is indeed executed. This shows that exit is a library function, and _exit is a system call, which does not refresh the contents of the buffer when exiting the process. At this point we can draw a conclusion: printf data is stored in the "buffer", exit can refresh it, and The system call interface _exit cannot refresh it. Therefore, the buffer must not be inside the operating system, but maintained by the C standard library. Related recommendations: "Linux Video Tutorial"
The above is the detailed content of What is the linux process creation command?. For more information, please follow other related articles on the PHP Chinese website!