Skip to content

Instantly share code, notes, and snippets.

@leveryd
Created September 29, 2021 09:22
Show Gist options
  • Save leveryd/7497d25a9898b4009aedb11f21acf937 to your computer and use it in GitHub Desktop.
Save leveryd/7497d25a9898b4009aedb11f21acf937 to your computer and use it in GitHub Desktop.
"信号的使用"测试
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
long int count;
void sig_term_handler(int signum, siginfo_t *info, void *ptr)
{
int i;
printf("hi\n");
for(i=0;i<3;i++){
printf("count: %ld\n", count);
sleep(1);
}
}
void catch_sigterm() {
static struct sigaction _sigact;
memset(&_sigact, 0, sizeof(_sigact));
_sigact.sa_sigaction = sig_term_handler;
_sigact.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &_sigact, NULL);
sigaction(SIGPIPE, &_sigact, NULL);
}
int main(){
catch_sigterm();
int i;
for(i=0;;i++){
count++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment