Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 我能';无法理解sigaction()结果_Linux_Unix_Signals_System_Eof - Fatal编程技术网

Linux 我能';无法理解sigaction()结果

Linux 我能';无法理解sigaction()结果,linux,unix,signals,system,eof,Linux,Unix,Signals,System,Eof,为什么在处理SIGCHLD信号后EOF在标准数据缓冲器中?我不知道为什么会这样。或者可能我不知道如何使用sigaction函数?fgetc()返回EOF,如果文件位于文件末尾或尝试读取字符时出错。在这种情况下,read()被信号中断是一个错误,而sigaction()的sau RESTART选项可防止此错误 要区分EOF和error,请使用feof()或ferror(),或测试变量errno错误号对于EOF情况将为0,对于错误(EINTR在这种情况下)将为非零。哦,我添加了一个标志选项“SA_R

为什么在处理SIGCHLD信号后EOF在标准数据缓冲器中?我不知道为什么会这样。或者可能我不知道如何使用sigaction函数?

fgetc()
返回
EOF
,如果文件位于文件末尾尝试读取字符时出错。在这种情况下,
read()
被信号中断是一个错误,而
sigaction()
sau RESTART
选项可防止此错误


要区分EOF和error,请使用
feof()
ferror()
,或测试变量
errno
<代码>错误号对于EOF情况将为
0
,对于错误(
EINTR
在这种情况下)将为非零。

哦,我添加了一个标志选项“SA_RESTART”,然后它就工作了。这是什么。。
#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");
  }
}
[process id]
EOF