信号
- 信号可工作在单个进程和多个进程中,用于处理异步事件。
- 主要包含两个 :alarm (用于产生闹钟信号)及signal (处理各类信号,包括但不限闹钟信号)
alarm:闹钟,参数是时间,成功返回0或剩余时间,错误返回-1
usigned int alarm(unsigned int seconds);
signal:参数signum为等待的信号,handler为触发处理方式,类似中断处理函数,成功返回0,错误返回-1
sighandler_t signal(int signum,sighandler_t handler);
等待信号的类型定义文件:其中包含有各类信号,包括闹钟信号SIGALRM、空闲信号SIGHUP、退出信号SIGQUIT等等,因此结合各类信号可实现不同功能的异步调用。
/* Signal number constants. Generic template.
Copyright (C) 1991-2020 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, see
<https://www.gnu.org/licenses/>. */
#ifndef _BITS_SIGNUM_GENERIC_H
#define _BITS_SIGNUM_GENERIC_H 1
#ifndef _SIGNAL_H
#error "Never include <bits/signum-generic.h> directly; use <signal.h> instead."
#endif
#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_XOPEN
# define SIG_HOLD ((__sighandler_t) 2) /* Add signal to hold mask. */
#endif
#define SIGINT 2 /* Interactive attention signal. */
#define SIGILL 4 /* Illegal instruction. */
#define SIGABRT 6 /* Abnormal termination. */
#define SIGFPE 8 /* Erroneous arithmetic operation. */
#define SIGSEGV 11 /* Invalid access to storage. */
#define SIGTERM 15 /* Termination request. */
#define SIGHUP 1 /* Hangup. */
#define SIGQUIT 3 /* Quit. */
#define SIGTRAP 5 /* Trace/breakpoint trap. */
#define SIGKILL 9 /* Killed. */
#define SIGBUS 10 /* Bus error. */
#define SIGSYS 12 /* Bad system call. */
#define SIGPIPE 13 /* Broken pipe. */
#define SIGALRM 14 /* Alarm clock. */
#define SIGURG 16 /* Urgent data is available at a socket. */
#define SIGSTOP 17 /* Stop, unblockable. */
#define SIGTSTP 18 /* Keyboard stop. */
#define SIGCONT 19 /* Continue. */
#define SIGCHLD 20 /* Child terminated or stopped. */
#define SIGTTIN 21 /* Background read from control terminal. */
#define SIGTTOU 22 /* Background write to control terminal. */
#define SIGPOLL 23 /* Pollable event occurred (System V). */
#define SIGXCPU 24 /* CPU time limit exceeded. */
#define SIGXFSZ 25 /* File size limit exceeded. */
#define SIGVTALRM 26 /* Virtual timer expired. */
#define SIGPROF 27 /* Profiling timer expired. */
#define SIGUSR1 30 /* User-defined signal 1. */
#define SIGUSR2 31 /* User-defined signal 2. */
#define SIGWINCH 28 /* Window size change (4.3 BSD, Sun). */
#define SIGIO SIGPOLL /* I/O now possible (4.2 BSD). */
#define SIGIOT SIGABRT /* IOT instruction, abort() on a PDP-11. */
#define SIGCLD SIGCHLD /* Old System V name */
#define __SIGRTMIN 32
#define __SIGRTMAX __SIGRTMIN
#define _NSIG (__SIGRTMAX + 1)
#endif /* bits/signum-generic.h. */
编写测试程序:
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
void sig_handler()
{
printf("hello handler\n");
}
int main (void)
{
signal(SIGALRM,sig_handler);
alarm(5);
for (int i = 0; i < 7; i++)
{
printf("sleep_....%d seconds\n",i);
sleep(1);
}
}
如果linux内核和开发板一样的话可以直接在ubuntu上测试:
root@ubuntu:/home/wly/topeet/code_for_linux_application# ./signal
sleep_....0 seconds
sleep_....1 seconds
sleep_....2 seconds
sleep_....3 seconds
sleep_....4 seconds
hello handler
sleep_....5 seconds
sleep_....6 seconds
root@ubuntu:/home/wly/topeet/code_for_linux_application#
bingo