Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
414
|
第十一章
超限運轉
11 章〈休眠與等待〉一節所探討的介面都能保證至少可以休眠所要求的時間(否則
會傳回錯誤碼以指出此事)。所要求的延遲時間沒有過去,這些介面絕不會成功返回。
然而,間隔時間比所要求的延遲時間還長是有可能的。
此現象可能是由於簡單的排班行為—所要求的時間可能已經過去,而且核心可能已經準
時喚醒行程,但是排班器可能已經選擇運行不同的任務。
然而,還存在一個更詭異的起因:
計時器超限運轉
timer overrun
)。當計時器的粒度
比所要求的時間間隔還大,便會發生此現象。舉例來說,假設系統計時器的時鐘週期具
10 ms 的時間間隔,而且有一個行程要求了 1 ms 的休眠時間。系統只能夠在 10 ms
的時間間隔(interval)量測時間以及回應時間相關事件(例如,喚醒一個休眠中的行
程)。如果,行程發出休眠要求時,計時器在距離一個週期屆滿前 1 ms 的地方,則一切
相安無事—在這 1 ms 中,所要求的時間(1 ms)將會過去,而且核心將會喚醒行程。
然而,如果行程送出休眠要求的同時,計時器剛好屆滿一個週期,行程將會修眠額外
9 ms !也就是,將有九次的 1 ms 超限運轉。平均而言,一個週期為
X
的計時器將具有
X/2
的超限運轉率。
使用高精確度的時間來源,例如 POSIX 時鐘所提供的那些,以及較高值的
HZ
,可最小
化計時器的超限運轉。
休眠的替代方案
如果可能,你應該避免休眠。通常,你無法避免,那也沒關係—特別是,如果你的程式
碼的修眠時間小於 1 秒。然而,若程式碼以修眠來「忙碌等待」 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AGILE學習手冊 | SCRUM、XP、精實和看板方法

AGILE學習手冊 | SCRUM、XP、精實和看板方法

Andrew Stellman, Jennifer Greene
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
算法技术手册(原书第2 版)

算法技术手册(原书第2 版)

George T.Heineman, Gary Pollice, Stanley Selkow

Publisher Resources

ISBN: 9789862769812