五十一. kill函数的使用
这里我们写一个kill.c
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
pid_t pid = fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}
if(pid > 0)
{
while(1)
{
printf("parent process pid = %d\n", getpid());
sleep(1);
}
}
else if(pid == 0)
{
sleep(2);
// 弑父
kill(getppid(), SIGKILL);
}
return 0;
}
运行一下
[root@VM_0_15_centos 7Day]# ls
alarm.c homework raise.c sigaction.c signal.c
alarm_uncle.c kill.c setitimer.c sigmaks.c thread_attr.c
[root@VM_0_15_centos 7Day]# gcc kill.c
[root@VM_0_15_centos 7Day]# ./a.out
parent process pid = 30669
parent process pid = 30669
parent process pid = 30669
Killed
五十二. kill函数pid参数介绍
五十三. raise和abort函数
写一个raise.c
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
pid_t pid = fork();
if(pid > 0)
{
// parent
int s;
pid_t wpid = wait(&s);
printf("child died pid = %d\n",wpid);
if(WIFSIGNALED(s))
{
printf("term by signal: %d\n", WTERMSIG(s));
}
}
else if(pid == 0)
{
raise(SIGINT);
// abort();
}
return 0;
}
运行一下:
[root@VM_0_15_centos 7Day]# vim raise.c
[root@VM_0_15_centos 7Day]# ls
alarm.c a.out kill.c setitimer.c sigmaks.c thread_attr.c
alarm_uncle.c homework raise.c sigaction.c signal.c
[root@VM_0_15_centos 7Day]# gcc raise.c
[root@VM_0_15_centos 7Day]# ./a.out
child died pid = 12026
term by signal: 2
写一个abort.c
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
pid_t pid = fork();
if(pid > 0)
{
// parent
int s;
pid_t wpid = wait(&s);
printf("child died pid = %d\n",wpid);
if(WIFSIGNALED(s))
{
printf("term by signal: %d\n", WTERMSIG(s));
}
}
else if(pid == 0)
{
// raise(SIGINT);
abort();
}
return 0;
}
运行一下
[root@VM_0_15_centos 7Day]# ls
abort.c alarm_uncle.c kill.c setitimer.c sigmaks.c thread_attr.c
alarm.c homework raise.c sigaction.c signal.c
[root@VM_0_15_centos 7Day]# gcc abort.c
[root@VM_0_15_centos 7Day]# ./a.out
child died pid = 12434
term by signal: 6
五十四. alarm函数
alarm的文档
写一个alarm.c
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
int ret = alarm(5);
printf("ret = %d\n", ret);
sleep(2);
ret = alarm(2);
printf("ret = %d\n", ret);
while(1)
{
printf("hello\n");
sleep(1);
}
return 0;
}
运行结果
[root@VM_0_15_centos 7Day]# gcc alarm.c
[root@VM_0_15_centos 7Day]# ./a.out
ret = 0
ret = 3
hello
hello
Alarm clock
五十五. 计算1s数多少个数