unix当中的精灵进程
unix系统中有许多在后台运行,并且无控制终端的进程。我们把它叫精灵进程。在unix中我们可以用 ps -ef 查看,其tty项用?标记。本文就精灵进程的特征,以及如何编写作一些探讨: 一 特征和编写规则 1. 因为精灵进程是在后台运行,所以首先调用fork()生成一个子进程,然后使父进程exit()。
2. 调用setsid()创建一个新的对话期。 3. 设定其工作目录。(chdir()) 4. 将文件创建屏蔽字设置为0 。(umask(0)) 5. 关闭不再需要的文件描述符。(close(int)) 二 例子 # include # include int main(void) { int pid ; int s_pid ; file *fp ; if(( fp = fopen("/usr/log","w+")) == null) 本文出自 51CTO.COM技术博客{ fprintf(stderr,"log file open error !\n") ; exit(0) ; } if((pid =fork()) < 0) return -1 ; else if( pid > 0 ) exit(0) ; else { s_pid = setsid() ; fprintf(fp,"setsid:= %d",s_pid) ; umask(0) ; chdir("/usr") ; close(0) ; close(1) ; close(2) ; sleep(100) ; fclose(fp) ; return 0 ; } } 精灵进程运行过程中的有关信息在/usr/log文件中。 |


田园
博客统计信息
热门文章
最新评论
友情链接
