升高 ()- 发送信号

格式

#include <signal.h>
int raise(int sig);

语言级别

ANSI

线程安全

描述

raise() 函数将信号 sig 发送到正在运行的程序。 如果在编译命令上使用 SYSIFCOPT (*ASYNCSIGNAL) 进行编译,那么此函数将使用异步信号。 此函数的异步版本向进程或线程抛出信号。

返回值

raise() 函数返回 0 (如果成功) ,非零 (如果失败)。

示例

此示例为信号 SIGUSR1建立名为 sig_hand 的信号处理程序。 每当发出 SIGUSR1 信号时都会调用信号处理程序,并将忽略信号的前九次出现。 在第 10 个上升信号上,它退出程序,错误代码为 10。 请注意,每次调用信号处理程序时都必须重新建立该信号处理程序。
#include <signal.h>
#include <stdio.h>
 
void sig_hand(int);  /* declaration of sig_hand() as a function */
 
int main(void)
{
   signal(SIGUSR1, sig_hand); /* set up handler for SIGUSR1 */
 
   raise(SIGUSR1);   /* signal SIGUSR1 is raised */
                     /* sig_hand() is called     */
}
 
void sig_hand(int sig)
{
   static int count = 0;  /* initialized only once */
 
   count++;
   if (count == 10)  /* ignore the first 9 occurrences of this signal */
      exit(10);
   else
      signal(SIGUSR1, sig_hand);  /* set up the handler again */
}
/* This is a program fragment and not a complete program */

相关信息