关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming
作者 佚名技术
来源 操作系统
浏览
发布时间 2012-06-27
null",O_RDWR); /* open stdin */ dup(i); /* stdout */ dup(i); /* stderr */ As Unix assigns descriptors sequentially, fopen call will open stdin and dup calls will provide a copy for stdout and stderr. 4) File Creation Mask [umask] Most servers runs as super-user, for security reasons they should protect files that they create. Setting user mask will pre vent unsecure file priviliges that may occur on file creation. umask(027); This will restrict file creation mode to 750 (complement of 027). 5) Running Directory [chdir] A server should run in a known directory. There are many advantages, in fact the opposite has many disadvantages: suppose that our server is started in a user''s home directory, it will not be able to find some input and output files. chdir("/servers/"); The root "/" directory may not be appropriate for every server, it should be choosen carefully depending on the type of the server. 6) Mutual Exclusion and Running a Single Copy [open,lockf,getpid] Most services require running only one copy of a server at a time. File locking method is a good solution for mutual exclusion. The first instance of the server locks the file so that other instances understand that an instance is already running. If server terminates lock will be automatically released so that a new instance can run. Recording the pid of the running instance is a good idea. It will surely be efficient to make ''cat mydaamon.lock'' instead of ''ps -ef|grep mydaemon'' lfp=open("exampled.lock",O_RDWR|O_CREAT,0640); if (lfp<0) exit(1); /* can not open */ if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */ /* only first instance continues */ sprintf(str,"%d\n",getpid()); write(lfp,str,strlen(str)); /* record pid to lockfile */ 7) Catching Signals [signal,sys/signal.h] A process may receive signal from a user or a process, its best to catch those signals and behave accordingly. Child processes send SIGCHLD signal when they terminate, server process must either ignore or handle these signals. Some servers also use hang-up signal to restart the server and it is a good idea to rehash with a signal. Note that ''kill'' command sends SIGTERM (15) by default and SIGKILL (9) signal can not be caught. signal(SIG_IGN,SIGCHLD); /* child terminate signal */ The above code ignores the child terminate signal (on BSD systems parents should wait for their child, so this signal should be caught to avoid zombie processes), and the one below demonstrates how to catch the signals. void Signal_Handler(sig) /* signal handler function */ int sig; { switch(sig){ case SIGHUP: /* rehash the server */ break; case SIGTERM: /* finalize the server */ exit(0) break; } } signal(SIGHUP,Signal_Handler); /* hangup signal */ signal(SIGTERM,Signal_Handler); /* software termination signal from kill */ First we construct a signal handling function and then tie up signals to that function. 8) Logging [syslogd,syslog.conf,openlog,syslog,closelog] A running server creates messages, naturally some are impor |
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |
你可能对下面的文章感兴趣
上一篇: 2010年企业Linux应用趋势下一篇: 招兼职solaris讲师
关于关于unix linux daemon程序编写的文章 e文 Unix Daemon Server Programming的所有评论