本文最后更新于2017年7月21日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!
=Start=
缘由:
要实现一个daemon程序,提供类似Nginx的reload功能(即,对其发送USR1信号,它会自动重新加载配置文件并实时生效),信号处理部分已经知道该如何处理了,现在就是在接收到信号后去读取文件内容并更新全局变量使即时生效。
正文:
参考解答:
初始代码是这样的:
&
{
/* some code here */
char *line = NULL, *index = NULL;
char pid_str[1024] = {0}, *pid_str_p, *token = NULL, delim[] = ", ";
size_t len = 0;
ssize_t nread;
int i = 0, pid;
while ((nread = getline(&line, &len, fp)) != -1) {
if (strncmp(line, "pid", 3) == 0) {
index = strchr(line, '=');
if (index) {
strncpy(pid_str, index+1, nread-6);
pid_str_p = pid_str;
token = strsep(&pid_str_p, delim);
while(token != NULL){
if (token[0] != '\0') {
pid = (int) strtol(token, NULL, 10);
if (pid <= 0 || pid == (int)LONG_MIN || pid == (int)LONG_MAX) {
} else {
if (i < MAX_PID_COUNT-1) {
pid_list[i++] = pid;
}
}
}
token = strsep(&pid_str_p, delim);
}
}
break;
}
}
/* some code here */
}
参考链接:
=END=