File IO and process creation daemon (c language)

         Summary: Process-related knowledge points

        There are three types of processes: ①interactive process, ②batch process, ③daemon process

Interactive process: related to the terminal, it can be run in the foreground or in the background, close the terminal, and the process ends.

Batch process: It has nothing to do with the terminal, and the specified process can be placed in a work queue to be executed in order, generally managed by the system.

Daemon: It has nothing to do with the terminal, and keeps certain events in the background or waits for certain event responses. For example, when we insert a USB flash drive into our computer, the computer will recognize it immediately. We have not done any operation on the program of recognizing the USB flash drive. It is always running in the background, which is the daemon process.

What I want to talk about today is how to create a daemon process. There are generally six steps in the creation of a daemon process, which are:

1) Create a process (fork() function), and the parent process exits (exit() function) to make the child process an orphan process and let it run in the background;

2) Let the child process leave the original session (setsid() function) (meaning it can be executed even if the terminal is closed);

3) Modify the current working path (chdir() function, this step is not necessary, but it is recommended to execute);

4) Reset the file permission mask (umask(0) function "0" means whatever the original permission is, this step is not necessary, but it is recommended to execute it)

5) Delete all file descriptors in the process (the getdtablesize() function returns the highest file descriptor, which can be deleted in a loop), in order to make the daemon process more stable.

6) Use the while(1) loop to execute the child process.

        What I will demonstrate to you today is the creation of a daemon process. The example is: create a daemon process and record the current time to time.log every second. code show as below

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <wait.h>

int main(int argc, char *argv[])
{
    pid_t pid = fork();  //创建子进程
    if(pid<0) //判断子进程是否创建成功
    {   
        perror("fork");
        exit(0);
    }
    if(pid>0) //父进程
    {
        exit(0);//退出父进程,让子进程变为孤儿进程。
    }
    else  //子进程
    {
       setsid();  //让子进程脱离原本会话
     */  chdir("/tmp"); //修改当前工作路径
         umask(0); // 重设文件权限掩码
         当然,修改完路径之后对应的日志文件也要拿到/tmp下面去,不然进程找不到文件,
         我这里只做个示范,就不修改路径了,直接在当前路径执行。   
     */
       int i =0;
       for(i=0;i<getdtablesize();i++)  //删除进程中所有的文件描述符
       {
            close(i);
       }
       FILE *fp = fopen("time.log","a+"); //打开日志文件
       if(fp==NULL)
       {
            perror("fopen");
            exit(0);
       }
       while(1)  //循环写入当前时间
       {
            time_t my_t;
            time(&my_t);  //获取当前时间(秒数)
            struct tm *t = localtime(&my_t); //调用函数获取当前时间(年月日时分秒)
            int num[6]={t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec};//时间装入数组
            fprintf(fp,"%2d年 %2d月 %2d日 %02d:%02d:%02d\n",num[0],num[1],num[2],num[3],num[4],num[5]);//写入日志文件
            sleep(1); //每隔一秒写入一次
       }
    }

    return 0;
}

operation result:

Even if the terminal is closed, the child process is still running, and the process status can be viewed through the ps -ef command;

 

        This is the end of today's sharing. The author is also a novice, welcome to correct me if I am wrong, and thank you grandpas for watching.

Guess you like

Origin blog.csdn.net/weixin_56187542/article/details/126233393