セマフォ: semaphore)とは、計算機科学において、並行プログラミング環境での複数の実行単位(主にプロセス)が共有する資源にアクセスするのを制御する際の、単純だが便利な抽象化を提供する変数または抽象データ型である。

語源の腕木式信号機

概要 編集


使使使便使01/使/使

[1]

en:semaphore

 


101使使使

11使使

10使19871[2]使

 


使使/

1

使



使

使

使

使

 


使 wait()  signal() 

2 V signal() P wait()VSP

S使P使V使

wait()  signal() 

wait(): 1 wait() 

signal(): 1

OS1

UNIXVP
function V(semaphore S, integer I):
    [S ← S + I]

function P(semaphore S, integer I):
    repeat:
        [if S >= I:
            S ← S - I
            break]

FIFO1PV1

/使使2使

: /  


/11使N





/2emptyCount fullCount emptyCount fullCount 2 emptyCount  fullCount 

 useQueue 2

emptyCount NfullCount 0useQueue 1
produce:
    P(emptyCount)
    P(useQueue)
    putItemIntoQueue(item)
    V(useQueue)
    V(fullCount)


consume:
    P(fullCount)
    P(useQueue)
    item ← getItemFromQueue()
    V(useQueue)
    V(emptyCount)



(一)1fullCount 0

(二)emptyCount N

(三) useQueue 1

(四)fullCount 

emptyCount useQueue  emptyCount + fullCount  N 

 


PVV verhogenPproberen[3]passeerprobeerpakkenV verhoogP passerenP probeer te verlagen prolaag [4][5][6][7] increase  decrease  V使

ALGOL 68 Linux[8]PV down  up wait  signalacquire  release[1]pend  post  procure  vacate 

 


2

/


使  

POSIX  


POSIXsemaphore.h使sem_init()[9]sem_open()[10]sem_init()psharedsem_wait()sem_post()sem_destroy()sem_close()

Windows  


WindowsWin32 APICreateSemaphore()WaitForSingleObject()ReleaseSemaphore()CloseHandle()[11]OpenSemaphore()[12]使[13][14]

MFCC++/RAIIWin32CSemaphore[15]CSingleLock使[16]

.NET  


.NET Framework 2.0System.Threading.Semaphore 使1.1Win32 API使使[17]Semaphore.NET Core.NET 5Windows使使[18]

.NET Framework 4.0System.Threading.SemaphoreSlim [19]

Java  


Javajava.util.concurrent.Semaphore 使Java 5 使

Perl  


PerlThread::Semaphore Coro::Semaphore 使

Python  


Pythonthreading Semaphore

Swift  


SwiftGrand Central Dispatch Dispatch  DispatchSemaphore 

脚注 編集

注釈 編集

  1. ^ Javajava.util.concurrent.Semaphoreクラスなどで使用されている。

出典 編集



(一)^ Dijkstra, Edsger W. Cooperating sequential processes (EWD-123). E.W. Dijkstra Archive. Center for American History, University of Texas at Austin. (original; transcription) (September 1965)

(二)^ The Little Book of Semaphores Allen B. Downey

(三)^ Silberschatz, Galvin & Gagne 2008, p. 234

(四)^ Dijkstra, Edsger W. Over Seinpalen (EWD-74). E.W. Dijkstra Archive. Center for American History, University of Texas at Austin. (original; transcription)

(五)^ Dijkstra, Edsger W. MULTIPROGAMMERING EN DE X8 (EWD-51). E.W. Dijkstra Archive. Center for American History, University of Texas at Austin. (original; transcription) ()

(六)^  "try-and-decrease"  "try-and..." 

(七)^ (PATCH 1/19) MUTEX: Introduce simple mutex implementation Linux Kernel Mailing List, 19 December 2005

(八)^ Linus Kernel hacking HOWTO LinuxGrill.com

(九)^ sem_init | The Open Group Base Specifications Issue 7, 2018 edition IEEE Std 1003.1-2017

(十)^ sem_open | The Open Group Base Specifications Issue 7, 2018 edition IEEE Std 1003.1-2017

(11)^ Using Semaphore Objects - Win32 apps | Microsoft Learn

(12)^ OpenSemaphoreW function (synchapi.h) - Win32 apps | Microsoft Learn

(13)^ CreateSemaphoreA function (winbase.h) - Win32 apps | Microsoft Learn

(14)^ CreateSemaphoreW function (synchapi.h) - Win32 apps | Microsoft Learn

(15)^ CSemaphore Class | Microsoft Learn

(16)^ Multithreading: How to Use the MFC Synchronization Classes | Microsoft Learn

(17)^ Semaphore Class (System.Threading) | Microsoft Learn

(18)^  - .NET | Microsoft Learn

(19)^ Semaphore  SemaphoreSlim - .NET | Microsoft Learn

参考文献 編集

  • Silberschatz, Abraham; Galvin, Peter Baer; Gagne, Greg (2008), Operating System Concepts (8th ed.), John Wiley & Sons. Inc, ISBN 978-0-470-12872-5 

関連項目 編集

外部リンク 編集