Linux 에서 의 SIGUSR 1 과 SIGUSR 2 에 대한 이해

3868 단어 linux
인터넷 에서 이 글 을 배 웠 습 니 다.
http://liyong-zone.blog.sohu.com/102060659.html
사 의 를 표 하 다.
컴 파일 할 때 사용:
g++ -o testsig.o testsig.cpp
실행 해 보 았 습 니 다:
[root@localhost test]# cat testsig.cpp

#include <iostream>

#include <signal.h>

using namespace std;



void CatchSigUsr1(int sig)

{

    cout<<"SIGUSR1 Caught"<<endl;

}



void CatchSigUsr2(int sig)

{

    cout<<"SIGUSR2 Caught"<<endl;

    exit(0);

}



int main()

{

    signal(SIGUSR1, CatchSigUsr1);

    signal(SIGUSR2, CatchSigUsr2);

    while(1)

    {

        sleep(1);

    }

}

[root@localhost test]# 

그 러 니까 os 급 에서 kill - s SIGUSR 1 을 보 내 거나... kill - s SIGUSR 2, 프로그램 활성화 가능 (CatchSigUsr 1, CatchSigUsr 2)
이렇게 고 칠 수도 있다.
[root@localhost test]# cat testsig.cpp

#include <iostream>

#include <signal.h>

using namespace std;



void CatchSigUsr1(int sig)

{

    cout<<"SIGUSR1 Caught"<<endl;

}



void CatchSigUsr2(int sig)

{

    cout<<"SIGUSR2 Caught"<<endl;

    exit(0);

}



void sigHandler(int signum)

{

   if (signum==SIGUSR1)

        CatchSigUsr1(signum);



   if (signum==SIGUSR2)

        CatchSigUsr2(signum);

}



int main()

{

    signal(SIGUSR1, sigHandler);

    signal(SIGUSR2, sigHandler);

    while(1)

    {

        sleep(1);

    }

}

[root@localhost test]# 

좋은 웹페이지 즐겨찾기