Technology Sharing

Android C Series: Linux Daemon

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Concept

Daemon process is a background service process in Linux. It is a long-lived process that is usually independent of the control terminal and periodically performs certain tasks or waits for certain events to occur.

2. Model

2.1 Daemon Programming Steps

  1. Create a child process, the parent process exits and all work is performed in the child process: formally separated from the control terminal;
  2. Create a new session setsid() function in the child process to make the child process completely independent and out of control;
  3. Change the current directory to the root directory chdir()Function: Prevent the unmountable file system from being occupied and can also be replaced with other paths;
  4. Reset file permission mask: umask() function prevents inherited file creation mask word from denying certain permissions and increases daemon flexibility;
  5. Close file descriptors: inherited open files will not be used, wasting system resources and cannot be uninstalled;
  6. Start executing the daemon core work;
  7. Daemon exit processing.

2.2 Code Model

#include <stdlib.h> 
#include <stdio.h> 
#include <fcntl.h>
void daemonize(void)
{
	pid_t pid;
	/*
	 * 成为一个新会话的首进程,失去控制终端 
	 */
	 if ((pid = fork()) < 0) { 
	 	perror("fork");
		exit(1);
		} else if (pid != 0) /* parent */
			exit(0); 
		setsid();
	/*
	 * 改变当前工作目录到/目录下. 
	 */
	if (chdir("/") < 0) { 
		perror("chdir"); 
		exit(1);
	}
	/* 设置umask为0 */ 
	umask(0);
	//重定向0,1,2文件描述符到 /dev/null,因为已经失去控制终端,再操作0,1,2没有意义.
	close(0); 
	open("/dev/null", O_RDWR); 
	dup2(0, 1);
	dup2(0, 2);
}
int main(void) {
	daemonize(); 
	while(1);
	/* 在此循环中可以实现守护进程的核心工作 */
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

When you run this program, it becomes a daemon process and is no longer associated with the current terminal. You cannot see it with the ps command, but you must run the ps command with the x parameter to see it. You can also see that closing the terminal window or logging off will not affect the operation of the daemon process.

3. Daemon Process Function

Why should the daemon process be separated from the controlling terminal?

The reason for being separated from the terminal is to prevent the process from being interrupted by any information generated by the terminal, and the information during the execution is not displayed on any terminal. In Linux, each interface for communication between the system and the user is called a terminal, and each process that starts from this terminal will be based on
Attached to this terminal, this terminal is called the control terminal of these processes. When the control terminal is closed, the corresponding processes will be automatically closed. However, many of our applications do not want to exit the process when the shutdown is interrupted, such as databases, Web services, game services, etc. At this time, a daemon process is needed.

4. Summary

This article introduces the concept of daemon, the programming steps of daemon, code implementation, and the role of daemon, helping us understand the operating principles of background programs.