c语言中如何启动新进程,使用stop创建一个守护进程,在C中启动功能

如何为此守护程序代码添加守护程序停止,启动和报告功能?

#include

#include

#include

#include

#include

#include

#include

#include

#include

int main(void) {

/* Our process ID and Session ID */

pid_t pid, sid;

/* Fork off the parent process */

pid = fork();

if (pid < 0) {

exit(EXIT_FAILURE);

}

/* If we got a good PID, then

we can exit the parent process. */

if (pid > 0) {

exit(EXIT_SUCCESS);

}

/* Change the file mode mask */

umask(0);

/* Open any logs here */

/* Create a new SID for the child process */

sid = setsid();

if (sid < 0) {

/* Log the failure */

exit(EXIT_FAILURE);

}

/* Change the current working directory */

if ((chdir("/")) < 0) {

/* Log the failure */

exit(EXIT_FAILURE);

}

/* Close out the standard file descriptors */

close(STDIN_FILENO);

close(STDOUT_FILENO);

close(STDERR_FILENO);

/* Daemon-specific initialization goes here */

/* The Big Loop */

while (1) {

/* Do some task here ... */

sleep(30); /* wait 30 seconds */

}

exit(EXIT_SUCCESS);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值