当我的 Mac 进入睡眠状态时,我的应用程序会发生什么情况?

发布于 2024-07-05 01:21:16 字数 282 浏览 8 评论 0原文

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

我认为非窗口进程只是在任意执行点挂起。 对于 Cocoa 应用程序来说也是如此,还是操作系统会等到控制权返回到运行循环调度程序并在“已知”位置进入睡眠状态? 现代操作系统是否会这样做,或者通常是否足够安全,可以简单地暂停应用程序,无论它在做什么?

我很好奇,因为从应用程序的角度来看,允许随时发生睡眠意味着系统时钟可能会突然大幅向前跳跃。 这是我在编码时通常不会考虑的可能性。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆萌少年 2024-07-12 01:21:16

查看这篇维基百科文章。 Cavver 正确地指出,网络连接之类的事情可能会超时,因此这些服务可能会中断。

Check out this Wikipedia article. Cavver is correct in stating that things like network connections may time out, and thus those services may be interrupted.

雨落□心尘 2024-07-12 01:21:16

如果您设置时间,它也会跳到正在运行的程序。 也没什么特别的。

And if you set the time it also appears to leap forward to the running programs. Nothing special either.

〗斷ホ乔殘χμё〖 2024-07-12 01:21:16

我相信它只会暂停所有应用程序,无论它们位于何处。

请记住,无论如何,这种情况总是会发生。 由于上下文切换,应用程序不断暂停和恢复。 因此,实际上,时钟可以在应用程序中的任意 2 条指令之间跳转,尽管通常不会以明显/显着的方式跳转。

如果操作系统等待应用程序返回到某个主循环,您可能会遇到应用程序导致睡眠挂起的情况。 如果他们做了很多工作并且没有返回到运行循环调度程序,他们将阻止机器进入睡眠状态。 那可就不太好办了。 :)

I believe it will just suspend all apps wherever they happen to be.

Remember, this happens all the time anyway. Applications are constantly suspended and resumed due to context switching. So, really, the clock could jump between any 2 instructions in your app, though usually not in a noticable/significant way.

If the OS waited for the app to return to some main loop you could run into situations where applications cause the sleep to hang. If they're doing a lot of work and not returning to the run loop dispatcher they would prevent the machine from going to sleep. That wouldn't be very good. :)

鹤仙姿 2024-07-12 01:21:16

这取决于您的应用程序。
如果您正在与外部系统交互(考虑网络或通过 USB/火线等执行某些操作),那么它可能会受到影响。 在 OSX 上运行的应用程序运行有限的时间(最多 10 毫秒),之后它会被内核中断,内核会从进程队列中安排一个新进程在 CPU 上运行。 这对于应用程序来说是透明的,应用程序“认为”它始终在 CPU 上运行。 因此,除了时间提前之外,向睡眠的过渡没有什么不同。
如果您需要知道已转换为睡眠模式,请参阅此技术说明,其中详细介绍了如何接收有关状态更改的通知:注册和取消注册睡眠和唤醒通知

It depends on your app.
If you are interacting with external systems (think networking or doing something over usb/firewire,etc) then it might be affected. An application running on OSX gets to run for a limited time ( max 10ms ) , after which it is interrupted by the kernel which schedules a new process from the process queue to run on the CPU. This is transparent for the application , which "thinks" that it runs all the time on the CPU. Thus , a transition to sleep is no different - apart from the time jumping ahead.
If you need to be aware that there was a transition to sleep mode please refer to this tech note which details how to receive notifications about the state change : Registering and unregistering for sleep and wake notifications

水晶透心 2024-07-12 01:21:16

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

您可以打开内核端口并注册睡眠/唤醒事件。 在这种情况下,当系统想要进入睡眠状态时,您的应用程序将收到一个事件。 你有几种可能性。 一是回复它,系统才能进步。 另一种是暂停睡眠; 然而,Apple 表示某些事件最多可以暂停 30 秒,之后系统将继续运行,无论您的应用程序是否喜欢。 最后,您可以取消它; 但并非所有活动都可以取消。 如果系统已经决定进入睡眠状态,您只能暂停最多 30 秒或立即允许,无法取消。 但是,您也可以监听一个事件,系统会询问应用程序是否可以立即睡觉,然后您可以回答“不”,从而取消睡眠。

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

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

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

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 possibly 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.

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.

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's page regarding catching these events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文