Reprint--sigprocmask Blocking Process

Source: Internet
Author: User
Tags new set semaphore

muge0913

Link: Linux c sigprocmask blocking process

1, sometimes do not want to receive the signal immediately stop the current execution, to deal with the signal, but also do not want to ignore the signal, but delay a period of time to call the signal processing function. This situation is achieved by blocking the signal.

2, signal blocking and ignoring the difference between the signal.

The concept of blocking and ignoring signals are different. The operating system does not speak the signal before it is unblocked by the process, the blocked signal does not affect the behavior of the process, and the signal is only temporarily blocked from passing. When a process ignores a signal, the signal is passed out, but the process discards the signal.

3, the signal blocking system call, they all play the role of blocking, they are not collaborative use.

[CPP]View Plaincopyprint?
    1. #include <signal.h>
    2. int Sigprocmask (UBT how,const sigset_t*set,sigset_t *oldset);
    3. int Sigsuspend (const sigset_t*sigmask);

The Sigprocmask sets the processing mode (blocking or non-blocking) of the signal within the signal shielding set.

Parameters:

How: Used to specify the way the signal is modified, there may be a choice of three

Sig_block//adds the signal contained in the set point to the current signal mask. That is, the signal mask and set signal set are performed or manipulated.

sig_unblock//removes the signal contained in the signal set pointed to by set from the current signal mask. That is, the signal mask and set are performed and manipulated.

Sig_setmask//Sets the value of the set to the new process signal mask. That is, set assigns a signal mask to a value operation.

Set: As a pointer to the signal set, this refers to the new set of signals, if you want to read only the current mask value, you can set it to null.

Oldset: Also a pointer to the signal set, where the original signal set is stored. can be used to detect what signal is present in the signal mask.

Return Description:

When executed successfully, returns 0. The failed return -1,errno is set to Einval.

Sigprocmask Example (demo adds a signal mask):

[CPP]View Plaincopyprint?
  1. #include <stdio.h>
  2. #include <signal.h>
  3. void Checkset ();
  4. void Main ()
  5. {
  6. Sigset_tblockset;
  7. Sigemptyset (&blockset);
  8. Sigaddset (&blockset,sigint);
  9. Sigaddset (&BLOCKSET,SIGTSTP);
  10. Checkset ();
  11. Sigprocmask (Sig_setmask,&blockset,null);
  12. Checkset ();
  13. Sigaddset (&blockset,sigterm);
  14. Sigprocmask (Sig_block,&blockset,null);
  15. Checkset ();
  16. Sigdelset (&blockset,sigterm);
  17. Sigprocmask (Sig_unblock,&blockset,null);
  18. Checkset ();
  19. }
  20. void Checkset ()
  21. {
  22. Sigset_tset set;
  23. printf ("checksetstart:\n");
  24. if (Sigprocmask (0,null,&set) <0)
  25. {
  26. printf ("Checksetsigprocmask error!!  \ n ");
  27. Exit (0);
  28. }
  29. if (Sigismember (&set,sigint))
  30. printf ("sigint\n");
  31. if (Sigismember (&SET,SIGTSTP))
  32. printf ("sigtstp\n");
  33. if (Sigismember (&set,sigterm))
  34. printf ("sigterm\n");
  35. printf ("checksetend\n");
  36. }

Sigprocmask Example (Demo adds a part of the code without being disturbed by the signal):

[CPP]View Plaincopyprint?
    1. #include <stdio.h>
    2. #include <signal.h>
    3. void Checkset ();
    4. void Func ();
    5. void Main ()
    6. {
    7. Sigset_tblockset,oldblockset,pendmask;
    8. printf ("pid:%ld\n", (Long) getpid ());
    9. Signal (SIGINT,FUNC); //Semaphore capture function, Snap to SIGINT, jump to function pointer func execution
    10. Sigemptyset (&blockset); //Initialize semaphore set
    11. Sigaddset (&BLOCKSET,SIGTSTP); //Add SIGTSTP to the semaphore set
    12. Sigaddset (&blockset,sigint); //Add SIGINT to the semaphore set
    13. Sigprocmask (Sig_setmask,&blockset,&oldblockset); //Block the SIGINT,SIGTSTP in Blockset and save the current signal screen word
    14. / * will not be disturbed by the signal when executing the following program * /
    15. Checkset ();
    16. Sleep (5);
    17. Sigpending (&pendmask); //Check the signal is pending
    18. The IF (Sigismember (&pendmask,sigint)) //sigint is pending. The so-called unresolved means that Sigquit is blocked and has not been processed.
    19. printf ("sigintpending\n");
    20. / * End of interruption * /
    21. Sigprocmask (Sig_setmask,&oldblockset,null); //Recover the Masked signal SIGINT SIGTSTP
    22. printf ("sigintunblocked\n");
    23. Sleep (6);
    24. }
    25. void Checkset ()
    26. {
    27. Sigset_tset;
    28. printf ("checksetstart:\n");
    29. if (Sigprocmask (0,null,&set) <0)
    30. {
    31. printf ("Checksetsigprocmask error!!  \ n ");
    32. Exit (0);
    33. }
    34. if (Sigismember (&set,sigint))
    35. printf ("sigint\n");
    36. if (Sigismember (&SET,SIGTSTP))
    37. printf ("sigtstp\n");
    38. if (Sigismember (&set,sigterm))
    39. printf ("sigterm\n");
    40. printf ("checksetend\n");
    41. }
    42. void Func ()
    43. {
    44. printf ("hellofunc\n");
    45. }

Reprint--sigprocmask Blocking Process

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.