UNIX编程(10)-信号

1.signal函数

 

#include <signal.h>

void (*signal(int signo, void (*func)(int)))(int);

Returns: previous disposition of signal (see following) if OK, SIG_ERR on error


一般可将signal函数的原型写成

Sigfunc *signal(int, Sigfunc *);

例:捕捉 SIGUSR1和SIGUSR2的简单程序

#include "apue.h"

static void sig_usr(int);   /* one handler for both signals */

int
main(void)
{
    if (signal(SIGUSR1, sig_usr) == SIG_ERR)
        err_sys("can't catch SIGUSR1");
    if (signal(SIGUSR2, sig_usr) == SIG_ERR)
        err_sys("can't catch SIGUSR2");
    for ( ; ; )
        pause();
}

static void
sig_usr(int signo)      /* argument is signal number */
{
    if (signo == SIGUSR1)
        printf("received SIGUSR1\n");
    else if (signo == SIGUSR2)
        printf("received SIGUSR2\n");
    else
        err_dump("received signal %d\n", signo);
}


2.中断的系统调用

3.可重入函数

4.SIGCLD语义

5.kill和raise函数

#include <signal.h>

int kill(pid_t pid, int signo);

int raise(int signo);

Both return: 0 if OK, 1 on error


调用raise(signo)等价于调用kill(getpid(), signo)

kill的pid参数有四种不同的情况

6.alarm和pause函数

 

#include <unistd.h>

unsigned int alarm(unsigned int seconds);

Returns: 0 or number of seconds until previously set alarm

参数seconds的值是秒数,经过了指定的seconds秒后会产生信号SIGALRM

 

#include <unistd.h>

int pause(void);

Returns: 1 with errno set to EINTR


pause函数使调用进程挂起直至捕捉到一个信号

7.信号集

 

#include <signal.h>

int sigemptyset(sigset_t *set);

int sigfillset(sigset_t *set);

int sigaddset(sigset_t *set, int signo);

int sigdelset(sigset_t *set, int signo);

All four return: 0 if OK, 1 on error

 

int sigismember(const sigset_t *set, int signo);

Returns: 1 if true, 0 if false, 1 on error


8.sigprocmask函数

 

#include <signal.h>

int sigprocmask(int how, const sigset_t *restrict set,
                sigset_t *restrict oset);

Returns: 0 if OK, 1 on error


当oset非空,那么进程的当前信号屏蔽字通过oset返回

当set非空,参数how指示如何修改当前信号屏蔽字

how

Description

SIG_BLOCK

The new signal mask for the process is the union of its current signal mask and the signal set pointed to by set. That is, set contains the additional signals that we want to block.

SIG_UNBLOCK

The new signal mask for the process is the intersection of its current signal mask and the complement of the signal set pointed to by set. That is, set contains the signals that we want to unblock.

SIG_SETMASK

The new signal mask for the process is replaced by the value of the signal set pointed to by set.

例:为进程打印信号屏蔽字

#include "apue.h"
#include <errno.h>

void
pr_mask(const char *str)
{
    sigset_t    sigset;
    int         errno_save;

    errno_save = errno;     /* we can be called by signal handlers */
    if (sigprocmask(0, NULL, &sigset) < 0)
        err_sys("sigprocmask error");

    printf("%s", str);
    if (sigismember(&sigset, SIGINT))   printf("SIGINT ");
    if (sigismember(&sigset, SIGQUIT))  printf("SIGQUIT ");
    if (sigismember(&sigset, SIGUSR1))  printf("SIGUSR1 ");
    if (sigismember(&sigset, SIGALRM))  printf("SIGALRM ");

    /* remaining signals can go here */

    printf("\n");
    errno = errno_save;
}


9.sigpending函数

 

#include <signal.h>

int sigpending(sigset_t *set);

Returns: 0 if OK, 1 on error


例:信号设置和sigprocmask实例

#include "apue.h"

static void sig_quit(int);

int
main(void)
{
    sigset_t    newmask, oldmask, pendmask;

    if (signal(SIGQUIT, sig_quit) == SIG_ERR)
        err_sys("can't catch SIGQUIT");

    /*
     * Block SIGQUIT and save current signal mask.
     */
    sigemptyset(&newmask);
    sigaddset(&newmask, SIGQUIT);
    if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
        err_sys("SIG_BLOCK error");

    sleep(5);   /* SIGQUIT here will remain pending */
    if (sigpending(&pendmask) < 0)
        err_sys("sigpending error");
    if (sigismember(&pendmask, SIGQUIT))
        printf("\nSIGQUIT pending\n");

    /*
     * Reset signal mask which unblocks SIGQUIT.
     */
    if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
        err_sys("SIG_SETMASK error");
    printf("SIGQUIT unblocked\n");

    sleep(5);   /* SIGQUIT here will terminate with core file */
    exit(0);
}

static void
sig_quit(int signo)
{
    printf("caught SIGQUIT\n");
    if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
        err_sys("can't reset SIGQUIT");
}


10.sigaction函数

#include <signal.h>

int sigaction(int signo, const struct sigaction
 *restrict act,
              struct sigaction *restrict oact);

Returns: 0 if OK, 1 on error

struct sigaction {
       void      (*sa_handler)(int);   /* addr of signal handler, */
                                       /* or SIG_IGN, or SIG_DFL */
       sigset_t sa_mask;               /* additional signals to block */
       int      sa_flags;              /* signal options, Figure 10.16 */

       /* alternate handler */
       void     (*sa_sigaction)(int, siginfo_t *, void *);
    };
11.sigsetjmp和siglongjmp函数

 

#include <setjmp.h>

int sigsetjmp(sigjmp_buf env, int savemask);

Returns: 0 if called directly, nonzero if returning from a call to siglongjmp

void siglongjmp(sigjmp_buf env, int val);

12.sigsuspend函数

 

#include <signal.h>

int sigsuspend(const sigset_t *sigmask);

Returns: 1 with errno set to EINTR

 13.abort函数

 

#include <stdlib.h>

void abort(void);

This function never returns


14.system函数

15.sleep函数

 

#include <unistd.h>

unsigned int sleep(unsigned int seconds);

Returns: 0 or number of unslept seconds

16.其他函数

 

#include <signal.h>

void psignal(int signo, const char *msg);


 

#include <string.h>

char *strsignal(int signo);

Returns: a pointer to a string describing the signal


 

#include <signal.h>

int sig2str(int signo, char *str);

int str2sig(const char *str, int *signop);

Both return: 0 if OK, 1 on error

猜你喜欢

转载自brxonline.iteye.com/blog/1129170