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

     1 #include <stdio.h>
     2 #include <unistd.h>
     3 #include <errno.h>
     4 #include <time.h>
     5 #include <stdlib.h>
     6 
     7 int main()
     8 {
     9     pid_t pid;
    10     pid = fork();
    11     if (pid <0)
    12        {
    13            perror("fail to fork");
    14            exit(1);
    15        }  
    16     if (pid == 0)
    17        {
    18            setsid();//在子进程中创建新会话
    19            chdir("/tmp");//改变目录
    20            umask(0);//重设文件权限掩码
    21            int i;
    22            for (i = 0;i<getdtablesize();i++)
    23                {
    24                    close(i);//关闭所有打开的文件描述符
    25                }
    26            FILE *fp_w;
    27            time_t t;
    28            fp_w = fopen("time.log","a");
    29            if (fp_w == NULL)
    30                {
    31                     perror("fail to fopen fp_w");
    32                     exit(1);
    33 }
    34
    35 while (1) 36 { 37 time(&t);//获取系统时间的秒数 38 fprintf(fp_w,"%s ",ctime(&t));//将秒数转化为字符 39 sleep(1); 40 fflush(fp_w);//刷新 41 } 42 } 43 else 44 { 45 exit(0); 46 } 47 return 0; 48 }

     gcc编译之后 用ps ajx命令查看一下进程

  • 相关阅读:
    辗转相除法
    并查集(详)
    LCA 最近公共祖先
    RMQ ST表 静态区间最大值
    manacher
    题解 CF33B String Problem
    Linux 下对拍程序
    CSP 考试注意事项
    题解 P4688 [Ynoi2016]掉进兔子洞
    CSP 2020 游记
  • 原文地址:https://www.cnblogs.com/renchong/p/5613457.html
Copyright © 2011-2022 走看看