linux信号量及计时器

下面是一个关于linux信号量和计时器的例子。

 

  1 #include <signal.h>
  2 #include <sys/time.h>
  3 #include <sys/errno.h>
  4 #include <cstring>
  5 #include <iostream>
  6 using namespace std;
  7 
  8 void handle_sig(int dunno)
  9 {
 10     cout << "get a signal: ";
 11     unsigned uRet = 0;
 12     switch(dunno)
 13     {
 14         case SIGHUP:
 15             cout << "SIGHUP" << endl;
 16             break;
 17         case SIGINT:
 18             cout << "SIGINT" << endl;
 19             break;
 20         case SIGQUIT:
 21             cout << "SIGQUIT" << endl;
 22             break;
 23         case SIGALRM:
 24             //uRet = alarm(2);
 25             //cout << "alarm ret " << uRet << endl;
 26             cout << "SIGALRM" << endl;
 27             break;
 28         default:
 29             cout << dunno << endl;
 30             break;
 31     }
 32 }    
 33 
 34 void SetMyTimer(int witch, long nSecs, long nUSecs, bool bRepeat)
 35 {
 36     struct itimerval oThis, oPre;    
 37     memset(&oThis, 0, sizeof(oThis));
 38     memset(&oPre, 0, sizeof(oPre));
 39 
 40     oThis.it_value.tv_sec = nSecs;
 41     oThis.it_value.tv_usec = nUSecs;
 42     //oThis.it_interval.tv_sec = 0;
 43     //oThis.it_interval.tv_usec = 0;
 44     
 45     if(bRepeat)
 46     {
 47         oThis.it_interval.tv_sec = nSecs;
 48         oThis.it_interval.tv_usec = nUSecs;
 49     }
 50 
 51     //oPre.it_value.tv_sec = 0;
 52     //oPre.it_value.tv_usec = 0;
 53     //oPre.it_interval.tv_sec = 0;
 54     //oPre.it_interval.tv_usec = 0;
 55 
 56     int nRet = setitimer(witch, &oThis, &oPre);
 57     cout << "setitimer ret " << nRet << endl;
 58     
 59     if(-1 == nRet)
 60     {
 61         cout << "errno " << errno << endl;
 62         return;
 63     }
 64     
 65     cout << "pre timer:" << endl;
 66     cout << "it_interval.tv_sec:" << oPre.it_interval.tv_sec << endl;
 67     cout << "it_interval.tv_usec:" << oPre.it_interval.tv_usec << endl;
 68     cout << "it_value.tv_sec:" << oPre.it_value.tv_sec << endl;
 69     cout << "it_value.tv_usec:" << oPre.it_value.tv_usec << endl;
 70 }
 71 
 72 int main()
 73 {
 74     cout << "process id is " << getpid() << endl;
 75     cout << "SIG_IGN : " << SIG_IGN << endl;
 76     cout << "SIG_DFL: " << SIG_DFL << endl;
 77 
 78     sig_t ret = signal(SIGHUP, handle_sig);
 79     if(ret != SIG_ERR)
 80     {
 81         cout << ret << endl;
 82     }
 83     
 84     ret = signal(SIGINT, handle_sig);
 85     if(ret != SIG_ERR)
 86     {
 87         cout << ret << endl;
 88     }
 89 
 90     ret = signal(SIGQUIT, handle_sig);
 91     if(ret != SIG_ERR)
 92     {
 93         cout << ret << endl;
 94     }
 95 
 96     ret = signal(SIGALRM, handle_sig);
 97     if(ret != SIG_ERR)
 98     {
 99         cout << ret << endl;
100     }
101 
102     //unsigned uRet = alarm(2);
103     //cout << "alarm ret " << uRet << endl;
104     
105     SetMyTimer(ITIMER_REAL, 1, 0, true);
106 
107     while(true)
108     {
109         int nRet = pause();
110         cout << "nRet = " << nRet << endl;
111     }
112     
113     return 0;
114 } 
posted @ 2012-09-17 23:07  夜塔  阅读(362)  评论(0编辑  收藏  举报