在unix的多进程编程中,僵尸进程的产生往往都是父进程没有处理SIGCHLD信号而引起的,我这里特别用ACE写了一个产生僵尸进程及其如何处理的用例。可以作为参考:程序循环启动一个时钟。 其中的信号采用ACE最简单的处理方法,[把黑体字部分去除,你就会看到僵尸进程噢!:)] #include "ace/OS_NS_unistd.h" #include "ace/Log_Msg.h" #include "ace/Process_Manager.h" #include "ace/Signal.h" pid_t RunThreeProgram(pid_t pid_cur); /* 下面用来处理SIGCHLD信号 #if defined (ACE_HAS_SIG_C_FUNC) extern "C" { #endif static void my_sighandler (int signo) { ACE_TRACE (ACE_TEXT ("::my_sighandler")); if (signo == SIGCHLD) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Signal SIGCHLD\n"))); } #if defined (ACE_HAS_SIG_C_FUNC) } #endif int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { ACE_OS::signal (SIGCHLD, (ACE_SignalHandler) my_sighandler);//指定SIGCHLD信号的处理函数 pid_t pids = ACE_INVALID_PID; for(int i=0;i<30;i++) { pids = RunThreeProgram(pids); ACE_DEBUG((LM_INFO,ACE_TEXT("Threads:[%d]\n"),pids)); ACE_OS::sleep(5); } if(getchar()) { ACE_DEBUG((LM_INFO,ACE_TEXT("Over!\n)); } return 0; } pid_t RunThreeProgram(pid_t pid_cur) { // Get the processwide process manager. ACE_Process_Manager* pm = ACE_Process_Manager::instance (); pm->terminate(pid_cur); //终止时会产生一个SIGCHLD信号 pm->wait(pidsss,0); ACE_Process_Options options; options.command_line (ACE_TEXT ("%s"), "/usr/bin/X11/xclock&"); // Spawn one child processes. pid_t pids = pm->spawn(options); return pids; } 【附加】unix下标准C的信号捕获和id打印函数 void catch_signal(int signal_no) { switch(signal_no) { case SIGHUP: printf("SIGHUP = %d\n",signal_no); break; case SIGINT: printf("SIGINT = %d\n",signal_no); exit(0); break; case SIGQUIT: printf("SIGQUIT = %d\n",signal_no); break; case SIGILL: printf("SIGILL = %d\n",signal_no); break; case SIGTRAP: printf("SIGTRAP = %d\n",signal_no); break; case SIGABRT: printf("SIGABRT = %d\n",signal_no); break; case SIGEMT: printf("SIGEMT = %d\n",signal_no); break; case SIGFPE: printf("SIGFPE = %d\n",signal_no); break; case SIGBUS: printf("SIGBUS = %d\n",signal_no); break; case SIGSYS: printf("SIGSYS = %d\n",signal_no); break; case SIGPIPE: printf("SIGPIPE = %d\n",signal_no); break; case SIGSEGV: printf("SIGSEGV = %d\n",signal_no); exit(-1); case SIGALRM: printf("SIGALRM = %d\n",signal_no); break; case SIGTERM: printf("SIGTERM = %d\n",signal_no); break; case SIGURG: printf("SIGURG = %d\n",signal_no); break; case SIGTSTP: printf("SIGTSTP = %d\n",signal_no); break; case SIGCONT: printf("SIGCONT = %d\n",signal_no); break; case SIGCHLD: printf("SIGCHLD = %d\n",signal_no); break; case SIGTTIN: printf("SIGTTIN = %d\n",signal_no); break; case SIGTTOU: printf("SIGTTOU = %d\n",signal_no); break; case SIGPOLL: printf("SIGPOLL = %d\n",signal_no); break; case SIGXCPU: printf("SIGXCPU = %d\n",signal_no); break; case SIGXFSZ: printf("SIGXFSZ = %d\n",signal_no); break; case SIGVTALRM: printf("SIGVTALRM = %d\n",signal_no); break; case SIGPROF: printf("SIGPROF = %d\n",signal_no); break; case SIGWINCH: printf("SIGWINCH = %d\n",signal_no); break; case SIGINFO: printf("SIGINFO = %d\n",signal_no); break; } return; } 在main()中增加: signal(SIGHUP,catch_signal); signal(SIGINT,catch_signal); signal(SIGQUIT,catch_signal); signal(SIGILL,catch_signal); signal(SIGTRAP,catch_signal); signal(SIGABRT,catch_signal); signal(SIGFPE,catch_signal); signal(SIGSYS,catch_signal); signal(SIGPIPE,catch_signal); signal(SIGCHLD,catch_signal); 。。。。。。。。。。
| | |