zoukankan      html  css  js  c++  java
  • 守护进程

    守护进程(daemon):

       是指在UNIX或其它多任务操作系统中在后台执行的电脑程序,并不会接收电脑用户的直接操作。此类程序会被进程的形式初始化。守护进程程序的名称通常以字母“d”结尾,例如:syslogd就是指管理系统日志的守护进程。

        通常,守护进程没有任何存在的父进程(即PPID=1),且在UNIX系统进程层次级中直接位于init之下。守护进程程序通常通过如下方式使自己成为守护进程:

         对一个子进程调用fork,然后使其父进程立即终止,使得这个子进程能在init下运行。这种方式通常被成为“脱壳”。

          系统通常在启动时一同启动守护进程。守护进程为对网络请求,硬件活动等进行响应,或其他通过某些任务对其他应用程序的请求进行回应提供支持。守护进程也能够对硬件进行配置(如在某些Linux系统上devfsd),运行计划任务(例如cron),以及运行其他任务。

        在原本的MAC OS系统中,此应用程序被成为“extensions”。而作为Unix-like的MAC OS X有守护进程。(在Mac OS X 中也有“服务”,但他们与Windows中类似的程序在概念上完全不相同)。

     删除守护进程 Kill -9 【pid】

    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/wait.h>
    #include<time.h>
    #include<signal.h>
    #include<sys/param.h>
    #include<sys/stat.h>
    
    void init_daemon(void);
    
    int main(void){
    FILE *fp;
    time_t t;
    init_daemon();
    while(1){
    sleep(10);
    if((fp=fopen("log.txt","a+"))>=0){
    t=time(0);
    fprintf(fp,"time:%s",asctime(localtime(&t)));
    fclose(fp);
     }
     }
     return 1;
    }
    
    void init_daemon(void){
    pid_t pid;
    int i;
    pid=fork();
    if(pid>0){
    exit(0);
    }else if(pid<0){
    exit(1);
    }else if(pid==0){
    setsid();
    chdir("/tmp");
    umask(0);
    for(i=0;i<NOFILE;++i){
    close(i);
    }
    return;
    }
    
    }
  • 相关阅读:
    设置sqlplus输出格式
    Using CrunchBase API
    Docker ( Is docker really better than VM ?)
    Cross platform GUI for creating SSL certs with OpenSSL
    PHP, Python Nginx works together!
    Your personal Mail Server iRedMail on ubuntu14.04 x64
    iphone/ipad/iOS on Linux Debian7/ubuntu12.04/linuxmint13/ubuntu14.04 compiling from source
    Internet Liberity -- a specific anonymous internet guide
    Organic Solar Cells
    Organic Solar Cells
  • 原文地址:https://www.cnblogs.com/sunwubin/p/3559567.html
Copyright © 2011-2022 走看看