Linux内核中的信号机制——信号发送

发表于:2013-2-16 10:23

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:ce123    来源:51Testing软件测试网采编

分享:

  现在我们来看看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;
}

43/4<1234>
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号