系統(tǒng)之家 - 系統(tǒng)光盤下載網(wǎng)站!

當(dāng)前位置:系統(tǒng)之家 > 系統(tǒng)教程 > linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

時間:2017-10-28 09:04:00 作者:chunhua 來源:系統(tǒng)之家 1. 掃描二維碼隨時看資訊 2. 請使用手機瀏覽器訪問: https://m.xitongzhijia.net/xtjc/20171028/109742.html 手機查看 評論

  守護(hù)進(jìn)程是什么?可能很多伙伴對于守護(hù)進(jìn)程都不怎么了解吧?守護(hù)進(jìn)程是操作系統(tǒng)后臺的一種特殊進(jìn)程,像Linux系統(tǒng)的大多數(shù)服務(wù)器都是通過守護(hù)進(jìn)程實現(xiàn)的。今天小編就來給大家科普一下什么是守護(hù)進(jìn)程以及l(fā)inux系統(tǒng)如何創(chuàng)建守護(hù)進(jìn)程。

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

  一、守護(hù)進(jìn)程是什么?

  Linux Daemon(守護(hù)進(jìn)程)是運行在后臺的一種特殊進(jìn)程。它獨立于控制終端并且周期性地執(zhí)行某種任務(wù)或等待處理某些發(fā)生的事件。它不需要用戶輸入就能運行而且提供某種服務(wù),不是對整個系統(tǒng)就是對某個用戶程序提供服務(wù)。Linux系統(tǒng)的大多數(shù)服務(wù)器就是通過守護(hù)進(jìn)程實現(xiàn)的。常見的守護(hù)進(jìn)程包括系統(tǒng)日志進(jìn)程syslogd、 web服務(wù)器httpd、郵件服務(wù)器sendmail和數(shù)據(jù)庫服務(wù)器mysqld等。

  守護(hù)進(jìn)程一般在系統(tǒng)啟動時開始運行,除非強行終止,否則直到系統(tǒng)關(guān)機都保持運行。守護(hù)進(jìn)程經(jīng)常以超級用戶(root)權(quán)限運行,因為它們要使用特殊的端口(1-1024)或訪問某些特殊的資源。

  一個守護(hù)進(jìn)程的父進(jìn)程是init進(jìn)程,因為它真正的父進(jìn)程在fork出子進(jìn)程后就先于子進(jìn)程exit退出了,所以它是一個由init繼承的孤兒進(jìn)程。守護(hù)進(jìn)程是非交互式程序,沒有控制終端,所以任何輸出,無論是向標(biāo)準(zhǔn)輸出設(shè)備stdout還是標(biāo)準(zhǔn)出錯設(shè)備stderr的輸出都需要特殊處理。

  守護(hù)進(jìn)程的名稱通常以d結(jié)尾,比如sshd、xinetd、crond等

  二、創(chuàng)建守護(hù)進(jìn)程的步驟

  首先我們要了解一些基本概念:

  1、進(jìn)程組 :

  每個進(jìn)程也屬于一個進(jìn)程組

  每個進(jìn)程主都有一個進(jìn)程組號,該號等于該進(jìn)程組組長的PID號 。

  一個進(jìn)程只能為它自己或子進(jìn)程設(shè)置進(jìn)程組ID號

  2、會話期:

  會話期(session)是一個或多個進(jìn)程組的集合。

  setsid()函數(shù)可以建立一個對話期:

  如果,調(diào)用setsid的進(jìn)程不是一個進(jìn)程組的組長,此函數(shù)創(chuàng)建一個新的會話期。

  (1)此進(jìn)程變成該對話期的首進(jìn)程

 。2)此進(jìn)程變成一個新進(jìn)程組的組長進(jìn)程。

 。3)此進(jìn)程沒有控制終端,如果在調(diào)用setsid前,該進(jìn)程有控制終端,那么與該終端的聯(lián)系被解除。 如果該進(jìn)程是一個進(jìn)程組的組長,此函數(shù)返回錯誤。

  (4)為了保證這一點,我們先調(diào)用fork()然后exit(),此時只有子進(jìn)程在運行

  現(xiàn)在我們來給出創(chuàng)建守護(hù)進(jìn)程的所需步驟:

  編寫守護(hù)進(jìn)程的一般步驟:

  (1)在父進(jìn)程中執(zhí)行fork并exit推出;

 。2)在子進(jìn)程中調(diào)用setsid函數(shù)創(chuàng)建新的會話;

 。3)在子進(jìn)程中調(diào)用chdir函數(shù),讓根目錄 ”/” 成為子進(jìn)程的工作目錄;

 。4)在子進(jìn)程中調(diào)用umask函數(shù),設(shè)置進(jìn)程的umask為0;

 。5)在子進(jìn)程中關(guān)閉任何不需要的文件描述符

  說明:

 。1)在后臺運行。

  為避免掛起控制終端將Daemon放入后臺執(zhí)行。方法是在進(jìn)程中調(diào)用fork使父進(jìn)程終止,讓Daemon在子進(jìn)程中后臺執(zhí)行。

  if(pid=fork())

  exit(0);//是父進(jìn)程,結(jié)束父進(jìn)程,子進(jìn)程繼續(xù)

  (2)脫離控制終端,登錄會話和進(jìn)程組

  有必要先介紹一下Linux中的進(jìn)程與控制終端,登錄會話和進(jìn)程組之間的關(guān)系:進(jìn)程屬于一個進(jìn)程組,進(jìn)程組號(GID)就是進(jìn)程組長的進(jìn)程號(PID)。登錄會話可以包含多個進(jìn)程組。這些進(jìn)程組共享一個控制終端。這個控制終端通常是創(chuàng)建進(jìn)程的登錄終端。

  控制終端,登錄會話和進(jìn)程組通常是從父進(jìn)程繼承下來的。我們的目的就是要擺脫它們,使之不受它們的影響。方法是在第1點的基礎(chǔ)上,調(diào)用setsid()使進(jìn)程成為會話組長:

  setsid();

  說明:當(dāng)進(jìn)程是會話組長時setsid()調(diào)用失敗。但第一點已經(jīng)保證進(jìn)程不是會話組長。setsid()調(diào)用成功后,進(jìn)程成為新的會話組長和新的進(jìn)程組長,并與原來的登錄會話和進(jìn)程組脫離。由于會話過程對控制終端的獨占性,進(jìn)程同時與控制終端脫離。

 。3) 禁止進(jìn)程重新打開控制終端

  現(xiàn)在,進(jìn)程已經(jīng)成為無終端的會話組長。但它可以重新申請打開一個控制終端?梢酝ㄟ^使進(jìn)程不再成為會話組長來禁止進(jìn)程重新打開控制終端:

  if(pid=fork())

  exit(0);//結(jié)束第一子進(jìn)程,第二子進(jìn)程繼續(xù)(第二子進(jìn)程不再是會話組長)

 。4)關(guān)閉打開的文件描述符

  進(jìn)程從創(chuàng)建它的父進(jìn)程那里繼承了打開的文件描述符。如不關(guān)閉,將會浪費系統(tǒng)資源,造成進(jìn)程所在的文件系統(tǒng)無法卸下以及引起無法預(yù)料的錯誤。按如下方法關(guān)閉它們:

  for(i=0;i 關(guān)閉打開的文件描述符close(i);>

 。5) 改變當(dāng)前工作目錄

  進(jìn)程活動時,其工作目錄所在的文件系統(tǒng)不能卸下。一般需要將工作目錄改變到根目錄。對于需要轉(zhuǎn)儲核心,寫運行日志的進(jìn)程將工作目錄改變到特定目錄如/tmpchdir("/")

 。6)重設(shè)文件創(chuàng)建掩模

  進(jìn)程從創(chuàng)建它的父進(jìn)程那里繼承了文件創(chuàng)建掩模。它可能修改守護(hù)進(jìn)程所創(chuàng)建的文件的存取位。為防止這一點,將文件創(chuàng)建掩模清除:umask(0);

  (7)處理SIGCHLD信號

  處理SIGCHLD信號并不是必須的。但對于某些進(jìn)程,特別是服務(wù)器進(jìn)程往往在請求到來時生成子進(jìn)程處理請求。如果父進(jìn)程不等待子進(jìn)程結(jié)束,子進(jìn)程將成為僵尸進(jìn)程(zombie)從而占用系統(tǒng)資源。如果父進(jìn)程等待子進(jìn)程結(jié)束,將增加父進(jìn)程的負(fù)擔(dān),影響服務(wù)器進(jìn)程的并發(fā)性能。在Linux下可以簡單地將SIGCHLD信號的操作設(shè)為SIG_IGN。

  signal(SIGCHLD,SIG_IGN);

  這樣,內(nèi)核在子進(jìn)程結(jié)束時不會產(chǎn)生僵尸進(jìn)程。這一點與BSD4不同,BSD4下必須顯式等待子進(jìn)程結(jié)束才能釋放僵尸進(jìn)程。

  三、創(chuàng)建守護(hù)進(jìn)程

  在創(chuàng)建之前我們先了解setsid()使用:

#include <unistd.h>
       pid_t setsid(void);
DESCRIPTION 
       setsid()  creates a new session if the calling process is not a process 
       group 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  process 
       are set to the PID of the calling process.  The calling process will be 
       the only process in this new process group and in this new session.

  //調(diào)用進(jìn)程必須是非當(dāng)前進(jìn)程組組長,調(diào)用后,產(chǎn)生一個新的會話期,且該會話期中只有一個進(jìn)程組,且該進(jìn)程組組長為調(diào)用進(jìn)程,沒有控制終端,新產(chǎn)生的group ID 和 session ID 被設(shè)置成調(diào)用進(jìn)程的PID

RETURN VALUE 
       On success, the (new) session ID of the calling  process  is  returned. 
       On  error,  (pid_t) -1  is  returned,  and errno is set to indicate the 
       error.

  現(xiàn)在根據(jù)上述步驟創(chuàng)建一個守護(hù)進(jìn)程:

  以下程序是創(chuàng)建一個守護(hù)進(jìn)程,然后利用這個守護(hù)進(jìn)程每個一分鐘向daemon.log文件中寫入當(dāng)前時間

  1. 01#include <stdio.h>
  2. 02#include <unistd.h>
  3. 03#include <stdlib.h>
  4. 04#include <time.h>
  5. 05#include <fcntl.h>
  6. 06#include <string.h>
  7. 07#include <sys/stat.h>
  8. 08#define ERR_EXIT(m) \
  9. 09do\
  10. 10{\
  11. 11perror(m);\
  12. 12exit(EXIT_FAILURE);\
  13. 13}\
  14. 14while (0);\
  15. 15void creat_daemon(void);
  16. 16int main(void)
  17. 17{
  18. 18time_t t;
  19. 19int fd;
  20. 20creat_daemon();
  21. 21while(1){
  22. 22fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);
  23. 23if(fd == -1)
  24. 24ERR_EXIT("open error");
  25. 25t = time(0);
  26. 26char *buf = asctime(localtime(&t));
  27. 27write(fd,buf,strlen(buf));
  28. 28close(fd);
  29. 29sleep(60);
  30. 30}
  31. 31return 0;
  32. 32}
  33. 33void creat_daemon(void)
  34. 34{
  35. 35pid_t pid;
  36. 36pid = fork();
  37. 37if( pid == -1)
  38. 38ERR_EXIT("fork error");
  39. 39if(pid > 0 )
  40. 40exit(EXIT_SUCCESS);
  41. 41if(setsid() == -1)
  42. 42ERR_EXIT("SETSID ERROR");
  43. 43chdir("/");
  44. 44int i;
  45. 45for( i = 0; i < 3; ++i)
  46. 46{
  47. 47close(i);
  48. 48open("/dev/null", O_RDWR);
  49. 49dup(0);
  50. 50dup(0);
  51. 51}
  52. 52umask(0);
  53. 53return;
  54. 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é)果:

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

  結(jié)果顯示:當(dāng)我一普通用戶執(zhí)行a.out時,進(jìn)程表中并沒有出現(xiàn)新創(chuàng)建的守護(hù)進(jìn)程,但當(dāng)我以root用戶執(zhí)行時,成功了,并在/目錄下創(chuàng)建了daemon.log文件,cat查看后確實每個一分鐘寫入一次。為什么只能root執(zhí)行,那是因為當(dāng)我們創(chuàng)建守護(hù)進(jìn)程時,已經(jīng)將當(dāng)前目錄切換我/目錄,所以當(dāng)我之后創(chuàng)建daemon.log文件是其實是在/目錄下,那肯定不行,因為普通用戶沒有權(quán)限,或許你會問那為啥沒報錯呢?其實是有出錯,只不過我們在創(chuàng)建守護(hù)進(jìn)程時已經(jīng)將標(biāo)準(zhǔn)輸入關(guān)閉并重定向到/dev/null,所以看不到錯誤信息。

  四、利用庫函數(shù)daemon()創(chuàng)建守護(hù)進(jìn)程

  其實我們完全可以利用daemon()函數(shù)創(chuàng)建守護(hù)進(jìn)程,其函數(shù)原型:

#include <unistd.h>
int daemon(int nochdir, int noclose);
DESCRIPTION 
       The daemon() function is for programs wishing to detach themselves from 
       the controlling terminal and run in the background as system daemons.
       If nochdir is zero, daemon()  changes  the  process’s  current  working 
       directory to the root directory ("/"); otherwise,
       If  noclose is zero, daemon() redirects standard input, standard output 
       and standard error to /dev/null; otherwise,  no  changes  are  made  to 
       these file descriptors.

  功能:創(chuàng)建一個守護(hù)進(jìn)程

  參數(shù):

  nochdir:=0將當(dāng)前目錄更改至“/”

  noclose:=0將標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出、標(biāo)準(zhǔn)錯誤重定向至“/dev/null”

  返回值:

  成功:0

  失。-1

  現(xiàn)在我們利用daemon()改寫剛才那個程序:

  1. 01#include <stdio.h>
  2. 02#include <unistd.h>
  3. 03#include <stdlib.h>
  4. 04#include <time.h>
  5. 05#include <fcntl.h>
  6. 06#include <string.h>
  7. 07#include <sys/stat.h>
  8. 08#define ERR_EXIT(m) \
  9. 09do\
  10. 10{\
  11. 11perror(m);\
  12. 12exit(EXIT_FAILURE);\
  13. 13}\
  14. 14while (0);\
  15. 15void creat_daemon(void);
  16. 16int main(void)
  17. 17{
  18. 18time_t t;
  19. 19int fd;
  20. 20if(daemon(0,0) == -1)
  21. 21ERR_EXIT("daemon error");
  22. 22while(1){
  23. 23fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);
  24. 24if(fd == -1)
  25. 25ERR_EXIT("open error");
  26. 26t = time(0);
  27. 27char *buf = asctime(localtime(&t));
  28. 28write(fd,buf,strlen(buf));
  29. 29close(fd);
  30. 30sleep(60);
  31. 31}
  32. 32return 0;
  33. 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; }

  當(dāng)daemon(0,0)時:

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

  結(jié)果同剛才一樣,也是只有root才能成功,普通用戶執(zhí)行時看不到錯誤信息

  現(xiàn)在讓daemon(0,1),就是不關(guān)閉標(biāo)準(zhǔn)輸入輸出結(jié)果:

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

  可以看到錯誤信息

  現(xiàn)在讓daemon(1,0),就是不重定向,結(jié)果如下:

