<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); > When I was developing Android, I encountered a service component that simply would not require the interface behind the process to silently perform the task. Now let's learn how the daemon service is implemented under Linux, and also learn the switches that control processes through shell scripts. </span>
Daemon implementation has the default mentality fork parent-child process, shut down the parent process, the child process with the console out of the operation, my side of the Daemon demo implementation is through the signal to control the output of the daemon process. First look at the code:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys /types.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h > #include <sys/ipc.h> #include <sys/shm.h> #include <signal.h> #include <syslog.h> #include
<errno.h> int mysignal (int signo,void (*FUNC) (int)) {struct sigaction act,oact;
Act.sa_handler = func;
Sigemptyset (&act.sa_mask);
act.sa_flags = 0;
Return sigaction (signo,&act,&oact);
} void Setdaemon () {pid_t pid,sid;
PID = fork ();
if (PID < 0) {printf ("Fork Failuer:%s\n", Strerror (errno));
Exit (Exit_failure);
} if (pid > 0) {exit (exit_success);
if (SID = Setsid ()) < 0) {printf ("Set SID failure:%s\n", Strerror (errno));
Exit (Exit_failure);
} void Listenfifo () {const char* filename = "Shaofifo";
int len = 0; Char buf[1024];
memset (buf,0,sizeof (BUF));
int fd = open (filename,o_rdonly);
if (fd = = 1) {printf ("Open failure:%s\n", Strerror (errno));
Exit (Exit_failure);
Len = Read (fd,buf,sizeof (BUF));
if (Len > 0) {if (Buf[strlen (BUF)-1] = = ' \ n ') {Buf[strlen (BUF)-1] = 0;
Close (Stdout_fileno);
Open (buf,o_wronly);
} void catch_signal (int signo) {switch (signo) {case Sigint:listenfifo ();
Break
int main (int arg,char* args[]) {Setdaemon ();
Mysignal (sigint,catch_signal);
while (1) {puts ("Hello World!!");
Sleep (1);
return exit_success;
}
Then we enter another console's device path in the pipe file, and the subprocess is the daemon shuts down the standard output and then receives the signal to read the device path in the pipe file, open the device, and continue the output
In the Shaofifo pipeline file input device path, the child process reads the output content here is mainly to learn the implementation of the Daemon Setsid () method is to let the child process out of the console
The general daemon needs to be controlled by a shell script, look at the implementation of the shell script
#! /bin/sh
whoami= ' WHOAMI '
pid= ' ps-u $WHOAMI | grep Service | awk ' {print '} '
if (test ' $ ' = ' start ') then
if (test "$PID" = "") then
./service
fi
fi
if (test "=" Stop ") then
if (test" $PID "!=" ") then
kill $PID
fi
fi
if (test "$" = "status") then
if (test "$PID" = "") then
echo "not run"
fi< C16/>if (test "$PID"!= "") then
echo "Run"
fi
fi
Over here. The implementation of the daemon through shell scripting ends here.