文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Example of writing a daemon process

Example of writing a daemon process

时间:2010-10-09  来源:djstava

In UNIX and other multitasking operating systems, a daemon is a computer program that runs in the background, rather than under the direct control of a user; they are usually initiated as background processes. Typically daemons have names that end with the letter "d": for example,httpd, the daemon that handles the HTTP requirements, or sshd, which handles incoming SSH connections.

In a Unix environment, the parent process of a daemon is often (but not always) the init process (pid=1). Processes usually become daemons by forking a child process and then having their parent process immediately exit, thus causing init to adopt the child process. This is a somewhat simplified view of the process as other operations are generally performed, such as dissociating the daemon process from any controlling tty.

/*daemon.c*/

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

void initDaemon(void)
{
    int pid;
    int i;

    /* terminate parent process */
    if(pid=fork())
        exit(0);
   
    /* exit when fork failed */
    else if(pid< 0)
        exit(1);

    /* continue only when it it the first child process
    ** run in a new session
    */
    setsid();

    /* seperate from terminator,kill the first child process */
    if(pid=fork())
        exit(0);
    else if(pid< 0)
        exit(1);

    /* close all file descriptors */
    for(i=0;i< NOFILE;++i)
        close(i);
    chdir("/home/djstava");

    /* reset file umask */
    umask(0);
    return;
}

void signalUSR1(int signal)
{
    /*handle SIGUSR1*/
    FILE *fp;
    time_t t;
    if((fp=fopen("log","a")) >=0)
    {
        t=time(0);
        fprintf(fp,"Received signal(%d),at %s\n", signal, asctime(localtime(&t)) );
        fclose(fp);
    }
}


int main(int argc, char ** argv)
{
    FILE *fp;
    time_t t;
    initDaemon();

    /* ignore signal */
    signal(SIGCHLD, SIG_IGN);

    /* handle SIGUSR1 */
    signal(SIGUSR1, signalUSR1);

    while(1)
    {
        sleep(60);
        if((fp=fopen("log","a")) >=0)
        {
            t=time(0);
            fprintf(fp,"djstava at %s\n",asctime(localtime(&t)) );
            fclose(fp);
        }
    }
}


Now compile the example and execute it like this.

gcc -o daemon daemon.c

./daemon

ps ax

sudo kill -s SIGUSR1 6942

cat /home/djstava/log


Finally,add a line to the file /etc/rc.local,and cp the executable file to the dir /bin.That's down,daemon will run when the system boots.

su - djstava -c "/bin/daemon"


References:

Unix Signals

SIGHUP 1 Exit Hangup
SIGINT 2 Exit Interrupt
SIGQUIT 3 Core Quit
SIGILL 4 Core Illegal Instruction
SIGTRAP 5 Core Trace/Breakpoint Trap
SIGABRT 6 Core Abort
SIGEMT 7 Core Emulation Trap
SIGFPE 8 Core Arithmetic Exception
SIGKILL 9 Exit Killed
SIGBUS 10 Core Bus Error
SIGSEGV 11 Core Segmentation Fault
SIGSYS 12 Core Bad System Call
SIGPIPE 13 Exit Broken Pipe
SIGALRM 14 Exit Alarm Clock
SIGTERM 15 Exit Terminated
SIGUSR1 16 Exit User Signal 1
SIGUSR2 17 Exit User Signal 2
SIGCHLD 18 Ignore Child Status
SIGPWR 19 Ignore Power Fail/Restart
SIGWINCH 20 Ignore Window Size Change
SIGURG 21 Ignore Urgent Socket Condition
SIGPOLL 22 Ignore Socket I/O Possible
SIGSTOP 23 Stop Stopped (signal)
SIGTSTP 24 Stop Stopped (user)
SIGCONT 25 Ignore Continued
SIGTTIN 26 Stop Stopped (tty input)
SIGTTOU 27 Stop Stopped (tty output)
SIGVTALRM 28 Exit Virtual Timer Expired
SIGPROF 29 Exit Profiling Timer Expired
SIGXCPU 30 Core CPU time limit exceeded
SIGXFSZ 31 Core File size limit exceeded
SIGWAITING 32 Ignore All LWPs blocked
SIGLWP 33 Ignore Virtual Interprocessor Interrupt for Threads Library
SIGAIO 34 Ignore Asynchronous I/O


djstava

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载