processes 2 信号

来源:互联网 发布:松江区网络交换机回收 编辑:程序博客网 时间:2024/05/16 17:09
//父子进程信号通信//signal 接收信号//kill 发送信号[root@localhost chapter11]# cat alarm.c /*  In alarm.c, the first function, ding, simulates an alarm clock.  */#include <signal.h>#include <stdio.h>#include <unistd.h>#include <stdlib.h>static int alarm_fired = 0;void ding(int sig){    alarm_fired = 1;}/*  In main, we tell the child process to wait for five seconds    before sending a SIGALRM signal to its parent.  */int main(){    pid_t pid;    printf("alarm application starting\n");    pid = fork();    switch(pid) {    case -1:      /* Failure */      perror("fork failed");      exit(1);    case 0:      /* child */        sleep(5);        kill(getppid(), SIGALRM);        exit(0);    }/*  The parent process arranges to catch SIGALRM with a call to signal    and then waits for the inevitable.  */    printf("waiting for alarm to go off \n");    (void) signal(SIGALRM, ding);    pause();    if (alarm_fired)        printf("Ding!\n");    printf("done\n");    exit(0);}[root@localhost chapter11]# ./alarm alarm application startingwaiting for alarm to go off Ding!done[root@localhost chapter11]# 
另一个接收信号的函数 sigaction
[root@localhost chapter11]# cat ctrlc2.c #include <signal.h>#include <stdio.h>#include <unistd.h>void ouch(int sig){    printf("OUCH! - I got signal %d\n", sig);}int main(){    struct sigaction act;    act.sa_handler = ouch;    sigemptyset(&act.sa_mask);    act.sa_flags = 0;    sigaction(SIGINT, &act, 0);  while(1) {    printf("Hello World!\n");    sleep(1);  }}[root@localhost chapter11]# ./ctrlc2Hello World!Hello World!^COUCH! - I got signal 2Hello World!^COUCH! - I got signal 2Hello World!^COUCH! - I got signal 2Hello World!^COUCH! - I got signal 2Hello World!^COUCH! - I got signal 2Hello World!^COUCH! - I got signal 2Hello World!Hello World!Hello World!Hello World!^COUCH! - I got signal 2Hello World!Hello World!Hello World!^\Quit (core dumped)[root@localhost chapter11]# 
常用信号:
[root@localhost chapter11]# cat  /usr/include/bits/signum.h /* Signal number definitions.  Linux version.   Copyright (C) 1995,1996,1997,1998,1999,2003 Free Software Foundation, Inc.   This file is part of the GNU C Library.   The GNU C Library is free software; you can redistribute it and/or   modify it under the terms of the GNU Lesser General Public   License as published by the Free Software Foundation; either   version 2.1 of the License, or (at your option) any later version.   The GNU C Library is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public   License along with the GNU C Library; if not, write to the Free   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   02111-1307 USA.  */#ifdef_SIGNAL_H/* Fake signal functions.  */#define SIG_ERR((__sighandler_t) -1)/* Error return.  */#define SIG_DFL((__sighandler_t) 0)/* Default action.  */#define SIG_IGN((__sighandler_t) 1)/* Ignore signal.  */#ifdef __USE_UNIX98# define SIG_HOLD((__sighandler_t) 2)/* Add signal to hold mask.  */#endif/* Signals.  */#defineSIGHUP1/* Hangup (POSIX).  */#defineSIGINT2/* Interrupt (ANSI).  */#defineSIGQUIT3/* Quit (POSIX).  */#defineSIGILL4/* Illegal instruction (ANSI).  */#defineSIGTRAP5/* Trace trap (POSIX).  */#defineSIGABRT6/* Abort (ANSI).  */#defineSIGIOT6/* IOT trap (4.2 BSD).  */#defineSIGBUS7/* BUS error (4.2 BSD).  */#defineSIGFPE8/* Floating-point exception (ANSI).  */#defineSIGKILL9/* Kill, unblockable (POSIX).  */#defineSIGUSR110/* User-defined signal 1 (POSIX).  */#defineSIGSEGV11/* Segmentation violation (ANSI).  */#defineSIGUSR212/* User-defined signal 2 (POSIX).  */#defineSIGPIPE13/* Broken pipe (POSIX).  */#defineSIGALRM14/* Alarm clock (POSIX).  */#defineSIGTERM15/* Termination (ANSI).  */#defineSIGSTKFLT16/* Stack fault.  */#defineSIGCLDSIGCHLD/* Same as SIGCHLD (System V).  */#defineSIGCHLD17/* Child status has changed (POSIX).  */#defineSIGCONT18/* Continue (POSIX).  */#defineSIGSTOP19/* Stop, unblockable (POSIX).  */#defineSIGTSTP20/* Keyboard stop (POSIX).  */#defineSIGTTIN21/* Background read from tty (POSIX).  */#defineSIGTTOU22/* Background write to tty (POSIX).  */#defineSIGURG23/* Urgent condition on socket (4.2 BSD).  */#defineSIGXCPU24/* CPU limit exceeded (4.2 BSD).  */#defineSIGXFSZ25/* File size limit exceeded (4.2 BSD).  */#defineSIGVTALRM26/* Virtual alarm clock (4.2 BSD).  */#defineSIGPROF27/* Profiling alarm clock (4.2 BSD).  */#defineSIGWINCH28/* Window size change (4.3 BSD, Sun).  */#defineSIGPOLLSIGIO/* Pollable event occurred (System V).  */#defineSIGIO29/* I/O now possible (4.2 BSD).  */#defineSIGPWR30/* Power failure restart (System V).  */#define SIGSYS31/* Bad system call.  */#define SIGUNUSED31#define_NSIG65/* Biggest signal number + 1   (including real-time signals).  */#define SIGRTMIN        (__libc_current_sigrtmin ())#define SIGRTMAX        (__libc_current_sigrtmax ())/* These are the hard limits of the kernel.  These values should not be   used directly at user level.  */#define __SIGRTMIN32#define __SIGRTMAX(_NSIG - 1)#endif/* <signal.h> included.  */[root@localhost chapter11]# 

信号值动作解释SIGHUP1终端线路挂断SIGINT2Term键盘输入的中断命令,从终端输入 Ctrl-C 时发生SIGQUIT3Core键盘输入的退出命令SIGILL4Core错误指令SIGABRT6Coreabort(3)发出的中止信号SIGFPE8Core浮点数异常SIGKILL9TermKILL信号SIGSEGV11Core非法内存访问SIGPIPE13Term管道断开SIGALRM14Termalarm(2)发出的中止信号SIGTERM15Term强制中止信号SIGUSR130,10,16Term用户自定义信号1SIGUSR231,12,17Term用户自定义信号2SIGCHLD20,17,18Ign子进程中止信号SIGCONT19,18,25Cont继续执行一个停止的进程SIGSTOP17,19,23Stop非终端来的停止信号SIGTSTP18,20,24Stop终端来的停止信号SIGTTIN21,21,26Stop后台进程读终端SIGTTOU22,22,27Stop后台进程写终端refer tohttp://tech.idv2.com/2006/09/28/useful-linux-signals/


原创粉丝点击