1 |
//$Header$ |
2 |
//------------------------------------------------------------------------------------------------- |
3 |
//This file is part of "kicker", a program for spawning threads and/or processes periodically in |
4 |
//order to unstall certain Windows programs and scripts. The cause of the stall is unknown, but |
5 |
//it is known that spawning threads and/or processes unstalls the system. |
6 |
//------------------------------------------------------------------------------------------------- |
7 |
//This source code and any program in which it is compiled/used is provided under the MIT License, |
8 |
//reproduced below. |
9 |
//------------------------------------------------------------------------------------------------- |
10 |
//Permission is hereby granted, free of charge, to any person obtaining a copy of |
11 |
//this software and associated documentation files(the "Software"), to deal in the |
12 |
//Software without restriction, including without limitation the rights to use, |
13 |
//copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the |
14 |
//Software, and to permit persons to whom the Software is furnished to do so, |
15 |
//subject to the following conditions : |
16 |
// |
17 |
//The above copyright notice and this permission notice shall be included in all |
18 |
//copies or substantial portions of the Software. |
19 |
// |
20 |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21 |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22 |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE |
23 |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24 |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25 |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
26 |
//SOFTWARE. |
27 |
//------------------------------------------------------------------------------------------------- |
28 |
#include <assert.h> |
29 |
#include <malloc.h> |
30 |
#include <process.h> |
31 |
#include <stdio.h> |
32 |
#include <string.h> |
33 |
#include <time.h> |
34 |
#include <windows.h> |
35 |
|
36 |
#define NELEM(A) (sizeof(A)/sizeof(A[0])) |
37 |
|
38 |
void emit_hline_std(void) |
39 |
{ |
40 |
printf("----------------------------------------------------------------------------------\n"); |
41 |
} |
42 |
|
43 |
void emit_mit_license(void) |
44 |
{ |
45 |
const char *ref[] = |
46 |
{ |
47 |
"Permission is hereby granted, free of charge, to any person obtaining a copy of", |
48 |
"this software and associated documentation files(the \"Software\"), to deal in the", |
49 |
"Software without restriction, including without limitation the rights to use,", |
50 |
"copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the", |
51 |
"Software, and to permit persons to whom the Software is furnished to do so,", |
52 |
"subject to the following conditions :", |
53 |
"", |
54 |
"The above copyright notice and this permission notice shall be included in all", |
55 |
"copies or substantial portions of the Software.", |
56 |
"", |
57 |
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR", |
58 |
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,", |
59 |
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE", |
60 |
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER", |
61 |
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,", |
62 |
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE", |
63 |
"SOFTWARE." |
64 |
}; |
65 |
|
66 |
size_t i; |
67 |
|
68 |
for (i=0; i<NELEM(ref); i++) |
69 |
{ |
70 |
printf("%s\n", ref[i]); |
71 |
} |
72 |
} |
73 |
|
74 |
const char *kickish_words[] = |
75 |
{ |
76 |
"Kick", |
77 |
"Smack", |
78 |
"Slug", |
79 |
"Punch", |
80 |
"Assault", |
81 |
"Hit", |
82 |
"Strike", |
83 |
"Slap", |
84 |
"Smack", |
85 |
"Spank", |
86 |
"Cuff", |
87 |
"Thump", |
88 |
"Swat", |
89 |
"Beat", |
90 |
"Thrash", |
91 |
"Batter", |
92 |
"Pound", |
93 |
"Pummel", |
94 |
"Box", |
95 |
"Whip", |
96 |
"Flog", |
97 |
"Cane", |
98 |
"Wallop", |
99 |
"Bash", |
100 |
"Bop", |
101 |
"Clout", |
102 |
"Clip", |
103 |
"Clobber", |
104 |
"Sock", |
105 |
"Swipe", |
106 |
"Crown", |
107 |
"Belt", |
108 |
"Tan", |
109 |
"Deck", |
110 |
"Floor", |
111 |
"Bump", |
112 |
"Knock", |
113 |
"Bang", |
114 |
"Tap", |
115 |
"Welt" |
116 |
}; |
117 |
|
118 |
volatile int ctrl_c_or_break_pressed = 0; //volatile because the Visual C++ environment spawns a thread to handle |
119 |
//CTRL-C or CTRL-BREAK. |
120 |
volatile int thread_ran = 0; //volatile because shared with thread. |
121 |
|
122 |
|
123 |
BOOL CtrlHandler(DWORD fdwCtrlType) |
124 |
{ |
125 |
switch (fdwCtrlType) |
126 |
{ |
127 |
case CTRL_C_EVENT: |
128 |
case CTRL_BREAK_EVENT: |
129 |
ctrl_c_or_break_pressed = 1; |
130 |
return(TRUE); |
131 |
//There is no default case. There is no break statement. I've assigned a |
132 |
//magic number to a variable. But the function still works fine. |
133 |
//Can you believe it? It must be a miracle! |
134 |
} |
135 |
|
136 |
return(FALSE); //Did I mention the multiple return points? This program should |
137 |
//blow up and wipe out several city blocks! |
138 |
} |
139 |
|
140 |
|
141 |
//Thread function. Parameter ignored. |
142 |
DWORD WINAPI MyThreadFunction(LPVOID lpParam) |
143 |
{ |
144 |
thread_ran = 1; |
145 |
|
146 |
return (0); |
147 |
} |
148 |
|
149 |
|
150 |
int c_main(int argc, char* argv[]) |
151 |
{ |
152 |
size_t selector = NELEM(kickish_words) - 1; |
153 |
size_t ui; |
154 |
int first_time = 1; |
155 |
HANDLE thread_handle; |
156 |
DWORD thread_id; |
157 |
|
158 |
emit_hline_std(); |
159 |
printf("kicker, Copyright 2016 David T. Ashley (dashley@gmail.com)\n"); |
160 |
emit_hline_std(); |
161 |
printf("This program is provided under the MIT License, reproduced below.\n"); |
162 |
emit_hline_std(); |
163 |
emit_mit_license(); |
164 |
emit_hline_std(); |
165 |
printf("This program accepts no command-line arguments and simply spawns a new thread\n"); |
166 |
printf("approximately every 1 second. The purpose of the program is to prevent\n"); |
167 |
printf("certain stalls that occur under Windows when running some programs/scripts\n"); |
168 |
printf("It is known that spawning a new thread periodically unstalls the programs/\n"); |
169 |
printf("scripts. The program can be terminated with CTRL-C or CTRL-BREAK.\n"); |
170 |
emit_hline_std(); |
171 |
|
172 |
SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE); |
173 |
//Yes, I know I'm supposed to check the return code. |
174 |
|
175 |
while (! ctrl_c_or_break_pressed) |
176 |
{ |
177 |
//Backspace over the previous word. |
178 |
if (! first_time) |
179 |
{ |
180 |
//Beep(750, 500); |
181 |
|
182 |
for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++) |
183 |
{ |
184 |
printf("%c", (char)8); |
185 |
} |
186 |
for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++) |
187 |
{ |
188 |
printf(" "); |
189 |
} |
190 |
for (ui = 0; ui < (strlen(kickish_words[selector]) + 1); ui++) |
191 |
{ |
192 |
printf("%c", (char)8); |
193 |
} |
194 |
} |
195 |
|
196 |
first_time = 0; |
197 |
|
198 |
//Go to next string. |
199 |
if (selector < (NELEM(kickish_words) - 1)) |
200 |
selector++; |
201 |
else |
202 |
selector = 0; |
203 |
|
204 |
//Print the description. |
205 |
printf("%s!", kickish_words[selector]); |
206 |
|
207 |
//Start the thread. This will hopefully unstall things. |
208 |
thread_handle = CreateThread( |
209 |
NULL, // default security attributes |
210 |
0, // use default stack size |
211 |
MyThreadFunction, // thread function name |
212 |
0, // argument to thread function |
213 |
0, // use default creation flags |
214 |
&thread_id); // returns the thread identifier |
215 |
|
216 |
//If the create call failed, terminate. |
217 |
if (thread_handle == NULL) |
218 |
{ |
219 |
printf("\nOuch!\n"); |
220 |
exit(0); |
221 |
} |
222 |
|
223 |
//Wait for thread termination. This should be nearly instant. But it |
224 |
//would seem to be the best way to wait so as not to chew up CPU |
225 |
//cycles. |
226 |
WaitForSingleObject(thread_handle, INFINITE); |
227 |
|
228 |
//At this point, the thread should have run, and the variable should have been |
229 |
//set. If not, there is a real logical problem. |
230 |
if (!thread_ran) |
231 |
{ |
232 |
printf("\nDouble-ouch\n"); |
233 |
exit(0); |
234 |
} |
235 |
|
236 |
thread_ran = 0; |
237 |
|
238 |
//Delay. Sleep() is likely the most effective way to not waste CPU capacity. |
239 |
Sleep(1000); |
240 |
} |
241 |
|
242 |
printf("\n"); |
243 |
emit_hline_std(); |
244 |
printf("Program terminated with CTRL-C or CTRL-BREAK.\n"); |
245 |
emit_hline_std(); |
246 |
|
247 |
return(0); |
248 |
} |
249 |
|
250 |
// End of c_main.c. |