关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming
作者 佚名技术
来源 操作系统
浏览
发布时间 2012-06-27
http://www.enderunix.org/documents/eng/daemon.php Unix Daemon Server Programming Introduction Unix processes works either in foreground or background. A process running in foreground interacts with the user in front of the terminal (makes I/O), whereas a background process runs by itself. The user can check its status but he doesn''t (need to) know what it is doing. The term ''daemon'' is used for processes that performs service in background. A server is a process that begins execution at startup (not neccessarily), runs forever, usually do not die or get restarted, operates in background, waits for requests to arrive and respond to them and frequently spawn other processes to handle these requests. Readers are suppossed to know Unix fundamentals and C language. For further description on any topic use "man" command (I write useful keywords in brackets), it has always been very useful, trust me :)) Keep in mind that this document does not contain everything, it is just a guide. 1) Daemonizing (programming to operate in background) [fork] First the fork() system call will be used to create a copy of our process(child), then let parent exit. Orphaned child will become a child of init process (this is the initial system process, in other words the parent of all processes). As a result our process will be completely detached from its parent and start operating in background. i=fork(); if (i<0) exit(1); /* fork error */ if (i>0) exit(0); /* parent exits */ /* child (daemon) continues */ 2) Process Independency [setsid] A process receives signals from the terminal that it is connected to, and each process inherits its parent''s controlling tty. A server should not receive signals from the process that started it, so it must detach itself from its controlling tty. In Unix systems, processes operates within a process group, so that all processes within a group is treated as a single entity. Process group or session is also inherited. A server should operate independently from other processes. setsid() /* obtain a new process group */ This call will place the server in a new process group and session and detach its controlling terminal. (setpgrp() is an alternative for this) 3) Inherited Descriptors and Standart I/0 Descriptors [gettablesize,fork,open,close,dup,stdio.h] Open descriptors are inherited to child process, this may cause the use of resources unneccessarily. Unneccesarry descriptors should be closed before fork() system call (so that they are not inherited) or close all open descriptors as soon as the child process starts running. for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */ There are three standart I/O descriptors: standart input ''stdin'' (0), standart output ''stdout'' (1), standart error ''stderr'' (2). A standard library routine may read or write to standart I/O and it may occur to a terminal or file. For safety, these descriptors should be opened and connectedthem to a harmless I/O device (such as /dev/null). i=open("/dev/ |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: 2010年企业Linux应用趋势下一篇: 招兼职solaris讲师
关于关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming的所有评论