linux信号处理编程实验报告,c++ 简单的Linux信号处理

[Q-3] Does the terminate variable in my example have to be volatile? I’ve

seen many examples where this variable is volatile, and others where

it is not.

标志终止应该是volatile sig_atomic_t:

因为处理函数可以异步调用。也就是说,处理程序可能在程序中的任何点被调用,不可预测。如果两个信号在非常短的间隔内到达,则一个处理程序可以在另一个中运行。并且认为更好的做法是声明volatile sig_atomic_t,这种类型始终以原子方式访问,避免中断对变量的访问的不确定性。 volatile告诉编译器不要优化并将其放入寄存器。 (详细内容见:Atomic Data Access and Signal Handling)。

另一个参考:24.4.7 Atomic Data Access and Signal Handling。

此外,7.14.1.1-5中的C11标准表明,只有可以从信号处理程序(访问其他程序具有未定义的行为)访问易失性sig_atomic_t的对象。

[Q-4] I’ve read that signal() is now deprecated, and to use sigaction(). Are

there any really good examples to show how to convert from the

previous signal() call? I’m having trouble with the new structure that

I have to create/pass and how it all fits together.

下面的示例(和评论中的链接)可能有所帮助:

// 1. Prepare struct

struct sigaction sa;

sa.sa_handler = sighandler;

// 2. To restart functions if interrupted by handler (as handlers called asynchronously)

sa.sa_flags = SA_RESTART;

// 3. Set zero

sigemptyset(&sa.sa_mask);

/* 3b.

// uncomment if you wants to block

// some signals while one is executing.

sigaddset( &sa.sa_mask, SIGINT );

*/

// 4. Register signals

sigaction( SIGINT, &sa, NULL );

参考文献:

> Beginning Linux Programming, 4th Edition:在本书中,您的代码在“第11章:进程和信号”中很好地用sigaction()解释。

> sigaction documentation,包括一个例子(快速学习)。

> GNU C库:Signal Handling

*我从1开始,目前我正在阅读3 GNU库

[Q-5] Is the second call to signal() necessary? Is there something similar that I need to be concerned with for sigaction()?

为什么你在程序终止之前将它设置为默认操作我不清楚。我认为下面的段落会给你一个答案:

07007

The call to signal establishes signal handling for only one occurrence of a signal. Before the signal-handling function is called, the library resets the signal so that the default action is performed if the same signal occurs again. Resetting signal handling helps to prevent an infinite loop if, for example, an action performed in the signal handler raises the same signal again. If you want your handler to be used for a signal each time it occurs, you must call signal within the handler to reinstate it. You should be cautious in reinstating signal handling. For example, if you continually reinstate SIGINT handling, you may lose the ability to interrupt and terminate your program.

signal()函数仅定义下一个接收信号的处理程序,之后恢复默认处理程序。因此,如果程序需要使用非默认处理程序继续处理信号,则信号处理程序需要调用signal()。

[Q-1a] Is any signal handling necessary?

是的,Linux会为你清理。例如,如果不关闭文件或套接字,Linux将在进程终止后执行清除。但是Linux可能不需要立即执行清理,它可能需要一些时间(可能是为了保持系统性能高或一些其他问题)。例如,如果不关闭tcp-socket并且程序终止,内核不会立即关闭套接字以确保所有数据都已传输,如果可能,TCP保证传递。

[Q-1b] Therefore, can I just replace the signal handler with just an infinite loop and let the OS gracefully exit the threads, de-allocate the memory, etc?

不,操作系统只在程序终止后才执行清理。当进程执行时,分配给该进程的资源不会被OS声明。 (操作系统无法知道您的进程是否处于无限循环中 – this is an unsolvable problem)。如果希望在进程终止后,操作系统为您执行清理操作,那么您不需要处理信号(即使您的进程由于信号异常终止)。

[Q] All I’m trying to accomplish to to have my: main loop run until either ctrlc or power is disconnected or something really bad happens.

不,有一个限制!你不能捕获所有的信号。某些信号不可捕获。 SIGKILL和SIGSTOP都是终止信号。引用一:

070010

The SIGKILL signal is used to cause immediate program termination. It cannot be handled or ignored, and is therefore always fatal. It is also not possible to block this signal.

我不知道,但可能是你可以在Windows系统中做这样的事情:通过编写TSR(某种内核模式挂钩)。我记得从我的论文时间,一些病毒不能被终止甚至从任务管理器,但我也相信他们欺骗用户的管理员权限。

我希望这个答案会帮助你。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值