Advertisement
Guest User

Untitled

a guest
May 20th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. /*lsp signal()함수 예제2 304p~305p*/
  2. /*ssu_signal_2.c*/
  3. /*20162468 박다은*/
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <signal.h>
  9. #include <sys/time.h> //gettimeofday
  10.  
  11. static void ssu_signal_handler(int signo);
  12.  
  13. int main()
  14. {
  15.     struct timeval A, B, C; //시간을 비교할 값들
  16.     gettimeofday(&A, NULL);
  17.     //=======================================
  18.  
  19.     if(signal(SIGINT, ssu_signal_handler) == SIG_ERR){ //handler
  20.         fprintf(stderr, "cannot handle SIGINT\n");
  21.         exit(EXIT_FAILURE);
  22.     }
  23.     if(signal(SIGTERM, ssu_signal_handler) == SIG_ERR){ //handler
  24.         fprintf(stderr, "cannot handle SIGTERM\n");
  25.         exit(EXIT_FAILURE);
  26.     }
  27.     if(signal(SIGPROF, SIG_DFL) == SIG_ERR){ //default
  28.         fprintf(stderr, "cannot reset SIGPROF\n");
  29.         exit(EXIT_FAILURE);
  30.     }
  31.     if(signal(SIGHUP, SIG_IGN) == SIG_ERR){ //ignore
  32.         fprintf(stderr, "cannot ignore SIGHUP\n");
  33.         exit(EXIT_FAILURE);
  34.     }
  35.    
  36.     while(1)
  37.         pause();
  38.  
  39.     //=======================================
  40.     gettimeofday(&B, NULL);
  41.  
  42.     C.tv_sec = B.tv_sec - A.tv_sec;
  43.     C.tv_usec = B.tv_usec - A.tv_usec;
  44.     if(C.tv_usec < 0){
  45.         C.tv_sec -= 1;
  46.         C.tv_usec += 1000000;
  47.     }
  48.     printf("ROLLCAKE::Running Time : %ld µs\n", C.tv_sec*1000000 + C.tv_usec); //작은 프로그램이므로 마이크로초로 계산
  49.     exit(0);
  50. }
  51.  
  52. void ssu_signal_handler(int signo){
  53.     if(signo == SIGINT)
  54.         printf("caught SIGINT\n");
  55.     else if(signo == SIGTERM)
  56.         printf("caught SIGTERM\n");
  57.     else{
  58.         fprintf(stderr, "unexpected signal\n");
  59.         exit(EXIT_FAILURE);
  60.     }
  61.  
  62.     exit(EXIT_SUCCESS);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement