The correlation function of operation process signal in C language using the detailed _c language

Source: Internet
Author: User

C language Signal () function: Set signal processing mode
header file:

#include <signal.h>

To define a function:

void (*signal (int signum, void (* handler) (int))) (int);

Function Description: Signal () sets the processing function of the signal according to the signal number specified by the parameter signum. When the specified signal arrives, it jumps to the function execution specified by the parameter handler. If the parameter handler is not a function pointer, it must be one of the following two constants:
1, Sig_ign ignored the parameters Signum specified signal.
2, SIG_DFL the parameter signum specified signal reset to the core preset signal processing.

For a signal number and description, please refer to Appendix D

Return value: Returns the previous signal processing function pointer and returns SIG_ERR (-1) If there is an error.

Additional note: After the signal occurs to the custom handler processing function execution, the system will automatically swap this handler back to the original system preset processing, if you want to change this operation, please use Sigaction ().

C-language Kill () function: Send a signal to a specified process
header file:

#include <sys/types.h>  #include <signal.h>

definition function: int kill (pid_t pid, int sig);

Function Description: Kill () can be used to send parameter sig specified signal to the parameter PID specified by the process. There are several conditions for parameter PID:
1, PID>0 will signal to process identification code for the PID process.
2, pid=0 the signal to the current process with the same process group of all processes
3. Pid=-1 transmits signal broadcast to all processes in the system
4, Pid<0 will signal to the process group identification code for the PID absolute value of all process parameters SIG representative signal number can refer to Appendix D

Return value: Returns 0 if the execution succeeds, or 1 if there is an error.

Error code:
1, einval parameter sig illegal
2, esrch parameter PID specified process or process group does not exist
3, eperm not enough to transmit the signal to the specified process

Example

#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ wait.h>
Main ()
{
  pid_t pid;
  int status;
  if (!) ( pid= Fork ())
  {
    printf ("Hi I am Child process!\n");
    Sleep (a);
    return;
  }
  else
  {
    printf ("Send signal to child process (%d) \ n", pid);
    Sleep (1);
    Kill (PID, SIGABRT);
    Wait (&status);
    if (wifsignaled (status))
      printf ("Chile process receive signal%d\n", wtermsig (status);
  }

Perform:

Sen signal to Child process (3170) Hi I am child process! Child process receive
signal 6

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.