Title: DIY A daemon that can run in the background when the console window is closed.
Every second to insert a record into the My.Log file, the record format is as follows: Yyyy-mm-dd hh:mi:se record content, yyyy for the year, MM for the month, DD for days, HH for hours, mi for minutes, se for seconds.
#ifdef __cplusplus extern " C " {}#endif// write log function //path: Log file name //msg: Log information int writelog (constcharconst char * msg); #ifdef __ Cplusplus}#endif
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include"mylog.h"intMainintArgChar*args[]) {pid_t PID=0; PID=Fork (); if(pid>0) {exit (0); } if(pid==0) {setsid (); ChDir ("/"); Umask (0); Close (Stdin_fileno); Close (Stdout_fileno); Close (Stderr_fileno); intI=0; Charbuf[ -]={0}; while(1) {Sleep (1); sprintf (BUF,"fly on air%d\n", i++); Writelog ("/home/test/1/testlog.txt", BUF); memset (BUF,0,sizeof(BUF)); } } return 0;}
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include"mylog.h"intMainintArgChar*args[]) {pid_t PID=0; PID=Fork (); if(pid>0) {exit (0); } if(pid==0) {setsid (); ChDir ("/"); Umask (0); Close (Stdin_fileno); Close (Stdout_fileno); Close (Stderr_fileno); intI=0; Charbuf[ -]={0}; while(1) {Sleep (1); sprintf (BUF,"fly on air%d\n", i++); Writelog ("/home/test/1/testlog.txt", BUF); memset (BUF,0,sizeof(BUF)); } } return 0;}
. suffixes:.c. OCC=Gccsrcs=hello.cobjs=$ (srcs:.c=. O) EXEC=tecdstart:$ (OBJS) -L.-lmylog-o $ (EXEC) $ (objs )"^_^-----OK------^_^". C.O: -wall-g-o [email protected]-C $< clean: -F $ ( Objs)F $ (EXEC)
Linux Linux program Exercise IX