现在我们来看看send_signal()(kernel/signal.c)函数,这个函数的主要工作是分配并初始化一个sigqueue结构,然后把它添加到信号队列中。

static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
   struct sigpending *signals)
{
 struct sigqueue * q = NULL;
 int ret = 0;

 /*
  * fast-pathed signals for kernel-internal things like SIGSTOP
  * or SIGKILL.
  */
 if ((unsigned long)info == 2)
  goto out_set;

 /* Real-time signals must be queued if sent by sigqueue, or
    some other real-time mechanism.  It is implementation
    defined whether kill() does so.  We attempt to do so, on
    the principle of least surprise, but since kill is not
    allowed to fail with EAGAIN when low on memory we just
    make sure at least one signal gets delivered and don't
    pass on the info struct.  */

 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
          ((unsigned long) info < 2 ||
           info->si_code >= 0)));//分配sigqueue结构
 if (q) {//如果成功分配到sigqueue结构,把它添加到队列中,并对其初始化
  list_add_tail(&q->list, &signals->list);
  switch ((unsigned long) info) {
  case 0:
   q->info.si_signo = sig;
   q->info.si_errno = 0;
   q->info.si_code = SI_USER;
   q->info.si_pid = current->pid;
   q->info.si_uid = current->uid;
   break;
  case 1:
   q->info.si_signo = sig;
   q->info.si_errno = 0;
   q->info.si_code = SI_KERNEL;
   q->info.si_pid = 0;
   q->info.si_uid = 0;
   break;
  default:
   copy_siginfo(&q->info, info);//拷贝sigqueue结构
   break;
  }
 } else {
  if (sig >= SIGRTMIN && info && (unsigned long)info != 1
     && info->si_code != SI_USER)
  /*
   * Queue overflow, abort.  We may abort if the signal was rt
   * and sent by user using something other than kill().
   */
   return -EAGAIN;
  if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
   /*
    * Set up a return to indicate that we dropped
    * the signal.
    */
   ret = info->si_sys_private;
 }

out_set:
 sigaddset(&signals->signal, sig);//设置信号位图
 return ret;
}