Browse code

added waiting for children processes

Jiri Kuthan authored on 11/01/2002 22:20:13
Showing 1 changed files
... ...
@@ -17,6 +17,7 @@
17 17
 #include <sys/mman.h>
18 18
 #include <sys/fcntl.h>
19 19
 #include <sys/time.h>
20
+#include <sys/wait.h>
20 21
 
21 22
 #include "config.h"
22 23
 #include "dprint.h"
... ...
@@ -331,32 +332,30 @@ int main_loop()
331 332
 
332 333
 static void sig_usr(int signo)
333 334
 {
334
-	DPrint("INT received, program terminates\n");
335
+	pid_t	chld;
336
+	int	chld_status;
337
+
335 338
 	if (signo==SIGINT || signo==SIGPIPE) {	/* exit gracefuly */
336
-#ifdef STATS
339
+		DPrint("INT received, program terminates\n");
340
+#		ifdef STATS
337 341
 		/* print statistics on exit only for the first process */
338
-
339 342
 		if (stats->process_index==0 && stat_file )
340 343
 			if (dump_all_statistic()==0)
341 344
 				printf("statistic dumped to %s\n", stat_file );
342 345
 			else
343 346
 				printf("statistics dump to %s failed\n", stat_file );
344
-#endif
347
+#		endif
345 348
 		/* WARNING: very dangerous, might be unsafe*/
346 349
 		if (is_main)
347 350
 			destroy_modules();
348 351
 #ifdef PKG_MALLOC
349 352
 		LOG(L_INFO, "Memory status (pkg):\n");
350 353
 		pkg_status();
351
-#endif
354
+#		endif
352 355
 #ifdef SHM_MEM
353 356
 		if (is_main){
354 357
 			LOG(L_INFO, "Memory status (shm):\n");
355 358
 			shm_status();
356
-		}
357
-#endif
358
-#ifdef SHM_MEM
359
-		if (is_main){
360 359
 			/*zero all shmem  alloc vars, that will still use*/
361 360
 			pids=0;
362 361
 			shm_mem_destroy();
... ...
@@ -376,6 +375,22 @@ static void sig_usr(int signo)
376 375
 		LOG(L_INFO, "Memory status (shm):\n");
377 376
 		shm_status();
378 377
 #endif
378
+	} else if (signo==SIGCHLD) {
379
+		while ((chld=waitpid( -1, &chld_status, WNOHANG ))>0) {
380
+			if (WIFEXITED(chld_status)) 
381
+				LOG(L_INFO, "child process %d exited normally, status=%d\n",
382
+					chld, WEXITSTATUS(chld_status));
383
+			else if (WIFSIGNALED(chld_status)) {
384
+				LOG(L_INFO, "child process %d exited by a signal %d\n",
385
+					chld, WTERMSIG(chld_status));
386
+#				ifdef WCOREDUMP
387
+				LOG(L_INFO, "core was %sgenerated\n", WCOREDUMP(chld_status) ?
388
+					"" : "not" );
389
+#				endif
390
+			} else if (WIFSTOPPED(chld_status)) 
391
+				LOG(L_INFO, "child process %d stopped by a signal %d\n",
392
+					chld, WSTOPSIG(chld_status));
393
+		}
379 394
 	}
380 395
 }
381 396
 
... ...
@@ -407,6 +422,10 @@ int main(int argc, char** argv)
407 422
 		DPrint("ERROR: no SIGUSR1 signal handler can be installed\n");
408 423
 		goto error;
409 424
 	}
425
+	if (signal(SIGCHLD , sig_usr)  == SIG_ERR ) {
426
+		DPrint("ERROR: no SIGCHLD signal handler can be installed\n");
427
+		goto error;
428
+	}
410 429
 
411 430
 	//memtest();
412 431
 	//hashtest();