C语言 如何触发 SIGUSR1 和 SIGUSR2?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6168636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:47:35  来源:igfitidea点击:

How to trigger SIGUSR1 and SIGUSR2?

csignals

提问by haunted85

I'm getting acquainted with signals in C. I can't figure out what kind of signals SIGUSR1and SIGUSR2are and how can I trigger them. Can anyone please explain it to me?

我越来越与C.我认识的信号不能想出什么样的信号SIGUSR1,并SIGUSR2是,我怎么能触发它们。任何人都可以向我解释一下吗?

回答by Oliver Charlesworth

They are user-definedsignals, so they aren't triggered by any particular action. You can explicitly send them programmatically:

它们是用户定义的信号,因此它们不会由任何特定操作触发。您可以以编程方式显式发送它们:

#include <signal.h>

kill(pid, SIGUSR1);

where pidis the process id of the receiving process. At the receiving end, you can register a signal handler for them:

哪里pid是接收进程的进程ID。在接收端,您可以为它们注册一个信号处理程序:

#include <signal.h>

void my_handler(int signum)
{
    if (signum == SIGUSR1)
    {
        printf("Received SIGUSR1!\n");
    }
}

signal(SIGUSR1, my_handler);

回答by cnicutar

They are signals that application developers use. The kernel shouldn't ever send these to a process. You can send them using kill(2)or using the utility kill(1).

它们是应用程序开发人员使用的信号。内核不应该将这些发送到进程。您可以使用kill(2)或使用该实用程序发送它们kill(1)

If you intend to use signals for synchronization you might want to check real-time signals (there's more of them, they are queued, their delivery order is guaranteed etc).

如果您打算使用信号进行同步,您可能需要检查实时信号(它们更多,它们已排队,它们的交付顺序得到保证等)。

回答by Евгений Кашинский

terminal 1

1号航站楼

dd if=/dev/sda of=debian.img

terminal 2

2号航站楼

killall -SIGUSR1 dd

go back to terminal 1

回到1号航站楼

34292201+0 records in
34292200+0 records out
17557606400 bytes (18 GB) copied, 1034.7 s, 17.0 MB/s