天天看點

Linux系統程式設計——09-linux-day08(守護程序-線程)

在學習Linux系統程式設計總結了筆記,并分享出來。有問題請及時聯系部落客:​​Alliswell_WP​​,轉載請注明出處。

09-linux-day08(守護程序-線程)

一、學習目标

1、守護程序的特點

2、熟練進行守護程序的建立

3、熟練掌握多線程的建立

4、熟練掌握線程的退出和資源回收

二、守護程序+線程

1、守護程序相關的概念

Linux系統程式設計——09-linux-day08(守護程式-線程)
Linux系統程式設計——09-linux-day08(守護程式-線程)

會話:程序組的更高一級,多個程序組對應一個會話。

程序組:多個程序在同一個組,第一個程序預設是程序組的組長。

建立會話的時候,組織不可以建立,必須是組員建立。

建立會話的步驟:建立子程序,父程序死去,子程序自當會長。

Linux系統程式設計——09-linux-day08(守護程式-線程)

》setsid——調用後就成為會長

man 2 setsid

pid_t setsid(void);

Linux系統程式設計——09-linux-day08(守護程式-線程)

 (例如:如果按照了mysql,可以通過ps aus | grep mysqld檢視mysql的守護程序)

Linux系統程式設計——09-linux-day08(守護程式-線程)
Linux系統程式設計——09-linux-day08(守護程式-線程)

前2步驟是必須的!

2、守護程序建立

》需求:建立一個守護程序:每分鐘在$HOME/log/ 建立一個檔案,程式名.時間戳

>touch daemon.c

>vi daemon.c

1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<fcntl.h>
 4 #include<stdlib.h>
 5 #include<sys/types.h>
 6 #include<sys/stat.h>
 7 #include<string.h>
 8 #include<signal.h>
 9 #include<sys/time.h>
10 #include<time.h>
11 
12 #define _FILE_NAME_FORMAT_ "%s/log/mydaemon.%ld" //定義檔案格式化
13 
14 void touchfile(int num){
15     char *HomeDir = getenv("HOME");
16     char strFilename[256]={0};
17     
18     sprintf(strFilename,_FILE_NAME_FORMAT_,HomeDir,time(NULL));
19     
20     int fd = open(strFilename,O_RDWR|O_CREAT,0666);
21     
22     if(fd < 0){
23         perror("open err");
24         exit(1);
25     }
26     close(fd);
27 }
28 
29 
30 int main(int argc, char *argv[])
31 {
32     //建立子程序,父程序退出
33     pid_t pid = fork();
34     if(pid > 0){
35         exit(1);
36     }
37     //當會長
38     setsid();
39     //設定掩碼
40     umask(0);
41     //切換目錄
42     chdir(getenv("HOME"));//切換到家目錄
43     //關閉檔案描述符
44     //close(0),close(1),close(2);//友善調試,不關
45     //執行核心邏輯
46     //struct itimerval myit = {{60,0},{60,0}};
47     struct itimerval myit = {{60,0},{1,0}};//友善調試,初始改為1s
48     setitimer(ITIMER_REAL,&myit,NULL);
49     struct sigaction act;
50     act.sa_flags = 0;
51     sigemptyset(&act.sa_mask);
52     act.sa_handler = touchfile;
53     
54     sigaction(SIGALRM,&act,NULL);
55     
56     while(1){
57         //每隔1分鐘在/home/wang/log下建立檔案
58         sleep(1);
59         //do sth
60     }
61     //退出
62     
63     return 0;
64 }      

>gcc daemon.c

>./a.out

(打開另一個終端,ps aux檢視a.out為守護程序,然後在原終端點叉子關閉a.out,在後打開的終端檢視a.out仍然存在。,另外要進入log檔案夾下檢視目錄是否仍然在目錄。)

3、守護程序擴充了解

擴充了解:

  通過nohup指令也可以達到守護程序建立的效果。

  用法:nohup cmd [ > 1.log ] &

    nohup指令會讓cmd收不到SIGHUP信号

    & 代表背景運作

>touch daemon1.c

>vi daemon1.c

1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<fcntl.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<sys/types.h>
 7 #include<time.h>
 8 
 9 
10 int main(int argc, char *argv[])
11 {
12     char strfileName[256] = {0};
13     
14     while(1){
15         memset(strFileName,0x00,sizeof(strFileName));
16         sprintf(strFileName,"%s/log/zhen2.%ld",getenv("HOME"),time(NULL));
17         int fd = open(strFileName,O_RDONLY|O_CREAT,0664);
18         if(fd < 0){
19             perror("open err");
20             exit(1);
21         }
22         close(fd);
23         sleep(5);
24     }
25     
26     return 0;
27 }      

>gcc daemon1.c

>./a.out

>nohup ./a.out &

(顯示:[1] 3451 忽略輸入并把輸出追加到'nohup.out';打開另一個終端輸入ps aux檢視,然後再終端點選叉子關閉,後打開的程序輸入ps aux發現a.out仍然活着,另外要進入log檔案夾下檢視目錄是否仍然在建立目錄。)

4、線程有關的概念

5、線程的優點和缺點

6、建立一個線程

7、線程的退出

8、線程的回收

9、殺死線程

10、線程分離

11、線程屬性設定分離

12、線程注意事項

13、線程同步的概念

14、mutex相關的函數