什么时候以及如何中断系统调用? [英] When and how are system calls interrupted?

查看:100
本文介绍了什么时候以及如何中断系统调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对> send()成功的"atomic"吗? ,因为我认为它实际上通常涉及系统调用,而不仅仅是在套接字上发送.

This is a followup question to Is a successful send() "atomic"?, as I think it actually concerns system calls in general, not just sends on sockets.

哪个系统调用可以被打断,什么时候可以打断?我已经了解了SA_RESTART,但并不完全了解发生了什么.

Which system calls can be interrupted, and when they are, where is the interruption handled? I've learned about SA_RESTART, but don't exactly understand what is going on.

  • 如果我在没有SA_RESTART的情况下进行系统调用,该调用是否可以被与我的应用程序无关的任何中断(例如,用户输入)中断,但需要操作系统中止我的调用并执行其他操作?还是只是被与我的过程直接相关的信号(CTRL + C,套接字关闭等)打断?

  • If I make a system call without SA_RESTART, can the call be interrupted by any kind of interrupts (e.g. user input) that don't concern my application, but require the OS to abort my call and do something else? Or is it only interrupted by signals that directly concern my process (CTRL+C, socket closed, ...)?

设置SA_RESTART时,send()或任何其他慢速"系统调用的语义是什么?它会一直阻塞直到我的所有数据传输完毕或套接字断开时,还是返回的数字小于send()参数中的计数?

When setting SA_RESTART, what are the semantics of a send() or any other "slow" syscall? Will it always block until all of my data is transmitted or the socket goes down, or can it return with a number smaller than the count in send()'s parameter?

在哪里实施重新启动?操作系统是否知道我希望在发生任何中断时重新启动该调用,还是某个信号发送到我的进程然后由库代码处理?还是我必须自己做,例如将呼叫包装在while循环中,并根据需要重试?

Where is restarting implemented? Does the OS know that I want the call to be restarted upon any interrupts, or is some signal sent to my process and then handled by library code? Or do I have to do it myself, e.g. wrap the call in a while loop and retry as often as necessary?

推荐答案

任何

System calls can be interrupted by any signal, this includes such signals as SIGINT (generated by CTRL-C), SIGHUP, etc.

设置SA_RESTART时,如果在接收到信号之前发送了任何数据,则send()将返回(具有发送的计数),如果设置了发送超时(如那些send()那样,则将返回错误EINTR).无法重新启动),否则send()将重新启动.

When SA_RESTART is set, a send() will return (with the sent count) if any data was transmitted before the signal was received, it will return an error EINTR if a send timeout was set (as those can't be restarted), otherwise the send() will be restarted.

系统调用重新启动是在内核的信号处理代码中实现的.系统调用在检测到未决信号(或等待被信号中断)后在内部返回-ERESTARTSYS,这将导致信号处理代码将指令指针和相关寄存器恢复到调用前的状态,从而使syscall重复执行.

System call restarting is implemented in the kernel's signal handling code. The system call internally returns -ERESTARTSYS upon detecting a pending signal (or having a wait interrupted by a signal), which causes the signal handling code to restore the instruction pointer and relevant registers to the state before the call, making the syscall repeat.

这篇关于什么时候以及如何中断系统调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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