Linux信号处理的应用

linux的system和sleep都涉及到信号的处理,以下是关于在实现system函数和sleep所涉及到的原理。

1.每个进程只有一个闹钟时钟,alarm;且alarm和setitimer共享一个定时器,因此它们会互相干扰。不过通过timer_create和timer_settime可以创建定时器和设置定时器,同时所发送的信号不限定于SIGALRM,可以是其它的SIGINT,SIGQUIT等信号。

2.sigsuspend(sigset_t *set)阻塞等待不再集合set的信号,并且调用对应的信号处理函数。sigsuspending保留发生的信号;

3.sigprocmask屏蔽信号集或打开信号集,返回之前的信号集,以用于未来还原到原来的状态。

4.sigaction设置信号对应的信号处理函数,和返回之前的信号处理函数,以便于未来还原到原来的处理信号处理函数

5.sleep可以被各种信号打断

6.信号实现父子进程间的同步,sigsuspend

7.popen创建管道,fork产生一个子进程,关闭管道的不使用端,执行一个shell以运行命令,然后等待命令结束

8.pipe(fd)管道是半双工的,socketpair域套接字是全双工的

system和sleep函数
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/time.h>

void sig_alarm(int signo)
{
  printf("catch SIGALRM in sleep_cmd\n");
}

void sig_real_alarm(int signo)
{
  printf("catch real SIGALRM\n");
}

int system_cmd(const char *pCmd)
{
  pid_t pid;
  int status;
  sigset_t newMask, oldMask;
  struct sigaction currAction, oldAction;

  if (pCmd == NULL)
  {
      return 1;
  }
  currAction.sa_handler = SIG_IGN;
  currAction.sa_flags = 0;
  sigemptyset(&currAction.sa_mask);

  if (sigaction(SIGINT, &currAction, &oldAction) < 0)
  {
      return (-1);
  }
  if (sigaction(SIGQUIT,&currAction, &oldAction) < 0)
  {
      return (-1);
  }

  sigemptyset(&newMask);
  sigemptyset(&oldMask);
  sigaddset(&newMask, SIGCHLD);
  if (sigprocmask(SIG_BLOCK, &newMask, &oldMask) < 0)
  {
      return (-1);
  }

  if ( (pid = fork()) < 0)
  {
      status = -1;
  }
  else if (pid == 0)
  {
      sigaction(SIGINT, &oldAction, NULL);
      sigaction(SIGQUIT, &oldAction, NULL);
      sigprocmask(SIG_SETMASK, &oldMask, NULL);
      execl("/bin/bash", "sh", "-c", pCmd, (char*)0);
      _exit(127);
  }
  else
  {
      while (waitpid(pid, &status, 0) < 0)
      {
          if (errno != EINTR)
          {
              status = -1;
              break;
          }
      }
  }
  
  if (sigaction(SIGINT, &oldAction, NULL) < 0)
  {
      return (-1);
  }
  if (sigaction(SIGQUIT, &oldAction, NULL) < 0)
  {
      return (-1);
  }
  if (sigprocmask(SIG_SETMASK, &oldMask, NULL) < 0)
  {
      return (-1);
  }
  return (status);
}


//sleep_cmd未处理与以前设置的闹钟的交互作用, 现在的处理会使之前设置的闹钟失效
unsigned int sleep_cmd(unsigned int nsec)
{
  sigset_t newMask, oldMask, suspendMask;
  struct sigaction newAction, oldAction;
  unsigned int uslept;

  newAction.sa_handler = sig_alarm;
  sigemptyset(&newAction.sa_mask);
  newAction.sa_flags = 0;
  if (sigaction(SIGALRM, &newAction, &oldAction) < 0)
  {
      printf("set SIGALRM new action error\n");
  }

  sigemptyset(&newMask);
  sigaddset(&newMask, SIGALRM);
  sigprocmask(SIG_BLOCK, &newMask, &oldMask);

  alarm(nsec);
  
  suspendMask = oldMask;
  sigdelset(&suspendMask, SIGALRM);   //不阻塞SIGALRM
  sigsuspend(&suspendMask);

  uslept = alarm(0);
  
  if (sigaction(SIGALRM, &oldAction, NULL) < 0)
  {
      printf("set SIGALRM old Action error\n");
  }
  sigprocmask(SIG_SETMASK, &oldMask, NULL);

  return uslept;
}


int main(int argc, char **argv)
{
  int index = 1;
  int cmdLen = 0;
  unsigned int remainSec = 0;
  char cmdBuff[1024] = {0};
  //struct itimerval its;
  struct itimerspec itms;
  struct sigaction sigAction;
  timer_t timerid;
  struct sigevent sev;

  if (argc <= 1)
  {
      printf("no cmd of argv\n");
      return 0;
  }
  for (index = 1; index < argc; index++)
  {
      sprintf(cmdBuff + cmdLen, "%s ", argv[index]);
      cmdLen += strlen(argv[index]) + 1;
  }
  cmdBuff[cmdLen] = '\0';
  printf("%s\n", cmdBuff);
  
  system_cmd(cmdBuff);

  sigAction.sa_handler = sig_real_alarm;
  sigemptyset(&sigAction.sa_mask);
  sigAction.sa_flags = 0;
  sigaction(SIGALRM, &sigAction, NULL);

  /*
 its.it_value.tv_sec = 1;
 its.it_value.tv_usec = 0;
 its.it_interval.tv_sec = 1;
 its.it_interval.tv_usec = 0;
 if (setitimer(ITIMER_REAL, &its, NULL)) 
 {
     printf("set timer error\n");
 }*/
  //setitimer 与 alarm共用一个定时器,互相干扰。
  //创建定时器,发送的信号为SIGALRM
  sev.sigev_notify = SIGEV_SIGNAL;
  sev.sigev_signo = SIGALRM;
  sev.sigev_value.sival_ptr = &timerid;
  if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1)
  {
     printf("timer_create error\n");
  }

  itms.it_value.tv_sec = 1;
  itms.it_value.tv_nsec = 0;
  itms.it_interval.tv_sec = itms.it_value.tv_sec;
  itms.it_interval.tv_nsec = itms.it_value.tv_nsec;

  //设置定时器
  if (timer_settime(timerid, 0, &itms, NULL) == -1)
  {
      printf("timer_settime error\n");
  }
  
  remainSec = sleep_cmd(5);
  printf("remain second: %d\n", remainSec);

  /*setitimer(ITIMER_REAL, &its, NULL);*/
  
  while(1)
  {
      sleep(10);
      raise(SIGKILL);
  }
  return 0;
}

Comments