Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2122000
  • 博文数量: 229
  • 博客积分: 7217
  • 博客等级: 上校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-19 17:23
个人简介

个人主页https://xugaoxiang.com,微信公众号: Dev_Club 或者搜索 程序员Club

文章分类

全部博文(229)

文章存档

2017年(1)

2016年(20)

2015年(23)

2013年(1)

2012年(23)

2011年(68)

2010年(62)

2009年(31)

分类: LINUX

2010-10-09 15:28:08

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
#include
#include
#include
#include
#include
#include

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

阅读(2273) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-10-10 15:38:33

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com