守護(hù)進(jìn)程是什么?linux創(chuàng)建守護(hù)進(jìn)程的步驟詳解

  這次普通用戶執(zhí)行成功了,以為沒有切換到/目錄下,有權(quán)限

  其實我們可以利用我們剛才寫的創(chuàng)建守護(hù)進(jìn)程程序默認(rèn)daemon()實現(xiàn):

  代碼如下:

  1. 01#include <stdio.h>
  2. 02#include <unistd.h>
  3. 03#include <stdlib.h>
  4. 04#include <time.h>
  5. 05#include <fcntl.h>
  6. 06#include <string.h>
  7. 07#include <sys/stat.h>
  8. 08#define ERR_EXIT(m) \
  9. 09do\
  10. 10{\
  11. 11perror(m);\
  12. 12exit(EXIT_FAILURE);\
  13. 13}\
  14. 14while (0);\
  15. 15void creat_daemon(int nochdir, int noclose);
  16. 16int main(void)
  17. 17{
  18. 18time_t t;
  19. 19int fd;
  20. 20creat_daemon(0,0);
  21. 21while(1){
  22. 22fd = open("daemon.log",O_WRONLY|O_CREAT|O_APPEND,0644);
  23. 23if(fd == -1)
  24. 24ERR_EXIT("open error");
  25. 25t = time(0);
  26. 26char *buf = asctime(localtime(&t));
  27. 27write(fd,buf,strlen(buf));
  28. 28close(fd);
  29. 29sleep(60);
  30. 30}
  31. 31return 0;
  32. 32}
  33. 33void creat_daemon(int nochdir, int noclose)
  34. 34{
  35. 35pid_t pid;
  36. 36pid = fork();
  37. 37if( pid == -1)
  38. 38ERR_EXIT("fork error");
  39. 39if(pid > 0 )
  40. 40exit(EXIT_SUCCESS);
  41. 41if(setsid() == -1)
  42. 42ERR_EXIT("SETSID ERROR");
  43. 43if(nochdir == 0)
  44. 44chdir("/");
  45. 45if(noclose == 0){
  46. 46int i;
  47. 47for( i = 0; i < 3; ++i)
  48. 48{
  49. 49close(i);
  50. 50open("/dev/null", O_RDWR);
  51. 51dup(0);
  52. 52dup(0);
  53. 53}
  54. 54umask(0);
  55. 55return;
  56. 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)建守護(hù)進(jìn)程的操作步驟就給大家講解到這里了,有此工作需求的伙伴,可以按照小編的步驟進(jìn)行創(chuàng)建。

標(biāo)簽

發(fā)表評論

0

沒有更多評論了

評論就這些咯,讓大家也知道你的獨特見解

立即評論

以上留言僅代表用戶個人觀點,不代表系統(tǒng)之家立場

其他版本軟件

熱門教程

人氣教程排行

Linux系統(tǒng)推薦

官方交流群 軟件收錄