
信号练习
开启 ubuntu ,新增一个档案 external.c,贴上以下程式码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
void handler(int sig) {
printf("You think hitting ctrl-c will stop the bomb?\n");
sleep(2);
printf("Well...");
fflush(stdout);
sleep(1);
printf("OK\n");
exit(0);
}
int main(){
signal(SIGINT, handler);
while(1){
}
}编译这个程式并且执行他
gcc -o external external.c
./external
炸弹蹦蹦蹦 (可莉炸鱼)
新增一个档案 internal.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int beeps = 0;
void handler(int sig) {
printf("BEEP\n");
fflush(stdout);
if (++beeps < 5)
alarm(1);
else {
printf("BOOM!\n");
exit(0);
}
}
int main() {
signal(SIGALRM, handler);
alarm(1);
while(1) {
}
}编译这个程式并且执行他
gcc -o internal internal.c
./internal
Latest posts by SHXJ (see all)
- 受保护的内容: NAS 版 Mathbot 管理网站与 Linebot 启动方法 - 2024 年 11 月 15 日
- Realtime 啥鬼的 - 2021 年 6 月 15 日
- nodejs 数学游戏 - 2021 年 6 月 8 日


