Advanced Process Programming-System Call-Creating a Daemon Process

Table of contents

System call API

Create daemon process

Advanced process management


System call API

Reference: Check when used

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

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:

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.

Guess you like

Origin blog.csdn.net/Staokgo/article/details/132630693