Man 7 signal 查找有什么信号可以使应用程序暂停
Standard signals
Linux supports the standard signals listed below. Several signal numbers are
architecture-dependent, as indicated inthe"Value" column. (Where three values
are given, thefirstone is usually valid for alpha and sparc, themiddleonefor x86, arm, and most other architectures, andthelastonefor mips. (Values
for parisc are not shown; see the Linux kernel source for signal numbering on
that architecture.) A - denotes that a signal is absent onthecorresponding
architecture.)
First the signals described inthe original POSIX.1-1990 standard.
Signal Value Action Comment
──────────────────────────────────────────────────────────────────────
SIGHUP 1 Term Hangup detected oncontrollingterminalor death of controlling process
SIGINT 2 Term Interrupt from keyboard
SIGQUIT 3 Core Quit from keyboard
SIGILL 4 Core Illegal Instruction
SIGABRT 6 Core Abort signal from abort(3)
SIGFPE 8 Core Floating point exception
SIGKILL 9 Term Kill signal
SIGSEGV 11 Core Invalid memory reference
SIGPIPE 13 Term Broken pipe: writeto pipe with no
readers
SIGALRM 14 Term Timer signal from alarm(2)
SIGTERM 15 Term Termination signal
SIGUSR1 30,10,16 Term User-defined signal 1
SIGUSR2 31,12,17 Term User-defined signal 2
SIGCHLD 20,17,18 Ign Child stopped or terminated
SIGCONT 19,18,25 Cont Continue if stopped
SIGSTOP 17,19,23 Stop Stop process
SIGTSTP 18,20,24 Stop Stop typed at terminal
SIGTTIN 21,21,26 Stop Terminal input for background process
SIGTTOU 22,22,27 Stop Terminal output for background process
The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
Next the signals notinthe POSIX.1-1990 standard but described in SUSv2 and
POSIX.1-2001.
Signal Value Action Comment
────────────────────────────────────────────────────────────────────
SIGBUS 10,7,10 Core Bus error (bad memory access)
SIGPOLL Term Pollable event (Sys V).
Synonym for SIGIO
SIGPROF 27,27,29 Term Profiling timer expired
SIGSYS 12,31,12 Core Bad argument to routine (SVr4)
SIGTRAP 5 Core Trace/breakpoint trap
SIGURG 16,23,21 Ign Urgent condition onsocket (4.2BSD)
SIGVTALRM 26,26,28 Term Virtual alarm clock (4.2BSD)
SIGXCPU 24,24,30 Core CPU time limit exceeded (4.2BSD)
SIGXFSZ 25,25,31 Core File size limit exceeded (4.2BSD)
#include <sys/types.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <signal.h>#include <errno.h>#include <signal.h>#include <errno.h>#include <sys/wait.h>/*
wait()函数的使用
*/void TestFunc(int loopnum)
{
printf("loopnum:%d\n", loopnum);
}
int main(void )
{
int procnum = 10;
int loopnum = 100;
int i = 0, j = 0;
printf("please enter you procNum : \n");
scanf("%d", &procnum);
printf("please enter you loopnum :\n");
scanf("%d", &loopnum);
pid_t pid;
for (i=0; i<procnum; i++)
{
pid = fork();
if (pid == 0)
{
for (j=0; j<loopnum; j++)
{
TestFunc(j);
}
exit(0);//使子进程退出,非常重要
}
}
//方法1#if 0int ret;
//wait系统调用会使父进程暂停执行,直到它的一个子进程结束为止while(1){
//使wait()函数返回不一定是子进程结束造成的,也有可能是信号中断造成
ret = wait(NULL);//让父进程阻塞的过程中有可能被别的信号中断 ,传NULL的妙用if(ret == -1){
//判断wait()函数返回的真正原因是什么if(errno == EINTR){//EINTR错误(被中断的系统调用)。
perror("信号中断");
continue;
}
}
break;
}
#endif#if 1 //可以封装成信号处理函数//避免产生僵尸进程
pid_t mypid = 0;
int stat;
while ((mypid = waitpid(-1, &stat, WNOHANG)) > 0)
{
printf("退出子进程pid %d terminated\n", mypid);
//;
}
#endifprintf("父进程退出\n");
return0;
}