中断可以这样编程吗?
我正在创建一个可以在后台生成程序的外壳。当程序开始使用一定量的内存(例如 100MB)时,我希望触发一个中断以导致函数运行。另一种方法是让进程继续运行并检查后台进程的状态。使用中断似乎使用更少的CPU资源。这是正确的吗?可以这样做吗?
我正在尝试在 Linux 中执行此操作。
I am creating a shell which can spawn programs in the background. When a program starts to use a certain amount of memory, like 100MB, I want an interrupt to be triggered that will cause a function to run. The alternative is to have a process keep running and checking the status of background processes. Using an interrupt seems to use less CPU resources. Is that correct? Is it possible to do this?
I'm trying to do this in Linux.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Linux 上执行此操作的一种方法是使用
ptrace
。然后,您可以使用 PTRACE_PEEKUSER 查看子进程在进行系统调用时向内核提供的值。您会寻找诸如 mmap 和/或 brk 之类的东西。被调用的函数是由执行 int 80 时 EAX 中的值定义的(抱歉,我不知道每个 mmap 或 brk 的数字)。One way to do this on Linux would be to use
ptrace
. You'd then use PTRACE_PEEKUSER to look at the values the child is supplying to the kernel when it makes system calls. You'd be looking for things like mmap and/or brk. The function being called is defined by the value in EAX when int 80 is executed (sorry, offhand I don't know the numbers for each mmap or brk).根据要求。如果捕获 malloc 调用就足够了,您可能会感兴趣:
如果,您想捕获 brk(2) 以及 - ( 相关文章),您可能想关注一些跟踪解决方案
或二进制仪器
Depending on requirements. If capturing malloc calls is enought you might be interested:
If, you'd like to capture brk(2) as well - (related article) , you might like to follow in some of tracing solution
or binary instrumentation
在 Linux 中,您可以使用
setrlimit
。根据您的情况,可以使用
RLIMIT_AS
或RLIMIT_DATA
。使用brk
的malloc
将在达到限制时失败。您可以监视malloc
返回的值,并自行发出信号。In Linux, you can set a resource limit for a process with
setrlimit
.In your case,
RLIMIT_AS
orRLIMIT_DATA
can be used.malloc
, which usesbrk
, will fail on reaching the limit. You can monitor the value returned bymalloc
, and raise a signal yourself.