Creation of Daemons under Linux

Source: Internet
Author: User
Tags syslog rsyslog

Recently Learning Linux C programming

I saw the creation of the daemon, it was fun,

Test environment Ubuntu 15.04

The test code is posted below

#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<signal.h>#include<sys/param.h>#include<sys/stat.h>#include<time.h>#include<syslog.h>intInit_pro (void ){    intpid//Process Number    inti; /*Ignore terminal IO signal, stop signal*/signal (Sigttou, sig_ign);    Signal (Sigttin, sig_ign);    Signal (SIGTSTP, sig_ign);    Signal (SIGHUP, sig_ign); /*open a new process under the current process*/PID=Fork (); //if pid>0 returns the process ID of the parent process, ends the parent process    if(PID >0) {exit (0);//ends the parent process, making the child process a background process    }    Else    //if the PID is less than 0, the child process fails to turn back    if(PID <0 )    {        return-1; }    //Create a new process group in which the child process becomes the first process in the process, leaving the process out of all endpointsSetsid (); //once again, create a new sub-process by fork, which is to create a new subprocess under the sub-process you just created.//The goal is to ensure that the process is not the process leader and that the process cannot open a terminalPID =Fork (); if(PID >0) {exit (0);//if the PID of the parent process (that is, the previous subprocess) is returned then end the process    }    Else    if(PID <0 )    {        return-1; }    //What is Nofile    //Close all file descriptors that are not required to inherit from the parent process//What isFile Descriptor     for(i =0; i < Nofile; Close (i++) ); //change the working directory so that the process does not contact any file system//There's actually only one last open subprocess left.ChDir"/"); //Set the file screen Word to 0Umask0); //Ignoring SIGCHLD signalssignal (SIGCHLD, sig_ign); return 0;}intMainintargcChar**argv)    {time_t now;    Init_pro (); Syslog (Log_user| Log_info,"Test Daemon! \ n");  while(1)    {        /*Code*/Sleep (8); Time (&Now ); Syslog (Log_user| Log_info,"system time: \t%s\t\t\n", CTime (&Now )); }}

Note that before using syslog, you first need to configure the/etc/rsyslog.conf file to add User*/var/log/test.log at the end of the file

Then restart the Rsyslog service

Compile run

When we open the Test.log, we'll find that every 8 seconds, he ticks back.

The use of Ps-ef can be observed

Creation of Daemons under Linux

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.