加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Signal Handling--ref

发布时间:2021-01-25 08:01:09 所属栏目:Linux 来源:网络整理
导读:signal is a software interrupt delivered to a process. The operating system uses signals to report exceptional situations to an executing program. Some signals report errors such as references to invalid memory addresses; others report asy

void
handle_sigint (int signum)
{
/ We may have been waiting for input when the signal arrived,but we are no longer waiting once we transfer control. /
waiting_for_input = 0;
longjmp (return_to_top_level,1);
}

int
main (void)
{
...
signal (SIGINT,sigint_handler);
...
while (1) {
prepare_for_command ();
if (setjmp (return_to_top_level) == 0)
read_and_execute_command ();
}
}

/ Imagine this is a subroutine used by various commands. /
char *
read_data ()
{
if (input_from_terminal) {
waiting_for_input = 1;
...
waiting_for_input = 0;
} else {
...
}
}

sigprocmask,if you want to allow more signals of this type to arrive; see section.)

sa_maskmember of the action structure passed tosigactionto explicitly specify which signals should be blocked while the signal handler runs. These signals are in addition to the signal for which the handler was invoked,and any other signals that are normally blocked by the process. See section.

sigprocmaskinside the handler only affects what signals can arrive during the execution of the handler itself,not what signals can arrive once the handler returns.

sigactionto establish a handler for a signal that you expect to receive asynchronously,if you want your program to work properly on System V Unix. On this system,the handling of a signal whose handler was established withsignalautomatically sets the signal's action back toSIG_DFL,and the handler must re-establish itself each time it runs. This practice,while inconvenient,does work when signals cannot arrive in succession. However,if another signal can arrive right away,it may arrive before the handler can re-establish itself. Then the second signal would receive the default handling,which could terminate the process.

SIGCHLDthat compensates for the fact that the number of signals recieved may not equal the number of child processes generate them. It assumes that the program keeps track of all the child processes with a chain of structures as follows:

struct process
{
  struct process *next;
  /* The process ID of this child.  */
  int pid;
  /* The descriptor of the pipe or pseudo terminal
     on which output comes from this child.  */
  int input_descriptor;
  /* Nonzero if this process has stopped or terminated.  */
  sig_atomic_t have_status;
  /* The status of this child; 0 if running,otherwise a status value from waitpid.  */
  int status;
};                        

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读