我无法理解 sigaction() 结果
I can't understand sigaction() result
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void handler(int sig)
{
pid_t pid;
int status;
while( (pid = waitpid(-1, &status, WNOHANG)) > 0 )
printf("%d\n", pid);
}
int main(void)
{
struct sigaction act;
pid_t pid;
int ch;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD, &act, 0);
pid = fork();
if( pid == 0 ) {
exit(0);
}
else {
if( (ch = fgetc(stdin)) == EOF )
printf("EOF\n");
}
}
你好,我想了解一下sigaction函数。如果我执行这个程序,结果如下。
[process id]
EOF
为什么在处理 SIGCHLD 信号后 EOF 在标准输入缓冲区中?我不知道为什么会这样。或者我不知道如何使用 sigaction 函数?
fgetc()
returns EOF
如果文件在文件末尾 或 尝试读取字符时发生错误.在这种情况下,read()
被信号中断是一个错误,而 sigaction()
的 SA_RESTART
选项可防止此错误。
要区分EOF和错误,使用feof()
或ferror()
,或测试变量errno
。对于 EOF 情况,errno
将为 0
,对于错误则为非零(在本例中为 EINTR
)。
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
void handler(int sig)
{
pid_t pid;
int status;
while( (pid = waitpid(-1, &status, WNOHANG)) > 0 )
printf("%d\n", pid);
}
int main(void)
{
struct sigaction act;
pid_t pid;
int ch;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD, &act, 0);
pid = fork();
if( pid == 0 ) {
exit(0);
}
else {
if( (ch = fgetc(stdin)) == EOF )
printf("EOF\n");
}
}
你好,我想了解一下sigaction函数。如果我执行这个程序,结果如下。
[process id]
EOF
为什么在处理 SIGCHLD 信号后 EOF 在标准输入缓冲区中?我不知道为什么会这样。或者我不知道如何使用 sigaction 函数?
fgetc()
returns EOF
如果文件在文件末尾 或 尝试读取字符时发生错误.在这种情况下,read()
被信号中断是一个错误,而 sigaction()
的 SA_RESTART
选项可防止此错误。
要区分EOF和错误,使用feof()
或ferror()
,或测试变量errno
。对于 EOF 情况,errno
将为 0
,对于错误则为非零(在本例中为 EINTR
)。