search
HomeOperation and MaintenanceLinux Operation and MaintenanceWhat 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.

What is the linux process creation command?

#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

What is the linux process creation command?

Let’s compile and run it:

What is the linux process creation command?

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.

What is the linux process creation command?

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: After fork, 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

3-What is the linux process creation command?

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.

Extended knowledge: process termination and

##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:

    After the code is run, the result is correct.
  1. After the code is run, the result is incorrect.
  2. The code did not finish running and the program crashed.
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.

Now we write a simple C program.

What is the linux process creation command?

Then we can get the exit code of the latest process through echo $? .

What is the linux process creation command?

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

What is the linux process creation command?

The result is as follows:

What is the linux process creation command?

What is the linux process creation command?

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

What is the linux process creation command?

The error message corresponding to exit code No. 2:

What is the linux process creation command?

2. exit and _exit

can be used to terminate a process. return statement, you can also call the exit and _exit functions

exit function:

1What is the linux process creation command?

## _exit function:

1What is the linux process creation command?

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

1What is the linux process creation command?

Because we brought \n, adding \n will refresh the buffer, and the content we printed will appear on the screen.

1What is the linux process creation command?

If we do not bring \n, we will observe the result:

1What is the linux process creation command?

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

1What is the linux process creation command?

Run executable file b:

1What is the linux process creation command?

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.

1What is the linux process creation command?

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The 5 Core Components of the Linux Operating SystemThe 5 Core Components of the Linux Operating SystemMay 08, 2025 am 12:08 AM

The five core components of the Linux operating system are: 1. Kernel, 2. System libraries, 3. System tools, 4. System services, 5. File system. These components work together to ensure the stable and efficient operation of the system, and together form a powerful and flexible operating system.

The 5 Essential Elements of Linux: ExplainedThe 5 Essential Elements of Linux: ExplainedMay 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.