nginx 通過Signal傳送控制訊號

2021-10-02 14:17:31 字數 1892 閱讀 8726

nginx 通過signal傳送控制訊號,類似nginx -s reload的流程如下:

1 訊號結構體

// 訊號結構體

typedef struct ngx_signal_t;

2 在nginx的main()函式裡呼叫ngx_get_options()

int ngx_cdecl

main(int argc, char *const *ar**)

...}

3 ngx_get_options()函式解析命令列引數

static ngx_int_t

ngx_get_options(int argc, char *const *ar**)

else if (ar**[++i]) else

if (ngx_strcmp(ngx_signal, "stop") == 0

|| ngx_strcmp(ngx_signal, "quit") == 0

|| ngx_strcmp(ngx_signal, "reopen") == 0

|| ngx_strcmp(ngx_signal, "reload") == 0)

ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal);

return ngx_error;

default:

ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1));

return ngx_error;}}

...return ngx_ok;

}

在nginx main函式呼叫ngx_get_options()函式解析,然後呼叫ngx_signal_process()函式

4 ngx_signal_process()函式

① 開啟工作程序檔案,讀取pid

② 呼叫ngx_os_signal_process函式處理

ngx_int_t

ngx_signal_process(ngx_cycle_t *cycle, char *sig)

n = ngx_read_file(&file, buf, ngx_int64_len + 2, 0);

if (ngx_close_file(file.fd) == ngx_file_error)

if (n == ngx_error)

while (n-- && (buf[n] == cr || buf[n] == lf))

pid = ngx_atoi(buf, ++n);

if (pid == (ngx_pid_t) ngx_error)

return ngx_os_signal_process(cycle, sig, pid);

}

5 ngx_os_signal_process()函式

遍歷signals陣列,根據給定訊號name,找到對應signo,呼叫kill命令,向該pid傳送signo號訊號

ngx_int_t

ngx_os_signal_process(ngx_cycle_t *cycle, char *name, ngx_pid_t pid)

ngx_log_error(ngx_log_alert, cycle->log, ngx_errno,

"kill(%p, %d) failed", pid, sig->signo);}}

return 1;

}

linux下執行緒通過signal通訊例項

1.linux系統中根據需求可選取不同的程序和執行緒間通訊的方式,羅列如下 調式linux下多執行緒間通訊方式 int databuff 1000000 int waction 0,raction 0 int size 100 pthread cond t buffisnotempty pthrea...

通過adb 傳送廣播

adb shell am broadcast options 作用 傳送乙個廣播 舉例 adb shell am broadcast a send by frank test 傳送乙個廣播send by frank test 舉例 adb shell am broadcast a android.i...

通過adb 傳送廣播

adb shell am broadcast options 作用 傳送乙個廣播 舉例 adb shell am broadcast a send by frank test 傳送乙個廣播send by frank test 舉例 adb shell am broadcast a android.i...