1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <unistd.h> #define SIGTERM_MSG "SIGTERM received.\n" void sig_term_handler(int signum, siginfo_t *info, void *ptr) { write(STDERR_FILENO, SIGTERM_MSG, sizeof(SIGTERM_MSG)); } 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); }
int main(int argc, char *argv[]) { if ( argc != 2 ) { printf("ERROR ELEMENT COUNTS."); return 1; }
int *p; long n = atol(argv[1]) * 1024 * 1024;
printf("Allocate Memory Size: %ld MB.\n", atol(argv[1])); p = (int *)malloc(n); if (p != NULL) printf("SUCCESS."); else printf("FAILED."); memset(p, 0, n);
catch_sigterm();
free(p); sleep(3000);
return 0; }
|