Get the App
SLTechnology News&Howtos  ›  Servers  › 

Write daemons

Shulou Source: shulou.com Published: 2022-06-03 08:54:46 10月03日 Update

1) create a child process, and the parent process exits

2) create a new session in the child process

The setsid () function is used to create a new session and act as the leader of the session group. Calling the setsid () function serves three purposes:

Let the process out of the control of the original session

Let the process get rid of the control of the original process group

Let the process get rid of the control of the original control terminal

3) change the working directory with the chdir () function

4) set the file permission mask with umask ()

5) close the file descriptor

# include # include int main (int argc, const char * argv []) {pid_t pid; int iJournal FD; char * buf = "This is a Daemon log\ n"; pid = fork (); if (pid

< 0) { perror("fail to fork\n"); exit(0); } if(pid >

0) / / the parent process exits the first step {exit (0);} setsid (); / / the second step chdir ("/ tmp"); / / the third step umask (0); / / the fourth step for (I = 0 I < getdtablesize ()) / / the fifth step loops to delete the file descriptor {close (I) } while (1) {if ((fd = open ("daemon.log", O_CREAT | O_RDWR | Olympiad Appedition0667))

Tags: Process control function file three function permission directory group leader terminal builder new club Zhongchuang work cycle Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info MySQL Shulou Technology Docker