In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly analyzes the relevant knowledge points of how to understand the Linux system daemon, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "how to understand the Linux system daemon".
What is a daemon? For example, Taobao and Alipay must run 24 hours a day, which is a very typical daemon process.
1. View the processes in the system
Ps axj1 parameter a lists not only the processes of the current user, but also those of all other users, parameter x lists not only the processes of the control terminal, but also the processes without the control terminal, and parameter j lists the information related to job control.
Any TPGID column that says-1 is a process that does not control the terminal, that is, a daemon. In the COMMAND column, kernel threads are represented by the names enclosed in []. These threads are created in the kernel and have no user-space code, so there is no program file name and command line, usually with a name that begins with k, indicating Kernel. We are already familiar with the init process. Udevd is responsible for maintaining the device files in the / dev directory, acpid is responsible for power management, and syslogd is responsible for maintaining the log files under / var/log. You can see that daemons often use the name ending with d to indicate Daemon.
2. Setsid function 1 > the most critical step in creating a daemon is to call the setsid function to create a new Session and become a Session Leader.
# include pid_t setsid (void); 12 return value: the id of the newly created Session (that is, the id of the current process) is returned when the function is called successfully, and-1 is returned if there is an error.
2 > it is important to note that the current process is not allowed to be the Leader of the process group before calling this function, otherwise the function returns-1. Solution: first fork and then call setsid,fork to create the child process and the parent process in the same process group, the Leader of the process group must be the first process of the group, so the child process can not be the first process of the group, there will be no problem in calling setsid in the child process.
3 > the result of a successful call to the function is:\ 1. Create a new Session, the current process becomes Session Leader, and the id of the current process is the id of Session. \ 2. Create a new process group. The current process becomes the Leader of the process group, and the id of the current process is the id of the process group. \ 3. If the current process originally has a control terminal, it 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 is only an ordinary open file rather than a control terminal.
3. Step 1 of creating a daemon > call umask to set the file mode creation mask to 0.
Umask (0); / / umask must be cleared 0, otherwise the creation of a file is affected by the default permissions of the system. 1 the file permission mask is to mask the corresponding bits in the file permissions. Because the newly created child process using the fork () function inherits the file permission mask of the parent process, this brings a lot of trouble for the child process to use the file (for example, the file in the parent process does not have permission to execute the file, but there will be a problem when the child process wants to execute the corresponding file). Therefore, it is necessary to set the permission mask of the file to 0 in the child process, that is, to have the maximum permissions at this time, which can greatly enhance the flexibility of the daemon.
2 > call fork, and the parent process exits (exit). Reason: 1) if the daemon is started as a simple shell command, then the termination of the parent process makes shell think that the command has been executed. 2) ensure that the child process is not the leader process of a process group.
3 > call setsid to create a new session. Setsid causes: 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. (fork again to ensure the daemon process, and then do not open the tty device)
The reason for calling setsid: because the first step in creating a daemon is to call the fork () function to create the child process, and then exit the parent process. Because when the fork () function is called, the child process copies the parent process's session period, process group, control terminal and other resources, although the parent process exits, but the session period, process group, control terminal, etc., have not changed, therefore, it is necessary to use setsid () Korean to completely separate the child process, thus getting rid of the control of other processes.
4 > change the current working directory to the root directory. Prevents a directory from being deleted in the current directory, causing the daemon to be invalid. The child process created with fork () inherits the current working directory of the parent process, because the file system where the current directory resides cannot be unmounted while the process is running, which will cause a lot of trouble for future use. So the usual practice is to make "/" the current directory of the daemon, and of course you can specify another directory as the working directory of the daemon.
5 > close file descriptors that are no longer needed. Like the file permission code, a child process created with the fork () function inherits some open files from the parent process. The files opened by these files may never be read or written by the daemon, and if they are not closed, the system resources will be wasted, resulting in the file system on which the process can not be unmounted and causing expected errors.
Such as: close the standard input stream, the standard output stream, the standard error stream.
Close (0); close (1); close (2); 1236 > others: ignore SIGCHLD signal.
Signal (SIGCHLD,SIG_IGN); 14. Create your own daemon:
1 / * * 2 * document description: mydaemon.c 3 * author: Duan Xiaoxue 4 * creation time: Wednesday, November 20, 2024, 12:26:43 5 * Development environment: Kali Linux/g++ v6.3.0 6 * * * / 7 8 # include 9 # include 10 # include 11 # include 12 # include 13 # include 14 15 void mydaemon () 16 {17 umask (0) / / set the file mode creation mask word to 0 18 pid_t pid = fork (); / / create child process 19 if (pid = =-1) 20 perror ("fork error"); 21 else if (pid = = 0) / / child 22 {23 setsid () / / create a new session 24 if (chdir ("/") ps axj | grep mydaemon to view the wizard process: 5. Call the system function daemon to create a daemon # include int daemon (int nochdir, int noclose); the daemon function is mainly used for programs that want to leave the console and run in the background in the form of a daemon. 2 > when nochdir is 0, daemon changes the directory of the current process to the root ("/") directory. 3 > when noclose is 0, daemon redirects all STDIN,STDOUT,STDERR of the process to / dev/null. For the black hole under / dev/null:linux, all data written will be discarded directly. Create a daemon with the daemon function: 1 / * * 2 * File description: daemon.c 3 * author: Duan Xiaoxue 4 * creation time: Friday, November 22, 2024 06:53:52 5 * Development environment: Kali Linux/g++ v6.3.0 6 * * * / 7 8 # include 9 # include 10 11 int main () 12 {13 daemon (1) / / create a daemon 14 while (1); 15 return 0; 16} 12345678910111213141516 3. How to kill a daemon? 1 > use the name of the ps axj | grep daemon to find the corresponding daemon, and then use the kill-9 process number to kill the corresponding process. 2 > use the ps-ef command to find the corresponding daemon, and then use the kill command to kill it. …… 3 > you can also create shell scripts to automatically manage the startup, shutdown and restart of the process. 4. Why would someone create a daemon to fork twice? A common implementation of a daemon function: int daemon (void) {pid_t pid = fork (); / / first fork if (pid! = 0) exit (0); / / parent / / first children if (setsid () = =-1) {printf ("setsid failed\ n"); assert (0); exit (- 1) } umask (0); pid = fork (); / / second fork if (pid! = 0) exit (0); / / second children chdir ("/"); for (int I = 0; I can see that I fork twice in the above code, although this is not necessary, but it does make some more optimized operations for the daemon. First, the first fork: the function of the first fork here is to make shell think that this command has been terminated and does not need to be hung on the terminal input; another is for the later setsid service, because the process that calls the setsid function cannot be the process group leader (will report an error Operation not permitted), if the child process is not forked, then the parent process is the process group leader and cannot call setsid. So here the subprocess becomes the leader of a new session group. The second fork: the second fork opens the terminal again in order to avoid misoperation in the later process. Because the prerequisite for opening a control terminal is that the process must be the session group leader, and through the second fork, we ensure that the child process out of the second fork will not be the session group leader. The following is a list of the signals generated by the control terminal. As long as these signals are properly processed in the program, the purpose of the above function can also be achieved. / / background process reading / writing terminal input produces the following two signals, or controlling the absence of terminal reading and writing will generate signal (SIGTTOU, SIG_IGN); signal (SIGTTIN, SIG_IGN); 12pm / press CTRL-C, CTRL-\ CTRL-Z will send the following signals to the foreground process group: signal (SIGINT, SIG_IGN); signal (SIGQUIT, SIG_IGN); signal (SIGTSTP, SIG_IGN) 123Universe / terminal disconnect, will send the following signal signal (SIGHUP, SIG_IGN) to the session leader or all members of the orphan process group; 1 there are some signals that can also be generated by the terminal shell, need to pay attention to signal (SIGCONT, SIG_IGN); signal (SIGSTOP, SIG_IGN); 12 above these signals, there should be some programs default processing (SIG_DFL) itself is to ignore (SIG_IGN), not to exit the process. But it won't cause any problem according to the above. At this point, the details of the daemon of the Linux system are shared. You are welcome to leave a message in the comments area. The above is the Linux system-related content shared by Liangxu tutorial Network for all friends. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! This is the end of the introduction on "how to understand the Linux system daemon". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!
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.