Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement daemon in Linux

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to implement daemons in Linux". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to implement daemons in Linux".

What is a daemon

When the Linux system starts, it will start many system service processes. These system service processes have no control terminal and can not interact with users directly. Other processes are created when the user logs in or runs the program, and terminates when the operation ends or the user logs out, but the system service processes are not affected by the user login and logout. This process is called a daemon.

The daemon, also known as the sprite process, is a special process that runs in the background, which is independent of the control terminal and periodically performs a task or waits for certain events to be processed.

View mode

The ps axj command: view the processes on the system.

Parameter a lists not only the processes of the current account, but also the processes of all other users

Parameter x lists not only the processes that control the terminal, but also all processes that control the terminal.

Parameter j lists information related to job control

Second, the call of the daemon

The most critical step in creating a daemon is to call the setsid function to create a new session and become the session leader.

When this function is called successfully, it returns the id of the newly created Session (that is, the id of the current process), and the error returns-1. Note that before calling this function, the current process is not allowed to be the Leader of the process group, otherwise the function returns-1. It is also easy to ensure that the current process is not a Leader of the process group, as long as you fork first and then call the setsid. The child process and the slave process created by fork are in the same process group, and the Leader of the process group must be the first process of the group, so the child process cannot be the first process of the group, and there will be no problem calling setsid in the child process.

The flags for successfully creating and invoking a daemon are:

1. Successfully create a new session, the current process becomes the leader of the session, and the ID of the session is the ID of the current process

2. Successfully create a new process group and become the leader of the group. The group id of the process group is the id of the current process.

3. If the current process has a control terminal, then the current process loses the control terminal and becomes a process without a control terminal. The so-called loss of control terminal means that the original control terminal is still open and can still read and write. But it's just an ordinary open "piece", not a control terminal.

Third, the creation of daemons

1. Use umask to set the file mode creation mask to 0.

2. Call fork () and the parent process exits (exit). (if the daemon is started as a simple shell command, then the parent process terminates so that shell thinks the command has been executed. At the same time, ensure that the process is not the leader process of a process group)

3. Call setsid to create a new session. (setsid will make 1, the calling process becomes the first process of the new session; 2, the calling process becomes the leader process of a process group; 3, the calling process does not control the terminal)

Change the current working directory to the root directory

5. Close file descriptors that are no longer needed

6. Ignore SIGCHLD signal

The code implements mydemon:

In addition to our own implementation, we can also call demon in the system to set up

The reason for the second fork?

In order to clarify the reasons for the second fork, we must clarify the role of the two fork.

The function of the first fork is: 1. Let shell think that this command has been terminated and does not need to be hung on the terminal input

2. For the later setsid service, because the process calling the setsid function cannot be the process group leader, if the child process is not fork, then the parent process is the process group leader, so setsid cannot be called. When the child process calls the setsid function, the child process is the session group leader and the process group leader, and is separated from the control terminal. At this time, no matter how the control terminal operates, the new process will not receive some signals to make the process exit. )

(2) the role of the second fork:

Although the contact with the terminal is currently closed, the terminal may be opened by mistake at a later stage. Only make sure that the session head process can open the terminal device, that is, fork again, exit the parent process, and once again, the child process of fork continues to run as a daemon, which ensures that the wizard process is not the first process of the dialogue period.

The second time is not necessary, it is optional, and some open source projects on the market are also fork one time.

These are all the contents of the article "how to implement daemons in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report