Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*lsp signal()함수 예제2 304p~305p*/
- /*ssu_signal_2.c*/
- /*20162468 박다은*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <signal.h>
- #include <sys/time.h> //gettimeofday
- static void ssu_signal_handler(int signo);
- int main()
- {
- struct timeval A, B, C; //시간을 비교할 값들
- gettimeofday(&A, NULL);
- //=======================================
- if(signal(SIGINT, ssu_signal_handler) == SIG_ERR){ //handler
- fprintf(stderr, "cannot handle SIGINT\n");
- exit(EXIT_FAILURE);
- }
- if(signal(SIGTERM, ssu_signal_handler) == SIG_ERR){ //handler
- fprintf(stderr, "cannot handle SIGTERM\n");
- exit(EXIT_FAILURE);
- }
- if(signal(SIGPROF, SIG_DFL) == SIG_ERR){ //default
- fprintf(stderr, "cannot reset SIGPROF\n");
- exit(EXIT_FAILURE);
- }
- if(signal(SIGHUP, SIG_IGN) == SIG_ERR){ //ignore
- fprintf(stderr, "cannot ignore SIGHUP\n");
- exit(EXIT_FAILURE);
- }
- while(1)
- pause();
- //=======================================
- gettimeofday(&B, NULL);
- C.tv_sec = B.tv_sec - A.tv_sec;
- C.tv_usec = B.tv_usec - A.tv_usec;
- if(C.tv_usec < 0){
- C.tv_sec -= 1;
- C.tv_usec += 1000000;
- }
- printf("ROLLCAKE::Running Time : %ld µs\n", C.tv_sec*1000000 + C.tv_usec); //작은 프로그램이므로 마이크로초로 계산
- exit(0);
- }
- void ssu_signal_handler(int signo){
- if(signo == SIGINT)
- printf("caught SIGINT\n");
- else if(signo == SIGTERM)
- printf("caught SIGTERM\n");
- else{
- fprintf(stderr, "unexpected signal\n");
- exit(EXIT_FAILURE);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement