TERMSIG(status),取使子进程结束的信号编号。
WTERMSIG(status) 取得子进程因信号而中止的信号代码,一般会先用 WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status) 若为当前暂停子进程返回的状态,则为真;对于这种情况可执行WSTOPSIG(status),取使子进程暂停的信号编号。
WSTOPSIG(status) 取得引发子进程暂停的信号代码,一般会先用 WIFSTOPPED 来判断后才使用此宏。
如果执行成功则返回子进程识别码(PID) ,如果有错误发生则返回
返回值-1。失败原因存于 errno 中。
fio使用waitpid的例子
* Run over the job map and reap the threads that have exited, if any.
?*/
static void reap_threads(unsigned int *nr_running, unsigned int *t_rate,
?unsigned int *m_rate)
{
...
realthreads = pending = cputhreads = 0;
for_each_td(td, i) {
int flags = 0;
?
/*
?* ->io_ops is NULL for a thread that has closed its
?* io engine
?*/
if (td->io_ops && !strcmp(td->io_ops->name, "cpuio"))
cputhreads++;
else
realthreads++;
...
flags = WNOHANG;
if (td->runstate == TD_EXITED)
flags = 0;
?
/*
?* check if someone quit or got killed in an unusual way
?*/
ret = waitpid(td->pid, &status, flags);
if (ret < 0) {
if (errno == ECHILD) {
log_err("fio: pid=%d disappeared %d\n",
(int) td->pid, td->runstate);
td->sig = ECHILD;
td_set_runstate(td, TD_REAPED);
goto reaped;
}
perror("waitpid");
} else if (ret == td->pid) {
if (WIFSIGNALED(status)) {
int sig = WTERMSIG(status);
?
if (sig != SIGTERM && sig != SIGUSR2)
log_err("fio: pid=%d, got signal=%d\n",
(int) td->pid, sig);
td->sig = sig;
td_set_runstate(td, TD_REAPED);
goto reaped;
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) && !td->error)
td->error = WEXITSTATUS(status);
?
td_set_runstate(td, TD_REAPED);
goto reaped;
}
}
?
...
让进程休眠
sleep函数将一个进程挂起一段指定的时间。sleep函数返回0,否则返回剩下的要休眠的秒数(是可能的,因为可能被信号中断而过早的返回)。
另外一个有用的函数是pause,该函数让调用函数休眠,直到该进程收到一个信号。
进程间的信号传递
note:由于时间关系,有空再补充。
源码fio-2.1.10
相关阅读: