Mac进入睡眠状态后,我的应用程序会如何处理? [英] What happens to my app when my Mac goes to sleep?

查看:792
本文介绍了Mac进入睡眠状态后,我的应用程序会如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Mac OS X进入睡眠状态时,由于关闭笔记本电脑或从Apple菜单中选择睡眠",它如何暂停正在执行的进程?

When Mac OS X goes to sleep, due to closing a laptop or selecting "Sleep" from the Apple menu, how does it suspend an executing process?

我认为非窗口进程只是在任意执行点被挂起.这对于Cocoa应用程序是否也适用,或者操作系统是否等到控制权返回运行循环调度程序并在已知"位置进入睡眠状态?是否有任何现代的操作系统可以做到这一点?或者,无论它在做什么,通常只要挂起一个应用程序通常足够安全吗?

I suppose non-windowed processes are simply suspended at an arbitrary point of execution. Is that also true for Cocoa apps, or does the OS wait until control returns to the run loop dispatcher, and goes to sleep in a "known" location? Does any modern OS do that, or is it usually safe enough to simply suspend an app no matter what it is doing?

我很好奇,因为从应用程序的角度来看,任何时候都允许睡眠意味着系统时钟可能会突然大幅跳升.在编码时,我通常不会考虑这种可能性.

I'm curious, because allowing sleep to occur at any moment means, from the app's perspective, the system clock could suddenly leap forward by a significant amount. That's a possibility I don't usually consider while coding.

推荐答案

如果CPU当前正在实际执行您的应用程序代码,那么您的应用程序将被完全中断.您的应用程序通过任务调度程序不断获得执行时间,该任务调度程序决定哪个应用程序获得CPU时间,在哪个内核上以及持续多长时间.一旦系统真正进入睡眠状态,调度程序便不再给您的应用程序任何时间,因此它会在任何时候停止执行,这几乎可以发生在任何地方.但是,内核必须处于干净状态.这意味着如果您刚刚调用内核(许多libC函数这样做),而该调用不在某个安全点(例如,睡眠,等待条件变为真等),或者可能持有关键的内核锁(例如,漏斗),内核可能会暂停睡眠,直到该调用返回用户空间或执行达到安全点为止,然后才最终从任务计划程序中取消您的应用.

Your app is interrupted exactly where it is that moment if the CPU is actually currently executing code of your app. Your app constantly gets execution time by the task scheduler, that decides which app gets CPU time, on which core, and for how long. Once the system really goes to sleep, the scheduler simply gives no time to your app any longer, thus it will stop execution wherever it is at that moment, which can happen pretty much everywhere. However, the kernel must be in a clean state. That means if you just made a call into the kernel (many libC functions do) and this call is not at some safe-point (e.g. sleeping, waiting for a condition to become true, etc.) or possible holding critical kernel locks (e.g. funnels), the kernel may suspend sleep till this call returns back to user space or execution reaches such a safe-point before it finally cancels your app from the task scheduler.

您可以打开内核端口并注册睡眠/唤醒事件.在这种情况下,当系统要进入睡眠状态时,您的应用将收到一个事件.您有几种可能性.一种是回复它,以使系统可以继续前进.另一个是暂停睡眠;但是,苹果表示某些事件最多可以暂停30秒,之后,无论您的应用程序是否喜欢,系统都将继续运行.最后,您可以取消它;尽管并非所有事件都可以取消.如果系统已经确定它将进入睡眠状态,则您最多只能将其暂停30秒,或者一次允许它,您无法取消它.但是,您也可以听一个事件,系统询问应用程序,现在可以进入睡眠状态了,在那里您可以回答否",从而导致睡眠被取消.

You can open a kernel port and register for sleep/wake-up events. In that case, your app will receive an event, when the system wants to go to sleep. You have several possibilities. One is to reply to it, that the system may progress. Another one is to suspend sleep; however, Apple says certain events can be suspended at most 30 seconds, after that, the system will just continue, whether your app likes it or not. And finally, you can cancel it; though not all events can be canceled. If the system already decided it will go to sleep, you can only suspend this by at most 30 seconds or allow it at once, you cannot cancel it. However, you can also listen to an event, where the system asks apps, if it is okay to go to sleep now and there you can reply "no", causing a sleep to be canceled.

可以睡觉吗"和我打算睡觉"之间的区别是:如果应用了节能设置,即如果用户没有移动鼠标,则发送第一个消息或在此处配置的时间键入任何内容.在这种情况下,系统只会询问睡眠是否正常.苹果DVD播放器之类的应用会说不",因为用户很可能观看DVD,因此不与计算机交互,仍然没有理由入睡. OTOH,如果用户关闭Mac Book,不询问应用程序,则系统将确定进入睡眠状态,并仅通知应用程序,这些应用程序现在最多有30秒对其做出反应.

The difference between "Is it okay to sleep" and "I'm planing on going to sleep" is: The first one is sent if the power saving settings are applied, that is, if the user has not moved the mouse or typed anything for the time configured there. In that case the system will just ask, if sleep is okay. An app like Apple's DVD Player will say "no", because most likely the user watches a DVD and thus doesn't interact with the computer, still no reason to go to sleep. OTOH, if the user closes his Mac Book, apps are not asked, the system will go to sleep for sure and just informs apps, that have now up to 30 seconds to react to it.

唤醒事件也很有趣.例如.如果系统唤醒,则打开的文件可能无法访问(已拔出外部驱动器),或者网络套接字不再起作用(网络已更改).因此,您可以在使用某些应用程序部件之前重新初始化它们,并且会遇到或多或少预期的错误.

Wake-up events can also be quite interesting to catch. E.g. if your system wakes up, open files might be inaccessible (an external drive has been unplugged) or network sockets won't work any longer (network has changed). So you may re-init certain app parts before using them and running into errors that are more or less expected.

Apple有关捕获这些事件的页面.

这篇关于Mac进入睡眠状态后,我的应用程序会如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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