如何-用C语言实现守护进程 [英] How to - Daemon process in C language
问题描述
你好,
如何创建守护程序"进程?此外,该守护进程还将创建3个新进程.(用C语言)
我已经阅读了一些有关它的文章,但是都没有一个有用的源代码示例.
你能帮我吗?
Tks
UNIX的实际规则有些复杂,距离我自己做这件事已有一段时间了.据我所知,主程序应该以分叉开始,然后父程序应该终止.然后,子进程需要更改某些设置,例如:
chdir(" ); // 切换到用户目录之外 setsid(); // 成为会话负责人 umask( 0 ); // 清除文件创建掩码
现在,此过程成为会话负责人,并且应该派生其他与之通信的子代.
我建议您检查上面三个命令的手册页,以验证我的建议是正确的.
如果您还没有的话,建议您购买 W的副本.理查德·史蒂文斯(Richard Stevens)的 [ ^ ]书 UNIX环境中的高级编程.它包含诸如此类的出色示例.</blockquote>
Linux守护程序编写方法
:)
claudiatita写道:如何创建守护进程"?
blockquote>
这取决于什么平台,以及您期望它做什么.在大多数现代操作系统上,用户创建的守护程序必须遵循一些相当复杂的规则,以免破坏系统.也许您可以阐明您希望实现的目标.
Hi there,
How do i do to create a "daemon" process? Furthermore, this daemon process should create 3 new processes.(In C language)
I''ve already read some articles about it, but none of them have some useful source code example.
Can you help me?
Tks
The actual rules for UNIX are a little complex, and it is some time since I have done this myself. As far as I recall, the main program should start by forking, and the parent should then terminate. The child process then needs to alter some settings, something like:
chdir("/"); // switch away from user directory setsid(); // become session leader umask(0); // clear file creation mask
This process now becomes the session leader and should fork the other children which will communicate back to it.
I suggest you check the man pages for the three commands above to verify that my suggestion is correct.
If you do not already, I would suggest buying a copy of W. Richard Stevens''s[^] book Advanced Programming in the UNIX Environment. It contains excellent examples of subjects such as this.
Linux Daemon Writing HOWTO
:)
claudiatita wrote:How do i do to create a "daemon" process?
It depends on what platform, and what you expect it to do. On most modern operating systems user created daemons have to follow some fairly complex rules in order not to disrupt the system. Perhaps you could clarify what it is you hope to achieve.
这篇关于如何-用C语言实现守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!