操作系統恐龍書第十版課後答案 ch07

7.1

Explain why Windows and Linux implement multiple locking mechanisms. Describe the circumstances under which they use spinlocks,mutex locks, semaphores, and condition variables. In each case, explain why the mechanism is needed.

這些操作系統根據應用程式開發者的需求提供不同的鎖定機制。自旋鎖對於多處理器系統很有用,因為線程可以在忙碌循環中運行(短時間內),而不是承擔被放入睡眠隊列的開銷。互斥鎖對於鎖定資源很有用。Solaris 2 使用自適應互斥鎖,這意味著在多處理器機器上,互斥鎖是用自旋鎖實現的。當資源需要長時間持有時,信號量和條件變量是更合適的同步工具,因為長時間自旋是低效的。

7.2 Windows provides a lightweight synchronization tool called slim reader
–writer locks. Whereas most implementations of reader–writer locks
favor either readers or writers, or perhaps order waiting threads using a
FIFO policy, slim reader–writer locks favor neither readers nor writers,
nor are waiting threads ordered in a FIFO queue. Explain the benefts of
providing such a synchronization tool.

簡單性。如果讀者-寫者鎖提供公平性或偏向讀者或寫者,它們會涉及更多的開銷。提供這樣一個簡單的同步機制可以使鎖的訪問速度更快。在需要讀者-寫者鎖的情況下,使用這種鎖可能是最合適的,但快速獲取和釋放它們同樣重要。

7.3 Describe what changes would be necessary to the producer and consumer processes in Figure 7.1 and Figure 7.2 so that a mutex lock could be used instead of a binary semaphore.

需要將對 wait(mutex) 和 signal(mutex) 的調用替換為對互斥鎖 API 的調用,例如 acquire(mutex) 和 release(mutex)

7.4 Describe how deadlock is possible with the dining-philosophers problem.

如果所有哲學家同時拿起他們的左叉子,當他們轉向拿起右叉子時,他們會發現右叉子不可用,並且會在等待右叉子可用時被阻塞。這種在等待資源可用時的阻塞情況就是死鎖。

7.5 Explain the difference between signaled and non-signaled states with
Windows dispatcher objects.

處於信號狀態的對象是可用的,線程在嘗試獲取它時不會被阻塞。當鎖被獲取時,它處於非信號狀態。當鎖被釋放時,它會轉回到信號狀態。

7.6 Assume val is an atomic integer in a Linux system. What is the value of
val after the following operations have been completed?
atomic set(&val,10);
atomic sub(8,&val);
atomic inc(&val);
atomic inc(&val);
atomic add(6,&val);
atomic sub(3,&val);

最終的數值是 10 - 8 + 1 + 1 + 6 - 3 = 7。

7.7 Describe two kernel data structures in which race conditions are possible. Be sure to include a description of how a race condition can occur.

這個問題有很多答案。一些內核數據結構包括進程(PID)管理系統、內核進程表和調度隊列。使用 PID 管理系統時,可能會同時創建兩個進程,並且在為每個進程分配唯一 PID 時會出現競爭條件。同樣類型的競爭條件也可能出現在內核進程表中:可能會同時創建兩個進程,並且在為它們分配內核進程表中的位置時會出現競爭條件。對於調度隊列,一個進程可能正在等待現在可用的 I/O。與此同時,另一個進程正在進行上下文切換。這兩個進程同時被移動到可運行隊列中。因此,在可運行隊列中存在競爭條件。

7.8 The Linux kernel has a policy that a process cannot hold a spinlock while
attempting to acquire a semaphore. Explain why this policy is in place.

因為獲取信號量可能會使進程在等待信號量可用時進入睡眠狀態。自旋鎖應該只持有短時間,而處於睡眠狀態的進程可能會持有自旋鎖過久。

7.9 Discuss the tradeoff between fairness and throughput of operations
in the readers–writers problem. Propose a method for solving the
readers–writers problem without causing starvation.

在讀者-寫者問題中,通過偏向多個讀者而不是允許單個寫者獨占訪問共享值,可以提高吞吐量。另一方面,偏向讀者可能會導致寫者的飢餓。在讀者-寫者問題中,可以通過為等待的進程保留時間戳來避免飢餓。當寫者完成任務時,它會喚醒等待時間最長的進程。當讀者到達並注意到另一個讀者正在訪問數據庫時,只有在沒有等待的寫者時,它才會進入臨界區。這些限制可以保證公平性。

7.10 Explain the difference between software and hardware transactional
memory.

軟體事務記憶體完全由軟體提供,通過編譯器將必要的工具注入到代碼中的事務塊中,無需硬體支持。相反,硬體事務記憶體不需要軟體支持,而是使用快取層次結構和快取一致性協議來提供事務記憶體。

  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值