Linux 中的工作线程
为什么有时需要创建新的工作线程来执行可延迟的工作函数,而不是仅仅将其调度到默认的“events/n”内核工作线程上?
Why is it sometimes necessary to create new worker threads, that execute deferrable work functions, instead of just scheduling it on the default "events/n" kernel worker thread ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事件工作线程需要复用许多任务,因此不允许阻塞它(在单个任务中调用schedule())。然而,有许多任务需要阻塞(等待磁盘、网络、某些设备等),并且内核中有许多函数在内部阻塞(如信号量甚至内存分配)。此类函数只能在系统调用内或专用内核线程中代表用户态运行。
虽然理论上可以将此类代码重写为一长串事件,但这会使其变得复杂且难以维护,并且内核线程的开销非常小。
The events worker thread need to multiplex many tasks, so it's not allowed to block it (call schedule() inside single task). However there are many tasks that need to block (wait for disk, network, some device etc.) and there are many functions in kernel that block internally (like semaphores or even memory allocation). Such functions may only be run on behalf of userland inside a syscall or in a dedicated kernel thread.
While it would be theoretically possible to rewrite such code to a long chain of events, it would make it complex and unmaintainable and the overhead of kernel thread is quite small.