In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
I have learned a lot about the linux daemon in the past two days, and it is very important, so add a little note today.
1. The concept of process: a dynamic execution process of a program.
Processes exist in memory, creating, scheduling, executing and dying. Process number is the only symbol of a process. Each process has its own independent memory space. In a 32-bit operating system, a process has 0-4G memory space, of which 0-3G belongs to the user and 3G-4G belongs to the kernel, so the process execution mode appears: user mode and kernel mode.
2. Types of processes: interactive processes, batch processes, daemons
3, daemon: background service process in Linux, daemon process
4. The steps to create the daemon:
(1) create a child process and the parent process exits.
After this step, the child process becomes an orphan process (the parent process exits before the child process, and the child process becomes an orphan process and is adopted by the init process). Using the fork () function, if the return value is greater than 0, it is indicated as the parent process, exit (0), the parent process exits and the child process continues.
(2) create a new session in the child process, making the current process the leader of the new session group.
Using the setsid () function, if the current process is not the leader of the process group, a new session period is created for the current process, making the current process the first process of the session group and the leader of the process group.
(3) change current directory to root directory.
Because the daemon runs in the background, it starts when the system is turned on and ends when the system is closed, so change its directory to the root directory of the system. The file system of a process cannot be unmounted while it is executing.
(4) reset the file permission mask.
The process inherits the file creation mask from the parent process, so the daemon access limit may be modified, so to clear the file creation mask, umask (0)
(5) close the file descriptor.
The child process inherits the open file descriptor from the parent process. So you can turn it off using close.
In this case, the getdtablesize () function is used to get the number of files opened by this process.
Follow the steps above to complete the creation of the daemon.
The specific code is as follows. The function of the daemon is relatively simple to write to the file:
# include # include int main () {/ / 1 create a child process, and the parent process exits pid_t pid = fork (); if (pid0) {exit (0);} else {while (1) {/ / 2 leader setsid (); / / change path to root directory chdir ("/ tmp"); / / reset file mask umask (0) / / close the file descriptor int des=getdtablesize (); int iTuno; for
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.