守護進程是什么?linux創(chuàng)建守護進程的步驟詳解
守護進程是什么?可能很多伙伴對于守護進程都不怎么了解吧?守護進程是操作系統(tǒng)后臺的一種特殊進程,像Linux系統(tǒng)的大多數(shù)服務(wù)器都是通過守護進程實現(xiàn)的。今天小編就來給大家科普一下什么是守護進程以及l(fā)inux系統(tǒng)如何創(chuàng)建守護進程。
一、守護進程是什么?
Linux Daemon(守護進程)是運行在后臺的一種特殊進程。它獨立于控制終端并且周期性地執(zhí)行某種任務(wù)或等待處理某些發(fā)生的事件。它不需要用戶輸入就能運行而且提供某種服務(wù),不是對整個系統(tǒng)就是對某個用戶程序提供服務(wù)。Linux系統(tǒng)的大多數(shù)服務(wù)器就是通過守護進程實現(xiàn)的。常見的守護進程包括系統(tǒng)日志進程syslogd、 web服務(wù)器httpd、郵件服務(wù)器sendmail和數(shù)據(jù)庫服務(wù)器mysqld等。
守護進程一般在系統(tǒng)啟動時開始運行,除非強行終止,否則直到系統(tǒng)關(guān)機都保持運行。守護進程經(jīng)常以超級用戶(root)權(quán)限運行,因為它們要使用特殊的端口(1-1024)或訪問某些特殊的資源。
一個守護進程的父進程是init進程,因為它真正的父進程在fork出子進程后就先于子進程exit退出了,所以它是一個由init繼承的孤兒進程。守護進程是非交互式程序,沒有控制終端,所以任何輸出,無論是向標準輸出設(shè)備stdout還是標準出錯設(shè)備stderr的輸出都需要特殊處理。
守護進程的名稱通常以d結(jié)尾,比如sshd、xinetd、crond等
二、創(chuàng)建守護進程的步驟
首先我們要了解一些基本概念:
1、進程組 :
每個進程也屬于一個進程組
每個進程主都有一個進程組號,該號等于該進程組組長的PID號 。
一個進程只能為它自己或子進程設(shè)置進程組ID號
2、會話期:
會話期(session)是一個或多個進程組的集合。
setsid()函數(shù)可以建立一個對話期:
如果,調(diào)用setsid的進程不是一個進程組的組長,此函數(shù)創(chuàng)建一個新的會話期。
(1)此進程變成該對話期的首進程
(2)此進程變成一個新進程組的組長進程。
(3)此進程沒有控制終端,如果在調(diào)用setsid前,該進程有控制終端,那么與該終端的聯(lián)系被解除。 如果該進程是一個進程組的組長,此函數(shù)返回錯誤。
(4)為了保證這一點,我們先調(diào)用fork()然后exit(),此時只有子進程在運行
現(xiàn)在我們來給出創(chuàng)建守護進程的所需步驟:
編寫守護進程的一般步驟:
(1)在父進程中執(zhí)行fork并exit推出;
(2)在子進程中調(diào)用setsid函數(shù)創(chuàng)建新的會話;
(3)在子進程中調(diào)用chdir函數(shù),讓根目錄 ”/” 成為子進程的工作目錄;
(4)在子進程中調(diào)用umask函數(shù),設(shè)置進程的umask為0;
(5)在子進程中關(guān)閉任何不需要的文件描述符
說明:
(1)在后臺運行。
為避免掛起控制終端將Daemon放入后臺執(zhí)行。方法是在進程中調(diào)用fork使父進程終止,讓Daemon在子進程中后臺執(zhí)行。
if(pid=fork())
exit(0);//是父進程,結(jié)束父進程,子進程繼續(xù)
(2)脫離控制終端,登錄會話和進程組
有必要先介紹一下Linux中的進程與控制終端,登錄會話和進程組之間的關(guān)系:進程屬于一個進程組,進程組號(GID)就是進程組長的進程號(PID)。登錄會話可以包含多個進程組。這些進程組共享一個控制終端。這個控制終端通常是創(chuàng)建進程的登錄終端。
控制終端,登錄會話和進程組通常是從父進程繼承下來的。我們的目的就是要擺脫它們,使之不受它們的影響。方法是在第1點的基礎(chǔ)上,調(diào)用setsid()使進程成為會話組長:
setsid();
說明:當進程是會話組長時setsid()調(diào)用失敗。但第一點已經(jīng)保證進程不是會話組長。setsid()調(diào)用成功后,進程成為新的會話組長和新的進程組長,并與原來的登錄會話和進程組脫離。由于會話過程對控制終端的獨占性,進程同時與控制終端脫離。
(3) 禁止進程重新打開控制終端
現(xiàn)在,進程已經(jīng)成為無終端的會話組長。但它可以重新申請打開一個控制終端。可以通過使進程不再成為會話組長來禁止進程重新打開控制終端:
if(pid=fork())
exit(0);//結(jié)束第一子進程,第二子進程繼續(xù)(第二子進程不再是會話組長)
(4)關(guān)閉打開的文件描述符
進程從創(chuàng)建它的父進程那里繼承了打開的文件描述符。如不關(guān)閉,將會浪費系統(tǒng)資源,造成進程所在的文件系統(tǒng)無法卸下以及引起無法預(yù)料的錯誤。按如下方法關(guān)閉它們:
for(i=0;i 關(guān)閉打開的文件描述符close(i);>
(5) 改變當前工作目錄
進程活動時,其工作目錄所在的文件系統(tǒng)不能卸下。一般需要將工作目錄改變到根目錄。對于需要轉(zhuǎn)儲核心,寫運行日志的進程將工作目錄改變到特定目錄如/tmpchdir("/")
(6)重設(shè)文件創(chuàng)建掩模
進程從創(chuàng)建它的父進程那里繼承了文件創(chuàng)建掩模。它可能修改守護進程所創(chuàng)建的文件的存取位。為防止這一點,將文件創(chuàng)建掩模清除:umask(0);
(7)處理SIGCHLD信號
處理SIGCHLD信號并不是必須的。但對于某些進程,特別是服務(wù)器進程往往在請求到來時生成子進程處理請求。如果父進程不等待子進程結(jié)束,子進程將成為僵尸進程(zombie)從而占用系統(tǒng)資源。如果父進程等待子進程結(jié)束,將增加父進程的負擔,影響服務(wù)器進程的并發(fā)性能。在Linux下可以簡單地將SIGCHLD信號的操作設(shè)為SIG_IGN。
signal(SIGCHLD,SIG_IGN);
這樣,內(nèi)核在子進程結(jié)束時不會產(chǎn)生僵尸進程。這一點與BSD4不同,BSD4下必須顯式等待子進程結(jié)束才能釋放僵尸進程。
三、創(chuàng)建守護進程
在創(chuàng)建之前我們先了解setsid()使用:
#include <unistd.h> pid_t setsid(void);DESCRIPTIONsetsid() creates a new session if the calling process is not a processgroup leader. The calling process is the leader of the new session,the process group leader of the new process group, and has no control-ling tty. The process group ID and session ID of the calling processare set to the PID of the calling process. The calling process will bethe only process in this new process group and in this new session.//調(diào)用進程必須是非當前進程組組長,調(diào)用后,產(chǎn)生一個新的會話期,且該會話期中只有一個進程組,且該進程組組長為調(diào)用進程,沒有控制終端,新產(chǎn)生的group ID 和 session ID 被設(shè)置成調(diào)用進程的PID
RETURN VALUEOn success, the (new) session ID of the calling process is returned.On error, (pid_t) -1 is returned, and errno is set to indicate theerror.現(xiàn)在根據(jù)上述步驟創(chuàng)建一個守護進程:
以下程序是創(chuàng)建一個守護進程,然后利用這個守護進程每個一分鐘向daemon.log文件中寫入當前時間
01#include <stdio.h>02#include <unistd.h>03#include <stdlib.h>04#include <time.h>05#include <fcntl.h>06#include <string.h>07#include <sys/stat.h>08#define ERR_EXIT(m) 09do10{11perror(m);12exit(EXIT_FAILURE);13}14while (0);15void creat_daemon(void);16int main(void)17{18time_t t;19int fd;20creat_daemon();21while(1){22fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);23if(fd == -1)24ERR_EXIT("open error");25t = time(0);26char *buf = asctime(localtime(&t));27write(fd,buf,strlen(buf));28close(fd);29sleep(60);30}31return 0;32}33void creat_daemon(void)34{35pid_t pid;36pid = fork();37if( pid == -1)38ERR_EXIT("fork error");39if(pid > 0 )40exit(EXIT_SUCCESS);41if(setsid() == -1)42ERR_EXIT("SETSID ERROR");43chdir("/");44int i;45for( i = 0; i < 3; ++i)46{47close(i);48open("/dev/null", O_RDWR);49dup(0);50dup(0);51}52umask(0);53return;54}復(fù)制代碼#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <fcntl.h>#include <string.h>#include <sys/stat.h>#define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while (0);void creat_daemon(void);int main(void){time_t t;int fd;creat_daemon();while(1){fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);if(fd == -1)ERR_EXIT("open error");t = time(0);char *buf = asctime(localtime(&t));write(fd,buf,strlen(buf));close(fd);sleep(60);}return 0;}void creat_daemon(void){pid_t pid;pid = fork();if( pid == -1)ERR_EXIT("fork error");if(pid > 0 )exit(EXIT_SUCCESS);if(setsid() == -1)ERR_EXIT("SETSID ERROR");chdir("/");int i;for( i = 0; i < 3; ++i){close(i);open("/dev/null", O_RDWR);dup(0);dup(0);}umask(0);return;}結(jié)果:
結(jié)果顯示:當我一普通用戶執(zhí)行a.out時,進程表中并沒有出現(xiàn)新創(chuàng)建的守護進程,但當我以root用戶執(zhí)行時,成功了,并在/目錄下創(chuàng)建了daemon.log文件,cat查看后確實每個一分鐘寫入一次。為什么只能root執(zhí)行,那是因為當我們創(chuàng)建守護進程時,已經(jīng)將當前目錄切換我/目錄,所以當我之后創(chuàng)建daemon.log文件是其實是在/目錄下,那肯定不行,因為普通用戶沒有權(quán)限,或許你會問那為啥沒報錯呢?其實是有出錯,只不過我們在創(chuàng)建守護進程時已經(jīng)將標準輸入關(guān)閉并重定向到/dev/null,所以看不到錯誤信息。
四、利用庫函數(shù)daemon()創(chuàng)建守護進程
其實我們完全可以利用daemon()函數(shù)創(chuàng)建守護進程,其函數(shù)原型:
#include <unistd.h>int daemon(int nochdir, int noclose);DESCRIPTIONThe daemon() function is for programs wishing to detach themselves fromthe controlling terminal and run in the background as system daemons. If nochdir is zero, daemon() changes the process’s current workingdirectory to the root directory ("/"); otherwise, If noclose is zero, daemon() redirects standard input, standard outputand standard error to /dev/null; otherwise, no changes are made tothese file descriptors.功能:創(chuàng)建一個守護進程
參數(shù):
nochdir:=0將當前目錄更改至“/”
noclose:=0將標準輸入、標準輸出、標準錯誤重定向至“/dev/null”
返回值:
成功:0
失敗:-1
現(xiàn)在我們利用daemon()改寫剛才那個程序:
01#include <stdio.h>02#include <unistd.h>03#include <stdlib.h>04#include <time.h>05#include <fcntl.h>06#include <string.h>07#include <sys/stat.h>08#define ERR_EXIT(m) 09do10{11perror(m);12exit(EXIT_FAILURE);13}14while (0);15void creat_daemon(void);16int main(void)17{18time_t t;19int fd;20if(daemon(0,0) == -1)21ERR_EXIT("daemon error");22while(1){23fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);24if(fd == -1)25ERR_EXIT("open error");26t = time(0);27char *buf = asctime(localtime(&t));28write(fd,buf,strlen(buf));29close(fd);30sleep(60);31}32return 0;33}復(fù)制代碼#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <fcntl.h>#include <string.h>#include <sys/stat.h>#define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while (0);void creat_daemon(void);int main(void){time_t t;int fd;if(daemon(0,0) == -1)ERR_EXIT("daemon error");while(1){fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);if(fd == -1)ERR_EXIT("open error");t = time(0);char *buf = asctime(localtime(&t));write(fd,buf,strlen(buf));close(fd);sleep(60);}return 0;}當daemon(0,0)時:
結(jié)果同剛才一樣,也是只有root才能成功,普通用戶執(zhí)行時看不到錯誤信息
現(xiàn)在讓daemon(0,1),就是不關(guān)閉標準輸入輸出結(jié)果:
可以看到錯誤信息
現(xiàn)在讓daemon(1,0),就是不重定向,結(jié)果如下:
這次普通用戶執(zhí)行成功了,以為沒有切換到/目錄下,有權(quán)限
其實我們可以利用我們剛才寫的創(chuàng)建守護進程程序默認daemon()實現(xiàn):
代碼如下:
01#include <stdio.h>02#include <unistd.h>03#include <stdlib.h>04#include <time.h>05#include <fcntl.h>06#include <string.h>07#include <sys/stat.h>08#define ERR_EXIT(m) 09do10{11perror(m);12exit(EXIT_FAILURE);13}14while (0);15void creat_daemon(int nochdir, int noclose);16int main(void)17{18time_t t;19int fd;20creat_daemon(0,0);21while(1){22fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);23if(fd == -1)24ERR_EXIT("open error");25t = time(0);26char *buf = asctime(localtime(&t));27write(fd,buf,strlen(buf));28close(fd);29sleep(60);30}31return 0;32}33void creat_daemon(int nochdir, int noclose)34{35pid_t pid;36pid = fork();37if( pid == -1)38ERR_EXIT("fork error");39if(pid > 0 )40exit(EXIT_SUCCESS);41if(setsid() == -1)42ERR_EXIT("SETSID ERROR");43if(nochdir == 0)44chdir("/");45if(noclose == 0){46int i;47for( i = 0; i < 3; ++i)48{49close(i);50open("/dev/null", O_RDWR);51dup(0);52dup(0);53}54umask(0);55return;56}復(fù)制代碼#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <time.h>#include <fcntl.h>#include <string.h>#include <sys/stat.h>#define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while (0);void creat_daemon(int nochdir, int noclose);int main(void){time_t t;int fd;creat_daemon(0,0);while(1){fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);if(fd == -1)ERR_EXIT("open error");t = time(0);char *buf = asctime(localtime(&t));write(fd,buf,strlen(buf));close(fd);sleep(60);}return 0;}void creat_daemon(int nochdir, int noclose){pid_t pid;pid = fork();if( pid == -1)ERR_EXIT("fork error");if(pid > 0 )exit(EXIT_SUCCESS);if(setsid() == -1)ERR_EXIT("SETSID ERROR");if(nochdir == 0)chdir("/");if(noclose == 0){int i;for( i = 0; i < 3; ++i){close(i);open("/dev/null", O_RDWR);dup(0);dup(0);}umask(0);return;}關(guān)于linux系統(tǒng)創(chuàng)建守護進程的操作步驟就給大家講解到這里了,有此工作需求的伙伴,可以按照小編的步驟進行創(chuàng)建。
相關(guān)文章:
1. 統(tǒng)信UOS怎么設(shè)置不讓瀏覽器上網(wǎng)? UOS禁止瀏覽器訪問網(wǎng)絡(luò)的技巧2. centos6.5下kvm環(huán)境搭建詳解3. Debian11怎么查看虛擬內(nèi)存使用情況? 查看進程占用虛擬內(nèi)存技巧4. 怎么用U盤安裝Win11正式版 Win11正式版本U盤安裝教程5. 在CentOS或RHEL 7上修改主機名的方法6. 什么是Unix 以及它為什么這么重要?7. Win11 正式版 Build 22621.1105一月累積更新補丁KB5022303發(fā)布(附完整更新日志)8. Mac無線網(wǎng)絡(luò)怎么設(shè)置?蘋果Mac電腦無線網(wǎng)絡(luò)設(shè)置教程9. solaris11如何擴展資源池?solaris11擴展資源池的方法10. 老毛桃winpe系統(tǒng)對注冊表信息進行備份的方法
