mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 08:06:32 +00:00
1
user/apps/test_sigint/.gitignore
vendored
Normal file
1
user/apps/test_sigint/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
test_sigint
|
20
user/apps/test_sigint/Makefile
Normal file
20
user/apps/test_sigint/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
ifeq ($(ARCH), x86_64)
|
||||
CROSS_COMPILE=x86_64-linux-musl-
|
||||
else ifeq ($(ARCH), riscv64)
|
||||
CROSS_COMPILE=riscv64-linux-musl-
|
||||
endif
|
||||
|
||||
CC=$(CROSS_COMPILE)gcc
|
||||
|
||||
.PHONY: all
|
||||
all: main.c
|
||||
$(CC) -static -o test_sigint main.c
|
||||
|
||||
.PHONY: install clean
|
||||
install: all
|
||||
mv test_sigint $(DADK_CURRENT_BUILD_DIR)/test_sigint
|
||||
|
||||
clean:
|
||||
rm test_sigint *.o
|
||||
|
||||
fmt:
|
29
user/apps/test_sigint/main.c
Normal file
29
user/apps/test_sigint/main.c
Normal file
@ -0,0 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 信号处理函数
|
||||
void handle_signal(int signal)
|
||||
{
|
||||
if (signal == SIGINT)
|
||||
{
|
||||
printf("Caught SIGINT (Ctrl+C). Exiting gracefully...\n");
|
||||
exit(0); // 终止程序
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 注册信号处理函数
|
||||
signal(SIGINT, handle_signal);
|
||||
|
||||
// 模拟一个长时间运行的进程
|
||||
while (1)
|
||||
{
|
||||
printf("Running... Press Ctrl+C to stop.\n");
|
||||
sleep(5);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user