C ++ processes kill and CTRL + C signals

Source: Internet
Author: User
Tags sigint signal
  1. From:
    Http://blog.csdn.net/xian0617/article/details/6689357
  2. /*
  3. * Waitquitsignal. h
  4. *
  5. * Created on: Aug 14,201 1
  6. * Author: xian0617
  7. */
  8. # Ifndef waitquitsignal_h _
  9. # Define waitquitsignal_h _
  10. # Include <signal. h>
  11. # Include <time. h>
  12. Class waitquitsignal {
  13. Public:
  14. Static void Init ();
  15. Static bool wait (bool & flag );
  16. PRIVATE:
  17. Static sigset_t m_wait_mask;
  18. Static struct timespec m_time;
  19. };
  20. # Endif/* waitquitsignal_h _*/

/* <Br/> * waitquitsignal. h <br/> * created on: Aug 14,201 1 <br/> * Author: xian0617 <br/> */</P> <p> # ifndef waitquitsignal_h _ <br/> # define waitquitsignal_h _ <br/> # include <signal. h> <br/> # include <time. h> <br/> class waitquitsignal {<br/> Public: <br/> static void Init (); <br/> static bool wait (bool & flag ); <br/> PRIVATE: <br/> static sigset_t m_wait_mask; <br/> static struct timespec m_time; <br/> }; </P> <p> # endif/* waitquitsignal_h _ */<br/>

[CPP]
View plaincopyprint?
  1. /*
  2. * Waitquitsignal. cpp
  3. *
  4. * A thread in Linux is essentially a light weighted process. When a thread is generated, a corresponding process control structure is generated,
  5. * This structure shares the same process memory space with the parent thread's process control structure. At the same time, the process control structure of the new thread will be from the parent thread (process)
  6. * Obtain the same process information, such as opening the file list and blocking mask. Because we modified the signal blocking mask after the subthread was generated
  7. * Code. At this moment, the subthread uses the original process information of the main thread. Therefore, the subthread will still respond to the SIGINT and sigterm signals. Therefore, when
  8. * When we use Ctrl + C to send a SIGINT signal, the main process does not process the signal, and the sub-process (thread) performs default processing, that is, exit.
  9. * When a child process exits, it sends a sigchld signal to the parent process (thread), indicating that the child process exits. Because the signal is not blocked
  10. * The main process (thread) also exits immediately, and the preceding running condition occurs. Therefore, one solution to this problem is to set the signal before the subthread is generated,
  11. * Set the signal within the subthread.
  12. * Created on: Aug 14,201 1
  13. * Author: xian0617
  14. */
  15. # Include <iostream>
  16. # Include "waitquitsignal. H"
  17. Sigset_t waitquitsignal: m_wait_mask;
  18. Struct timespec waitquitsignal: m_time;
  19. // Call this before thread create
  20. Void waitquitsignal: Init (){
  21. Try {
  22. Signal (sigkill, sig_ign );
  23. Sigemptyset (& m_wait_mask );
  24. Sigaddset (& m_wait_mask, SIGINT );
  25. Sigaddset (& m_wait_mask, sigquit );
  26. Sigaddset (& m_wait_mask, sigterm );
  27. Pthread_sigmask (sig_block, & m_wait_mask, 0 );
  28. } Catch (STD: exception & E ){
  29. STD: cerr <"exception:" <E. What () <STD: Endl;
  30. }
  31. M_time. TV _sec = 0;
  32. M_time. TV _nsec = 0;
  33. }
  34. Bool waitquitsignal: Wait (bool & flag ){
  35. Try {
  36. Siginfo_t SIG;
  37. Switch (sigtimedwait (& m_wait_mask, & Sig, & m_time )){
  38. Case SIGINT:
  39. Case sigquit:
  40. Case sigterm:
  41. Flag = false;
  42. Break;
  43. Default:
  44. Break;
  45. }
  46. } Catch (STD: exception & E ){
  47. STD: cerr <"exception:" <E. What () <STD: Endl;
  48. }
  49. Return flag;
  50. }

/* <Br/> * waitquitsignal. CPP <br/> * in Linux, a thread is essentially a light weighted process. When a thread is generated, a corresponding process control structure is generated, <br/> * This structure shares the same process memory space with the parent thread's process control structure. At the same time, the process control structure of the new thread will copy the same process information from the parent thread (process) <br/> *, such as opening the file list and blocking mask. Because the signal blocking mask is modified after the subthread is generated <br/> * code, the subthread uses the original process information of the main thread, therefore, the sub-thread will still respond to the SIGINT and sigterm signals. Therefore, when <br/> * we use Ctrl + C to send a SIGINT signal, the main process will not process the signal, the sub-process (thread) performs default processing, that is, exit. <Br/> * when a child process exits, it sends a sigchld signal to the parent process (thread), indicating that the child process exits. Because the signal is not blocked, as a result, <br/> * The main process (thread) also exits immediately and the preceding running condition occurs. Therefore, one solution to this problem is to set the signal before the subthread is generated. <br/> * or set the signal inside the subthread. <Br/> * created on: Aug 14,201 1 <br/> * Author: xian0617 <br/> */</P> <p> # include <iostream> <br/> # include "waitquitsignal. H "<br/> sigset_t waitquitsignal: m_wait_mask; <br/> struct timespec waitquitsignal: m_time; <br/> // call this before thread create <br/> void waitquitsignal: Init () {<br/> try {<br/> signal (sigkill, sig_ign ); <br/> sigemptyset (& m_wait_mask); <br/> sigaddset (& m_wait_mask, SIGINT); <br/> sigaddset (& m_wait_mask, sigquit ); <br/> sigaddset (& m_wait_mask, sigterm); <br/> pthread_sigmask (sig_block, & m_wait_mask, 0); <br/>} catch (STD: exception & E) {<br/> STD: cerr <"exception:" <E. what () <STD: Endl; <br/>}< br/> m_time. TV _sec = 0; <br/> m_time. TV _nsec = 0; <br/>}< br/> bool waitquitsignal: Wait (bool & flag) {<br/> try {<br/> siginfo_t SIG; <br/> switch (sigtimedwait (& m_wait_mask, & Sig, & m_time) {<br/> case SIGINT: <br/> case sigquit: <br/> case sigterm: <br/> flag = false; <br/> break; <br/> default: <br/> break; <br/>}</P> <p>} catch (STD: exception & E) {<br/> STD: cerr <"exception: "<E. what () <STD: Endl; <br/>}< br/> return flag; <br/>}< br/>

Sample Model

[CPP]
View plaincopyprint?
  1. Int main (INT argc, char ** argv ){
  2. // Step 1 init
  3. Waitquitsignal: Init ();
  4. // Step 2 create subthread
  5. // Step 3 Wait Signal
  6. While (waitquitsignal: Wait (FLAG )){
  7. Sleep (1 );
  8. }
  9. // Step 4 deal quit job
  10. // Save ();
  11. }

Int main (INT argc, char ** argv) {<br/> // Step1 init <br/> waitquitsignal: Init (); <br/> // step2 create subthread </P> <p> // Step3 wait signal <br/> while (waitquitsignal: Wait (FLAG )) {<br/> sleep (1); <br/>}< br/> // step4 deal quit job <br/> // save (); <br/>}

Related Article

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.