C语言编写linux下的守护进程
利用
下载的这段代码,成功实现了守护进程,原来守护进程是很简单的事情。
在main函数中执行
init_daemon();//初始化为Daemon
就可以把进程变成守护进程
#include
#include
#include
#include
#include
void
init_daemon(void
)
{
int
pid;
int
i;
if
(pid=fork())
exit
(0);//是父进程,结束父进程
else
if
(pid< 0)
exit
(1);//fork失败,退出
//是第一子进程,后台继续执行
setsid();//第一子进程成为新的会话组长和进程组长
//并与控制终端分离
if
(pid=fork())
exit
(0);//是第一子进程,结束第一子进程
else
if
(pid< 0)
exit
(1);//fork失败,退出
//是第二子进程,继续
//第二子进程不再是会话组长
for
(i=0;i< NOFILE;++i)//关闭打开的文件描述符
close
(i);
chdir("/tmp"
);//改变
工作目录到/tmp
umask(0);//重设文件创建掩模
return
;
}
相关文档:
理解c中的序列点
http://blog.chinaunix.net/u1/42826/showart_364176.html
让我们来看看下面的代码:
int i=7;
printf(”%d\n”, i++ * i++);
你认为会返回什么?56?no。正确答案是返回 49?很多人会问为什么?难道不该打印出56吗?在ccfaq中有非常详尽的解释,根本原因在于c中的序列 ......
C/C++: 十六进制转10进制源码
收藏
view plain
copy to clipboard
print
?
int
hex_char_value(
char
c)
{
if
(c >=
'0'
&& ......
@echo off
@echo ╔┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉┉╗
@echo ┋ 心 如 止 水 ┋
@echo ┋ ┋
@echo ┋ DIY 个 性 BAT C 盘 防 毒 批 处 理 ┋
@echo ┋ ......
C/C++ optimizing compilers are great--but there *are* a few techniques for hand-tuning your code to run as efficiently as possible on the AMD Athlon64 and Opteron processors, especially when developing DLLs, device drivers, or other performance-bound pieces of code.
Alan Zeichick
Share | ......