星期二, 1月 03, 2006

Semaphore的使用

# Using a semaphore to get permission to access a printer.
可以把semaphore包裝(Encapsulate semaphore)在驅動printer的函數中,
Task-1呼叫該函數時並不會知道是否存取了semaphore.
Task-2也同時呼叫該函數時因為semaphore的已被Pend,
必須等待一段時間才能存取semaphore.

Pseudo code:
void Driver(char *string)
{
Acquire port's semaphore.
Send command to device.
if(GetSemaphore() != True)
{
Wait a while.
if(GetSemaphore() != True)
{
Release semaphore.
return (error code).
}
Release semaphore.
}
else
{
Release semaphore.
}


# Synchronization, I--> Unilateral rendezous.
II-> Bilateral rendezous.

沒有留言: