Skip to content

Instantly share code, notes, and snippets.

@shun159
Created September 8, 2021 07:11
Show Gist options
  • Save shun159/3e20a8c0badcb97fbc2488f67083da4b to your computer and use it in GitHub Desktop.
Save shun159/3e20a8c0badcb97fbc2488f67083da4b to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#define NUMSIGS 64
#define MULTIPLE_OF_(x, i) (i % x == 0)
void (sig_handler) (int);
int sig_count[NUMSIGS + 1];
volatile static int line;
volatile int signumbuf[NUMSIGS * 100], sigcountbuf[NUMSIGS * 100];
int main(int argc, char* argv[]) {
sigset_t sigmask_new, sigmask_old;
struct sigaction sigact, oldact;
int signum, rc, i;
rc = sigfillset(&sigmask_new);
rc = sigprocmask(SIG_SETMASK, &sigmask_new, &sigmask_old);
memset(&sigact, 0, sizeof(struct sigaction));
sigact.sa_handler = sig_handler;
sigact.sa_flags = 0;
sigact.sa_mask = sigmask_new;
printf("\nInstalling signal handler and Raising signal for signal number: \n\n");
for (signum = 1; signum <= NUMSIGS; signum++) {
if (signum == SIGKILL || signum == SIGSTOP || signum == 32 || signum == 33) {
printf(" --");
} else {
sigaction(signum, &sigact, &oldact);
rc = raise(signum);
rc = raise(signum);
rc = raise(signum);
if (rc) {
printf("Failed on Signal %d\n", signum);
} else {
printf("%4d", signum);
}
}
if (MULTIPLE_OF_(16, signum))
printf("\n");
}
fflush(stdout);
rc = sigprocmask(SIG_SETMASK, &sigmask_old, NULL);
printf("\nSignal Number(Times Processed)\n");
printf("---------------------------------------------------\n");
for (i = 1; i <= NUMSIGS; i++) {
printf("%4d: %3d ", i, sig_count[i]);
if (MULTIPLE_OF_(8, i))
printf("\n");
}
printf("\n");
printf("\nHistory: Signal Number(Count Processed)\n");
printf("---------------------------------------------------\n");
for (i = 0; i < line; i++) {
if (MULTIPLE_OF_(8, i))
printf("\n");
printf("%4d(%1d)", signumbuf[i], sigcountbuf[i]);
}
printf("\n");
exit(EXIT_SUCCESS);
}
void sig_handler(int sig) {
sig_count[sig]++;
signumbuf[line] = sig;
sigcountbuf[line] = sig_count[sig];
line++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment