Linux C:接收到信号后,是否可以知道发送者的PID? [英] Linux C: upon receiving a signal, is it possible to know the PID of the sender?

查看:344
本文介绍了Linux C:接收到信号后,是否可以知道发送者的PID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的C程序处理SIGUSR1.当它接收到信号时,是否可以知道是谁发送的? IE,.获得该过程的pid?

Suppose my C program handles SIGUSR1. When it receives the signal, is it possible to know who sent it? I.e,. to get the pid of that process?

推荐答案

是的,如果您使用sigaction调用而不是signal来设置信号处理程序.这样做将使您设置一个带有三个参数的信号处理程序:

Yes, if you use the sigaction call to set up your signal handler instead of signal. Doing so will let you set up a signal handler that takes three parameters:

  • 一个int,用于信号编号(就像signal一样)
  • A siginfo_t *,它是一种结构,其中包含有关信号源的各种信息,包括发送方的pid(如果适用). (还包括有关诸如SIGSEGV之类的自动信号的信号原因的一些信息.)
  • A ucontext_t *,与哪个线程获得信号有关.大多是可忽略的.
  • An int, for the signal number (just like signal)
  • A siginfo_t *, which is a structure containing all sorts of information about the source of the signal, including the pid of the sender if applicable. (It also includes some information about the cause of the signal for automatic signals like SIGSEGV.)
  • A ucontext_t *, which has to do with which thread got the signal. Mostly ignorable.

这篇关于Linux C:接收到信号后,是否可以知道发送者的PID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