Table of contents
System call API
Reference: Check when used
-
How to perform multi-process programming under Linux (preliminary) - Zhihu (zhihu.com) .
-
Three methods of system calls under Linux_Haifeng Linying's blog-CSDN blog .
-
linux system call (continuously updated....)_tiramisu_L's blog-CSDN blog .
Through the library functions provided by glibc, direct calls using syscall, trapping through int instructions; popen() function, fgets() function, system() function, strstr() function.
The system() function first calls fork(), then calls exec() to execute the user's login shell, uses it to find the command of the executable file and analyzes the parameters, and finally uses one of the wait() function family to wait for the child The end of the process. The function popen() is similar to the function system(), except that it calls the pipe() function to create a pipe through which the standard input and standard output of the program are completed. These two functions are designed for less diligent programmers, have considerable flaws in efficiency and safety, and should be avoided when possible.
Create daemon process
Reference: Check when used
-
Explanation of two forks in Linux applications leng_que's blog - CSDN blog linux two forks , if a process forks a child process, but does not want it to wait for the child process to terminate, nor does it want the child process to be in a zombie state until the parent process terminates, The trick to achieve this requirement is to call fork twice.
-
The daemon process runs in the background and is very important. Most servers under Linux are implemented using daemon processes. Because it runs independently of the terminal, that is to say, its operation is not affected by the current login or logout. It forms a session of its own, periodically performs a task or waits for certain events to be processed.
Create a daemon process: Method 1: fork() twice; Method 2: Call the daemon() function to create it.
-
The difference between forking once and twice in the daemon process_Number_0_0's blog-CSDN blog .
-
etc.
Creating a daemon process means writing a memory-resident program.
For starting a program in the shell and putting a program in the background for execution: As long as we add
&
the symbol after the command, SHELL will put our program in the background to run.For writing programs: here is the method of one fork() (the parent process terminates, the child process enters the background), and the method of two fork() (the parent process runs, the child process terminates, the grandchild process enters the background)
The idea of creating a background process: first, the parent process creates a child process, and then the child process kills the parent process, so that the child process becomes an orphan process/is adopted to the init process/becomes a background task, and the task processing work is handled by the child process (terminating the parent process) Process:
kill(getppid(),SIGTERM);
).
There are four commonly used methods:
-
Start a program in the shell and add
&
the identifier at the end to put it into the background for execution. -
A fork() method (the parent process terminates and the child process enters the background).
-
Two fork() methods (the parent process runs, the child process terminates, and the grandchild process enters the background).
-
Use the daemon() function,
int daemon (int nochdir, int noclose);
, number: nochdir non-0 means that the working directory will not be changed to the root directory, noclose non-0 means that all open file descriptors will not be closed, usually set to 0, return 0 on success, and return on failure -1 and sets the errno value.
Advanced process management
For reference:
-
《Linux System Prorgrammin》。
-
Linux System Programming_ Chinese version_ by _ Harbin Institute of Technology (Translation).
-
.etc
content include:
-
Process scheduling.
-
Yield the processor.
-
Process priority.
-
Processor affinity (discussing the balance/equalization of multi-processes across multi-processors).
-
Real-time system (some real-time support of Linux, mainly including setting scheduling policies, setting priorities, etc.).
-
Resource limitations.
Just refer to professional books and check them when you need them. The advanced programming of processes is quite professional, and it seems that it requires a lot of understanding and experience of the system, etc.