2007年11月7日 星期三

Mutual exclusion

接續

看spec. 與真的要實做lib的 初步體驗


原本不懂的同步問題
給他研究一下

在Operating System Concepts (sixth edition)
第七章探討主題 Process Synchronization
The Critical-Section Problem是其中一個議題
書上提到
The critical-section problem is to design a protocol that the processes can use to cooperate.

如果有許多的processes(or threads)
是不是需要使用到這個概念去解
不知道charles當初是不是這樣想的

再提到對於critical section problem的解法有
A solution to the critical-section problem must satisfy the following three requirements:
1. mutual exclusion(=mutex)
If process Pi is executing in its critical section, then no other processes can be executing in thier critical sections.

2. progress
有process離開 critical section後, 有人想要進 就可以馬上進critical section

3. bounded waiting
第n個 process在等待第n-1個 processes進入之後,就可以進入 critical seciton

MFC提供 CreateMutex

The handle returned by CreateMutex has MUTEX_ALL_ACCESS access to the new mutex object and can be used in any function that requires a handle to a mutex object.

to release its ownership, the thread must call ReleaseMutex once for each time that the mutex satisfied a wait.

Multiple processes can have handles of the same mutex object, enabling use of the object for interprocess synchronization. To provide object sharing, a process can specify the name of a mutex object in a call to the CreateMutex function.

Use the CloseHandle function to close the handle.


搭配WaitForSingleObject處理同步問題
http://www.hosp.ncku.edu.tw/~cww/htmapi72.htm
用紅綠燈的觀念講解 WaitForSingleObject
個人認為講解的相當清楚
綠燈=signaled
紅燈=unsignaled

ResetEvent
This method sets the state of the event to nonsignaled until explicitly set to signaled by the SetEvent method.

最後用 ReleaseMutex來釋放 mutex

This function releases ownership of the specified mutex object.


在進入critical section之間
就可以達到傳送cmd以及底層是否運作完成的目的

透過Facebook分享

沒有留言: