10.21 Operation Control Signal

Source: Internet
Author: User


The POSIX.1 uses six signals to achieve job control:

    • SIGCHLD child process has stopped or terminated
    • Sigcont the already stopped process continues to run
    • SIGSTOP Stop process signal (cannot be captured or ignored)
    • SIGTSTP Interactive Stop Signal
    • Sigttin Background Process Group members read from control terminal
    • Sigttou background Process Group members write to control terminal
      In addition to signal SIGCHLD, many applications do not handle the above signals: the interactive shell will usually complete all the work necessary to process the above signals. When we enter a pending character (usually Control-z), SIGTSTP is sent to all processes in the foreground process group. When we tell the shell to resume the operation of a job, the shell will send a sigcont signal to all processes in the specified job, similarly, if Sigttin or Sigttou is sent to the process, the process will be stopped by default. The job control shell will recognize this event and notify us.
      There is still some interaction between the task control signals, and when any one of the four Stop signals (Sigstop,sigtstp,sigttin,sigttou) is generated, any sigcont signals that are in the suspended state for the same process will be discarded, similarly, When the Sigcont signal is generated, any stop signals that are in the suspended state of the same process are discarded.
      Note that the default behavior of the Sigcont is to continue running a process that is in a stopped state, otherwise, the signal will be ignored, normally we do not have to do any processing of this signal, when the sigcont signal is generated, the process will continue to run, that is, the signal is blocked or ignored.
Example

The program in Figure 10.31 illustrates the normal code sequence when a process handles job control. The program simply replicates its standard input to its standard output, but the annotations given in the signal processing function are appropriate for the typical action performed by the program that manages the screen.
"' #include" apue.h "

Define Buffsize 1024

static void sig_tstp (int signo)/signal handler for SIGSTOP/
{
sigset_t Mask;
/* ... move cursor to lower left Corner,reset TTY mode ... /
/
Unblock sigstop,since it ' s blocked while we ' re handling it */
Sigempty (&mask);
Sigaddset (&mask, SIGSTOP);
Sigprocmask (Sig_unblock, &mask, NULL);

signal(SIGTSTP, SIG_DFL); /*reset disposition to default */kill(getpid(), SIGTSTP);  /*and send the signal to ourself *//* we won‘t return from the kill until we‘re continued */signal(SIGTSTP, sig_tstp);/* ... reset tty mode, redraw screen ... */

}

int main (void)
{
int n;
Char Buf[buffsize];

/* * only catch SIGTSTP if we‘re running with a jb-control shell.  */if(signal(SIGTSTP, SIG_IGN) == SIG_DFL){    signal(SIGTSTP, sig_tstp);}while((n = read(STDIN_FILENO, buf, BUFFSIZE) > 0){    if(write(STDOUT_FILENO, buf, n) != n)    {        err_sys("write error");    }}if(n < 0)    err_sys("read error");exit(0);

}
"'
Figure 10.31 What to handle SIGTSTP
When the program in Figure 10.31 begins to run, the capture signal SIGTSTP is set only when the SIGTSTP signal is processed SIG_DFL, for the following reasons:

    • When the above program is started by a program that does not support job control (for example,/bin/sh), the processing of the signal will be set to sig_ign; in fact, the shell does not display the signal to ignore, init set three job control signal (Sigtstp,sigttin, Sigttou) is handled by Sig_ign, which is inherited by all login shells
    • Only the shell with the job control will reset the three signals to be handled in SIG_DFL.
      When we enter the pending signal, the process receives the SIGTSTP signal and the signal processing function is called, at which point we will perform any terminal-related processing: such as moving the cursor to the lower-left corner, resuming the terminal mode, and so on, and then resetting its processing mode to the default processing (stop process) And when the signal is unblocked, we send the same signal sigtstp to ourselves. We must unblock the signal because we are currently processing the same signal and the system will automatically block the signal when it is captured. At this point, the system will stop running the process. Follow-up will only resume when the process receives the sigcont signal, we do not capture the sigcont signal, because the sigcont signal is handled by default to resume the running process, at which point the program will continue to run as if it had just returned from the kill function. When the program resumes running, we will reset the processing of the signal SIGTSTP and perform any terminal processing we want (for example, we can redraw the screen).


From for notes (Wiz)

10.21 Operation Control Signal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.