signal函数
主要用来设置某种信号的处理函数!!
头文件
#include<signal.h>
函数原型
typedef void (*sighandler_t)(int);
sighandler_t signal(int signum, sighandler_t handler);
参数
-
signum:要设置的信号类型
-
handler:
- SIG_IGN:忽略
- SIG_DFL:默认
- void ( *sighandler_t )( int ):自定义函数指针
返回值
- 成功:上一次设置的handler
- 失败:SIG_ERR
测试代码:
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
//信号处理函数
void signal_handler(int sig)
{
printf("\nThis signal number is %d \n",sig);
if(SIGINT == sig)
{
printf("Have got SIGINT!\n\n");
printf("The signal has been restored to the default processing mode!\n\n");
//恢复信号处理方式为默认处理方式
signal(SIGINT,SIG_DFL);
}
}
int main()
{
printf("\nThis is an alarm test function.\n\n");
signal(SIGINT,signal_handler);
while(1)
{
printf("Waiting for the SIGINT signal,plz enter \" ctrl + c \" ...\n");
sleep(1);
}
exit(0);
}
编译与测试
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$ gcc ctrl_c.c
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$ ./a.out
This is an alarm test function.
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
^C <-----------------------------注意,这里第一次 ctrl + c
This signal number is 2
Have got SIGINT!
The signal has been restored to the default processing mode!
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
Waiting for the SIGINT signal,plz enter " ctrl + c " ...
^C <-----------------------------注意,这里第二次 ctrl + c
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$
kill函数
主要功能是发送信号!!!
主要是给其他进程发送信号, 而 raise() 给自己发送信号
头文件
#include<sys/types.h>
#include<signal.h>
函数原型
int kill(pid_t pid,int sig);
参数
- pid:进程id
- sig:要发送的信号
返回值
- 成功:0
- 失败:-1
raise函数
头文件
#include<signal.h>
函数原型
int raise(int sig);
参数
- sig:发送信号
返回值
- 成功:0
- 失败:非0
二合一测试程序
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
pid_t pid;
int ret;
//创建一个子进程
if((pid = fork()) < 0)
{
printf("Fork error.\n");
exit(1);
}
if(0 == pid)
{
//子进程中使用raise()函数发出 SIGSTOP 信号,是子进程暂停
printf("Child is waiting for SIGSTOP signal!\n\n");
//子进程停在这里
raise(SIGSTOP);
//子进程跑不到的地方
printf("Child won't run here.\n");
exit(0);
}
else
{
//父进程 domain
//父进程休眠 3 秒,让子进程先执行
sleep(3);
//发送 SIGKILL 信号,杀掉子进程
if((ret = kill(pid,SIGKILL)) == 0)
{
printf("Parent kill %d!\n\n",pid);
}
//一直阻塞到子进程退出
wait(NULL);
printf("Parent exit!\n");
exit(0);
}
}
编译与测试
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$ gcc kill_raise.c
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$ ./a.out
Child is waiting for SIGSTOP signal!
Parent kill 3185!
Parent exit!
jl@jl-virtual-machine:~/test$
jl@jl-virtual-machine:~/test$
wait函数
#include <sys/types.h> /* 提供类型pid_t的定义 */
#include <sys/wait.h>
pid_t wait(int *status);
进程一旦调用了wait,就立即阻塞自己,由wait自动分析是否当前进程的某个子进程已经退出,如果让它找到了这样一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到有一个出现为止。
参数status用来保存被收集进程退出时的一些状态,它是一个指向int类型的指针。但如果我们对这个子进程是如何死掉的毫不在意,只想把这个僵尸进程消灭掉,(事实上绝大多数情况下,我们都会这样想),我们就可以设定这个参数为NULL
如:pid = wait(NULL);
如果成功,wait会返回被收集的子进程的进程ID,如果调用进程没有子进程,调用就会失败,此时wait返回-1,同时errno被置为ECHILD。