Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
8e48ab34
Commit
8e48ab34
authored
Oct 24, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解决defunct进程的问题
parent
1f89a868
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
68 行增加
和
39 行删除
+68
-39
server/Process.cpp
+67
-38
server/Process.h
+1
-1
没有找到文件。
server/Process.cpp
查看文件 @
8e48ab34
...
...
@@ -34,9 +34,8 @@
#include "Util/File.h"
#include "Util/logger.h"
#include "Util/uv_errno.h"
#include "
Util/TimeTicker
.h"
#include "
Thread/WorkThreadPool
.h"
#include "Process.h"
#include "Poller/Timer.h"
using
namespace
toolkit
;
void
Process
::
run
(
const
string
&
cmd
,
const
string
&
log_file_tmp
)
{
...
...
@@ -46,12 +45,11 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
throw
std
::
runtime_error
(
StrPrinter
<<
"fork child process falied,err:"
<<
get_uv_errmsg
());
}
if
(
_pid
==
0
)
{
//子进程
//子进程关闭core文件生成
struct
rlimit
rlim
=
{
0
,
0
};
setrlimit
(
RLIMIT_CORE
,
&
rlim
);
//在启动子进程时,暂时禁用SIGINT、SIGTERM信号
// ignore the SIGINT and SIGTERM
signal
(
SIGINT
,
SIG_IGN
);
signal
(
SIGTERM
,
SIG_IGN
);
...
...
@@ -109,24 +107,73 @@ void Process::run(const string &cmd, const string &log_file_tmp) {
InfoL
<<
"start child proces "
<<
_pid
;
}
void
Process
::
kill
(
int
max_delay
)
{
if
(
_pid
<=
0
)
{
/**
* 获取进程是否存活状态
* @param pid 进程号
* @param exit_code_ptr 进程返回代码
* @param block 是否阻塞等待
* @return 进程是否还在运行
*/
static
bool
s_wait
(
pid_t
pid
,
int
*
exit_code_ptr
,
bool
block
)
{
if
(
pid
<=
0
)
{
return
false
;
}
int
status
=
0
;
pid_t
p
=
waitpid
(
pid
,
&
status
,
block
?
0
:
WNOHANG
);
int
exit_code
=
(
status
&
0xFF00
)
>>
8
;
if
(
exit_code_ptr
){
*
exit_code_ptr
=
(
status
&
0xFF00
)
>>
8
;
}
if
(
p
<
0
)
{
WarnL
<<
"waitpid failed, pid="
<<
pid
<<
", err="
<<
get_uv_errmsg
();
return
false
;
}
if
(
p
>
0
)
{
InfoL
<<
"process terminated, pid="
<<
pid
<<
", exit code="
<<
exit_code
;
return
false
;
}
//WarnL << "process is running, pid=" << _pid;
return
true
;
}
static
void
s_kill
(
pid_t
pid
,
int
max_delay
,
bool
force
){
if
(
pid
<=
0
)
{
//pid无效
return
;
}
if
(
::
kill
(
_pid
,
SIGTERM
)
==
-
1
)
{
WarnL
<<
"kill process "
<<
_pid
<<
" falied,err:"
<<
get_uv_errmsg
();
}
else
{
//等待子进程退出
auto
pid
=
_pid
;
EventPollerPool
::
Instance
().
getPoller
()
->
doDelayTask
(
max_delay
,[
pid
](){
//最多等待2秒,2秒后强制杀掉程序
if
(
waitpid
(
pid
,
NULL
,
WNOHANG
)
==
0
)
{
::
kill
(
pid
,
SIGKILL
);
WarnL
<<
"force kill process "
<<
pid
;
}
if
(
::
kill
(
pid
,
force
?
SIGKILL
:
SIGTERM
)
==
-
1
)
{
//进程可能已经退出了
WarnL
<<
"kill process "
<<
pid
<<
" failed:"
<<
get_uv_errmsg
();
return
;
}
if
(
force
){
//发送SIGKILL信号后,阻塞等待退出
s_wait
(
pid
,
NULL
,
true
);
DebugL
<<
"force kill "
<<
pid
<<
" success!"
;
return
;
}
//发送SIGTERM信号后,2秒后检查子进程是否已经退出
WorkThreadPool
::
Instance
().
getPoller
()
->
doDelayTask
(
max_delay
,[
pid
](){
if
(
!
s_wait
(
pid
,
nullptr
,
false
))
{
//进程已经退出了
return
0
;
});
}
//进程还在运行
WarnL
<<
"process still working,force kill it:"
<<
pid
;
s_kill
(
pid
,
0
,
true
);
return
0
;
});
}
void
Process
::
kill
(
int
max_delay
,
bool
force
)
{
if
(
_pid
<=
0
)
{
return
;
}
s_kill
(
_pid
,
max_delay
,
force
);
_pid
=
-
1
;
}
...
...
@@ -134,28 +181,10 @@ Process::~Process() {
kill
(
2000
);
}
Process
::
Process
()
{
}
Process
::
Process
()
{}
bool
Process
::
wait
(
bool
block
)
{
if
(
_pid
<=
0
)
{
return
false
;
}
int
status
=
0
;
pid_t
p
=
waitpid
(
_pid
,
&
status
,
block
?
0
:
WNOHANG
);
_exit_code
=
(
status
&
0xFF00
)
>>
8
;
if
(
p
<
0
)
{
WarnL
<<
"waitpid failed, pid="
<<
_pid
<<
", err="
<<
get_uv_errmsg
();
return
false
;
}
if
(
p
>
0
)
{
InfoL
<<
"process terminated, pid="
<<
_pid
<<
", exit code="
<<
_exit_code
;
return
false
;
}
//WarnL << "process is running, pid=" << _pid;
return
true
;
return
s_wait
(
_pid
,
&
_exit_code
,
block
);
}
int
Process
::
exit_code
()
{
...
...
server/Process.h
查看文件 @
8e48ab34
...
...
@@ -36,7 +36,7 @@ public:
Process
();
~
Process
();
void
run
(
const
string
&
cmd
,
const
string
&
log_file
);
void
kill
(
int
max_delay
);
void
kill
(
int
max_delay
,
bool
force
=
false
);
bool
wait
(
bool
block
=
true
);
int
exit_code
();
private
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论