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