Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.

Commit 0d2d73b

Browse files
committed
feat: 捕捉指定信号 signal() 运用
1 parent 09f6553 commit 0d2d73b

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

15_sys_usr_signal/signal

8.41 KB
Binary file not shown.

15_sys_usr_signal/signal.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/wait.h>
4+
#include <sys/time.h>
5+
#include <signal.h>
6+
7+
void func(int no); //用作回调函数,给 signal() 调用
8+
9+
int main(int argc, char *argv[])
10+
{
11+
signal(SIGINT, func); //设置信号捕捉函数, 捕捉 ctrl + c
12+
13+
while (true) {
14+
printf("Keep the thread running for the non-death state.\n");
15+
sleep(1);
16+
}
17+
18+
return 0;
19+
}
20+
21+
void func(int no)
22+
{
23+
printf("捕捉的信号为: %d\n", no);
24+
}

0 commit comments

Comments
 (0)