unix系統中有許多在後臺運行,並且無控制終端的進程。我們把它叫精靈進程。在unix中我們可以用 ps -ef 查看,其tty項用?標記。本文就精靈進程的特徵,以及如何編寫作一些探討:
一 特徵和編寫規則
2. 調用setsid()創建一個新的對話期。
3. 設定其工作目錄。(chdir())
4. 將文件創建屏蔽字設置爲0 。(umask(0))
5. 關閉不再需要的文件描述符。(close(int))
二 例子
# include
# include
int
main(void)
{
int pid ;
int s_pid ;
file *fp ;
{
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文件中。