Linux Daemon Programming

Source: Internet
Author: User

Overview:
    • Daemon running in the background is also known as a "background service process." It is not the process of controlling the terminal with which it is connected. It is independent of the control terminal, the usual cycle of performing some kind of task.
    • The daemon is detached from the terminal in order to prevent the information in the process of execution from appearing on any terminal and the process will not be interrupted by any terminal information generated by any terminal.
    • Because in Linux, each system communicates with the user interface called the terminal, each process which starts from this terminal will depend on this terminal, this terminal is called these Process Control terminal.
    • When the control terminal is closed, the corresponding process will be automatically closed. But the daemon can break through this limitation, and it will be executed until the entire system shuts down.
    • Process group: is a collection of one or more processes. The process group is uniquely identified by the process group ID. In addition to the process number (PID), the Process group ID (GID) is also an essential attribute of a process. Each process has a leader process, and the process number of its leader process equals the process group ID. And the process group ID is not affected by the exit of the leader process.
    • Session Period: The session period is a collection of one or more process groups. Typically, a session starts at the user logon and terminates when the user exits, during which all the processes that the user runs are part of the session period.
    • Control Terminal: Because in Linux, each system communicates with the user interface called the terminal, every process which starts from this terminal will depend on this control terminal.
features of the daemon:
    1. The most important feature of the daemon is running in the background.
    2. The daemon must be isolated from the environment before it is run. These environments include file descriptors that are not closed, control terminals, sessions and process groups, working directories that have file creation masks, and so on. These environments are usually inherited from the parent process by the daemon.
    3. The daemon is started in a way that is special: it can be started from the startup script/ETC/RC.D when the Linux system starts, can be started by the job planning process Crond, and can be performed by the user terminal (usually the shell).
Daemon Process Programming Process:

1. Create child process, parent process exits

2. Get rid of the current session, create a new conversation group, the child process becomes the leader

3. Close the file description symbol

4. Setting the file mask

5. Set up working directory

Note: Sometimes the SIGCHLD signal signal (SIGCHLD, sig_ign) is processed, and the zombie process is prevented (zombie).

Below you can add anything you want to daemon do.

Example:

1#include <stdio.h>2#include <string.h>3#include <string.h>4#include <stdlib.h>5#include <sys/types.h>6#include <sys/stat.h>7#include <sys/wait.h>8 9 Ten intMainintargcConst Char*argv[]) One { A     intret; -     inti; -     Char* BUF ="Hello World"; the pid_t pid; -FILE *FP; -          -PID =fork (); +     if(PID <0){ -Perror ("Fork"); +         return-1; A     } at     Else if(PID >0){ -Exit0);//Parent Process Exits -     } -  -  -Setsid ();//let the child process become the leader and create a new session and detach from the current terminal in  -  to //turn off the standard input stream +      for(i=0;i<3; i++){ - Close (i); the     } *  $ //change the working directory to the root directory (optional)Panax NotoginsengChDir"/"); -  the //Set file mask (optional) +Umask0); A  the  + //The daemon writes content to a file for a period of time -      while(1){ $      $fp = fopen ("Daemon.log","A +"); -         if(!FP) { -             return-1; the         } - Wuyiret = fwrite (buf,1, strlen (BUF), FP); the         if(Ret <0){ -             return-1; Wu         } -  About fclose (FP); $  -Sleep5); -     } -  A     return 0; +}

Linux Daemon Programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.