Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / examples / APG / Signals / SigInfo.cpp
blob557b2008fcea284e8ee8aadab316907464612117
1 #include "ace/Log_Msg.h"
2 #include "ace/Reactor.h"
3 #include "ace/Event_Handler.h"
4 #include "ace/Signal.h"
5 #include "ace/OS_NS_unistd.h"
6 #include "ace/OS_NS_string.h"
8 #if !defined (ACE_LACKS_UNIX_SIGNALS)
10 // Listing 1 code/ch11
11 class MySignalHandler : public ACE_Event_Handler
13 public:
14 MySignalHandler () : ACE_Event_Handler()
15 { }
17 // Listing A code/ch11
18 int handle_signal (int signum, siginfo_t * siginfo = 0, ucontext_t * = 0)
20 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Received signal [%S]\n"),
21 signum));
22 if (siginfo == 0)
24 ACE_DEBUG ((LM_INFO,
25 ACE_TEXT ("No siginfo_t available for ")
26 ACE_TEXT ("signal [%S]\n"),
27 signum));
28 return 0;
30 // Listing A
31 #if defined (ACE_LINUX)
32 // Listing B code/ch11
33 ACE_DEBUG ((LM_INFO,
34 ACE_TEXT ("errno for this signal is %d [%s]\n"),
35 siginfo->si_errno,
36 ACE_OS::strerror (siginfo->si_errno)));
37 ACE_DEBUG ((LM_INFO,
38 ACE_TEXT ("signal was sent by process %d")
39 ACE_TEXT (" / user %d\n"),
40 siginfo->si_pid,
41 siginfo->si_uid));
43 switch (siginfo->si_code)
45 case SI_TIMER:
46 ACE_DEBUG ((LM_INFO, ACE_TEXT ("Timer expiration\n")));
47 break;
49 case SI_USER:
50 ACE_DEBUG ((LM_INFO,
51 ACE_TEXT ("Sent by kill, sigsend or raise\n")));
52 break;
54 case SI_KERNEL:
55 ACE_DEBUG ((LM_INFO,
56 ACE_TEXT ("Sent by kernel\n")));
57 break;
58 // ...
61 // Listing B
63 // Listing C code/ch11
64 switch (signum)
66 case SIGFPE:
67 switch (siginfo->si_code)
69 case FPE_INTDIV:
70 case FPE_FLTDIV:
71 ACE_DEBUG ((LM_INFO,
72 ACE_TEXT ("Divide by zero at %@\n"),
73 siginfo->si_addr));
74 break;
76 case FPE_INTOVF:
77 case FPE_FLTOVF:
78 ACE_DEBUG ((LM_INFO,
79 ACE_TEXT ("Numeric overflow at %@\n"),
80 siginfo->si_addr));
81 break;
83 // ...
85 break;
87 // Listing C
88 // Listing D code/ch11
89 case SIGSEGV:
90 switch (siginfo->si_code)
92 // ...
94 break;
95 // Listing D
97 // Listing E code/ch11
98 case SIGCHLD:
99 ACE_DEBUG ((LM_INFO,
100 ACE_TEXT ("A child process has exited\n")));
101 ACE_DEBUG ((LM_INFO,
102 ACE_TEXT ("The child consumed %l/%l time\n"),
103 siginfo->si_utime,
104 siginfo->si_stime));
105 ACE_DEBUG ((LM_INFO,
106 ACE_TEXT ("and exited with value %d\n"),
107 siginfo->si_status));
108 break;
109 // ...
111 // Listing E
112 #endif /* __linux__ */
113 return 0;
116 // Listing 1
118 #endif /* ACE_LACKS_UNIX_SIGNALS */
120 #if !defined (ACE_LACKS_UNIX_SIGNALS)
122 int ACE_TMAIN (int, ACE_TCHAR *[])
124 #if defined (ACE_LACKS_FORK)
125 //FUZZ: disable check_for_lack_ACE_OS
126 ACE_DEBUG ((LM_DEBUG,
127 "This example requires fork()\n"));
128 //FUZZ: enable check_for_lack_ACE_OS
129 #else
130 // Create a child process so that we can test our
131 // ability to handle SIGCHLD
133 // Listing 2 code/ch11
134 ACE_Sig_Set signalSet;
135 signalSet.fill_set ();
137 MySignalHandler h1;
138 ACE_Reactor::instance ()->register_handler (signalSet, &h1);
139 pid_t childPid = ACE_OS::fork ();
140 if (childPid == 0) // This is the parent process.
142 // Exclude B
143 ACE_OS::sleep (10);
144 return 100;
145 // Exclude B
147 ACE_Reactor::instance ()->run_reactor_event_loop ();
148 // Listing 2
150 #endif /* ACE_LACKS_FORK */
152 return 0;
155 #else
158 ACE_TMAIN (int, ACE_TCHAR *[])
160 ACE_DEBUG ((LM_DEBUG,
161 "This example does not work on this platform.\n"));
162 return 1;
165 #endif /* !ACE_LACKS_UNIX_SIGNALS */