Daemon and daemon output

Source: Internet
Author: User
Tags terminates

1 Creating an output program2 Creating a daemon
1 Creating an output daemon does not contact the terminal, so you need to create another program for the output. You can also use/bin/echo directly
-----example_daemon_help.cc
#include <stdio.h>int main (int argc, char** argv) {     if (argc = = 1)     {          printf ("only one parameter \ n"); 
   }     Else if (argc = = 2)     {          printf ("%s \ n", argv[1]);     }     else     {          printf ("too more parameter:%d\n", argc);     }     return 0;}



Compile:
g++-o example_daemon_help example_daemon_help.cc--std=c++11
2 Creating a Daemon
Write a function First: int daemon_init (const char* pname, int facility), and then, as soon as the process name is passed into this function, it becomes the daemon.
-----from the 13th chapter of UNP, with changes
int Daemon_init (const char* pname, int facility) {umask (0);     pid_t pid;     if (PID = fork ()) < 0) return-1; else if (PID > 0) _exit (0);      Parent terminates//child 1 continues//become session leader if (Setsid () < 0) return-1;     Ignore SIGHUP//Signal (SIGHUP, sig_ign);     struct Sigaction sa;     Sa.sa_handler = sig_ign;     Sigemptyset (&sa.sa_mask);     sa.sa_flags = 0;     if (Sigaction (SIGHUP, &sa, nullptr) < 0) _exit (0);     if (PID = fork ()) < 0) return-1;     else if (PID > 0) _exit (0);     Child 1 terminates//child 2 continues helpguy::d aemon_flag = 1;     Changes working directory ChDir ("/");     Close off file descriptors Rlimit RL;     if (Getrlimit (Rlimit_nofile, &AMP;RL) < 0) return-1; int Fdmax = Rl.rlim_max = = rlim_infinity?     1024:rl.rlim_max;     for (int i = 0; i < Fdmax; ++i) Close (i);REDIRECT stdin, stdout and stderr to/dev/null open ("/dev/null", o_rdonly);     Open ("/dev/null", O_RDWR);     Open ("/dev/null", O_RDWR);     Openlog (PName, log_pid, facility); return 0;}

About _exit See: http://blog.csdn.net/alex_my/article/details/39318607
Here is the entire program code:
-----example_daemon.cc
#include "helpguy.h"  //contains Daemon_init function # include <syslog.h>int main (int argc, char** argv) {     //daemon Process     Daemon_init (argv[0], 0);     int count = 0;     while (++count <= 2)     {          sleep (3);                   Notifice          pid_t pid;          if (PID = fork ()) = = 0)          {               ///child process inherits the parent process's file descriptor               //In Daemon_init, the parent process's stdout is redirected to/dev/null               close (1);               Open ("/dev/pts/0", o_wronly);                             EXEC               execl ("/home/alex_my/code/unp/example_daemon_help", "Example_daemon_help", "3 sec Now", nullptr);               You can also use this               //execl ("/bin/echo", "echo", "Clock 3", nullptr);          }                   Parent          waitpid (PID, nullptr, 0);     }     return 0;}

Daemon_init can be placed in this file. Compilation: g++-o example_daemon example_daemon.cc--std=c++11
There is use to nullptr, so add--std=c++11, note that the equal sign does not have spaces on either side
I put it in the static library file of Libhelpguy.a. Compilation: g++-O example_daemon example_daemon.cc-l.-lhelpguy--std=c++11
About creating a static library see http://blog.csdn.net/alex_my/article/details/43529323

About EXECL See: http://blog.csdn.net/alex_my/article/details/39374447

Daemon and daemon output

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.