ピーターソンのアルゴリズム
[Wikipedia|▼Menu]

ピーターソンのアルゴリズムは、通信のために共有メモリだけを使い2個[注 1]のプロセス間でリソースを競合することなく共有する相互排他のためのアルゴリズムである。これは、1981年、ロチェスター大学の Gary Peterson が定式化した。

ハードウェアレベルでは一般に、アトミックなアクセスを達成するのにピーターソンのアルゴリズムを必要とすることはない。プロセッサにはテスト・アンド・セット命令などが装備されていて、同時並行的アクセスを実現できる。特殊なソフトウェアの技術は必要ではない。
アルゴリズム flag[0] = 0 flag[1] = 0 p0: flag[0] = 1 p1: flag[1] = 1 turn = 1 turn = 0 while( flag[1] && turn == 1 ); while( flag[0] && turn == 0 ); // do nothing // do nothing // critical section // critical section ... ... // end of critical section // end of critical section flag[0] = 0 flag[1] = 0

このアルゴリズムでは、二つの変数 flag と turn を使用する。flag の値が 1 のとき、そのプロセスがクリティカルセクションに入りたいことを示している。turn はその時点で優先権を持つプロセスの識別子を保持する。プロセス P0 がクリティカルセクションに入るには、P1がクリティカルセクションに入ろうとしていないか、P1 が turn を 0 に設定して P0 に優先権を与えている必要がある。

このアルゴリズムは以下に述べるミューテックスの3つの基本条件を満たしている。
相互排他

P0とP1は決して同時にクリティカルセクションには入らない。P0がクリティカルセクションにあるときflag[1]かturnのどちらかが 0 である。どちらにしても P1 はクリティカルセクションに入れない。
進捗要求

プロセス P0 がクリティカルセクションに入ろうとしていない場合、P1 は即座にクリティカルセクションに入ることができる。P0 と P1 の間でクリティカルセクションに入るべき順番は全く決まっていない。
有限の待ち時間

プロセスはクリティカルセクション1回分の処理時間以上に待たされることはない。他のプロセスに優先権を与えると、そのプロセスはクリティカルセクションの最後まで動作し、自身のflagを 0 にするので、もう一方のプロセスがクリティカルセクションに入ることができるようになる。
2個のPOSIXスレッドを使用したC言語での実装例/* * ANSI C89 source, KNF style implementation of Peterson's Algorithm. * * Copyright (c) 2005, Matthew Mondor * Released in the public domain (may be licensed under the GFDL). * * Please fix any bugs as needed, preserving the KNF style and this comment, * unless considered inconvenient in which case you can do whatever you want * with the code. */#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <pthread.h>struct pa_desc { int *f0, *f1; int last;};void pa_init(void);void pa_desc_init(struct pa_desc *, int);void pa_desc_lock(struct pa_desc *);void pa_desc_unlock(struct pa_desc *);void *thread0_main(void *);void *thread1_main(void *);void threadx_critical(void);int main(int, char **);static int pa_f0, pa_f1, pa_last;/* Shared */#define BUCKETS 100int gi, g[BUCKETS];/* * Initializes the pa system. To be called by the process once before * initializing pa descriptors. */voidpa_init(void){ pa_f0 = pa_f1 = pa_last = 0;}/* * Initializes a pa descriptor for use by a thread. * One thread should use id 0 and the other one id 1. */voidpa_desc_init(struct pa_desc *d, int id){ assert(id == 0 |。id == 1); if (id == 0) { d->f0 = &pa_f0; d->f1 = &pa_f1; d->last = 1; } else if (id == 1) { d->f0 = &pa_f1; d->f1 = &pa_f0; d->last = 0; }}voidpa_desc_lock(struct pa_desc *d){ for (*d->f0 = 1, pa_last = d->last; *d->f1 == 1 && pa_last == d->last; ) ;}voidpa_desc_unlock(struct pa_desc *d){ *d->f0 = 0;}/* * Main loop of the first concurrent thread *//* ARGSUSED */void *thread0_main(void *args){ struct pa_desc d; pa_desc_init(&d, 0); for (;;) { pa_desc_lock(&d); threadx_critical(); pa_desc_unlock(&d); } /* NOTREACHED */ return NULL;}/* * Main loop of the second concurrent thread *//* ARGSUSED */void *thread1_main(void *args){ struct pa_desc d; pa_desc_init(&d, 1); for (;;) { pa_desc_lock(&d); threadx_critical(); pa_desc_unlock(&d); } /* NOTREACHED */ return NULL;}/* * Critical function which would normally have concurrency issues if two * threads executed it without locking. */voidthreadx_critical(void){ int i; /* Do something which would normally have dual concurrency issues */ for (i = 0; i < BUCKETS; i++) g[i] = gi++; for (i = 0; i < BUCKETS; i++) (void) printf("g[%d] = %d\n", i, g[i]);}/* * Test program which demonstrates that dual concurrency can be achieved * without locking using Peterson's algorithm. We run two concurrent threads * which voluntarily perform concurrency sensitive operations on a shared * memory region (gi, g[BUCKETS]). *//* ARGSUSED */intmain(int argc, char **argv){ pthread_t tid1, tid2; pthread_attr_t tattr; gi = 0; pa_init(); pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, 0); /* Note: a real application would perform error checking */ pthread_create(&tid1, &tattr, thread0_main, NULL); pthread_create(&tid2, &tattr, thread1_main, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL); /* NOTREACHED */ return EXIT_SUCCESS;}

最近のCPUは実行効率を改善するために命令やメモリアクセスの並べ替えを行う。そのようなプロセッサには何らかの方法でメモリアクセスの順番を変えないようにする機能がある。例えばメモリバリア命令がそれである。ピーターソンのアルゴリズムなどをアウト・オブ・オーダー実行プロセッサで実装するには、一般にそのような操作を使用して設計した順番通りに処理が行われるようにしなければならない。

そのようなCPUには不可分操作機能も備わっている。例えば、x86系プロセッサの XCHG 命令、AlphaMIPSPowerPCなどのアーキテクチャのLoad-Link/Store-Conditional命令などである。これらの命令は共有メモリを使用した手法よりも効率的に同期プリミティブを構築するのに使われる。
注釈^ "Operating Systems Review, January 1990 ('Proof of a Mutual Exclusion Algorithm', M Hofri)" で議論されているように、ピーターソンのアルゴリズムは2個以上のプロセスに一般化できる

関連項目

デッカーのアルゴリズム

ランポートのパン屋のアルゴリズム

ミューテックス

排他制御
.mw-parser-output .asbox{position:relative;overflow:hidden}.mw-parser-output .asbox table{background:transparent}.mw-parser-output .asbox p{margin:0}.mw-parser-output .asbox p+p{margin-top:0.25em}.mw-parser-output .asbox{font-size:90%}.mw-parser-output .asbox-note{font-size:90%}.mw-parser-output .asbox .navbar{position:absolute;top:-0.90em;right:1em;display:none}

この項目は、コンピュータに関連した書きかけの項目です。この項目を加筆・訂正などしてくださる協力者を求めていますPJ:コンピュータ/P:コンピュータ)。
.mw-parser-output .hlist ul,.mw-parser-output .hlist ol{padding-left:0}.mw-parser-output .hlist li,.mw-parser-output .hlist dd,.mw-parser-output .hlist dt{margin-right:0;display:inline-block;white-space:nowrap}.mw-parser-output .hlist dt:after,.mw-parser-output .hlist dd:after,.mw-parser-output .hlist li:after{white-space:normal}.mw-parser-output .hlist li:after,.mw-parser-output .hlist dd:after{content:" ・\a0 ";font-weight:bold}.mw-parser-output .hlist dt:after{content:": "}.mw-parser-output .hlist-pipe dd:after,.mw-parser-output .hlist-pipe li:after{content:" |\a0 ";font-weight:normal}.mw-parser-output .hlist-hyphen dd:after,.mw-parser-output .hlist-hyphen li:after{content:" -\a0 ";font-weight:normal}.mw-parser-output .hlist-comma dd:after,.mw-parser-output .hlist-comma li:after{content:"、";font-weight:normal}.mw-parser-output .hlist-slash dd:after,.mw-parser-output .hlist-slash li:after{content:" /\a0 ";font-weight:normal}.mw-parser-output .hlist dd:last-child:after,.mw-parser-output .hlist dt:last-child:after,.mw-parser-output .hlist li:last-child:after{content:none}.mw-parser-output .hlist dd dd:first-child:before,.mw-parser-output .hlist dd dt:first-child:before,.mw-parser-output .hlist dd li:first-child:before,.mw-parser-output .hlist dt dd:first-child:before,.mw-parser-output .hlist dt dt:first-child:before,.mw-parser-output .hlist dt li:first-child:before,.mw-parser-output .hlist li dd:first-child:before,.mw-parser-output .hlist li dt:first-child:before,.mw-parser-output .hlist li li:first-child:before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child:after,.mw-parser-output .hlist dd dt:last-child:after,.mw-parser-output .hlist dd li:last-child:after,.mw-parser-output .hlist dt dd:last-child:after,.mw-parser-output .hlist dt dt:last-child:after,.mw-parser-output .hlist dt li:last-child:after,.mw-parser-output .hlist li dd:last-child:after,.mw-parser-output .hlist li dt:last-child:after,.mw-parser-output .hlist li li:last-child:after{content:")\a0 ";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li:before{content:" "counter(listitem)" ";white-space:nowrap}.mw-parser-output .hlist dd ol>li:first-child:before,.mw-parser-output .hlist dt ol>li:first-child:before,.mw-parser-output .hlist li ol>li:first-child:before{content:" ("counter(listitem)" "}.mw-parser-output .navbar{display:inline;font-size:75%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}.mw-parser-output .infobox .navbar{font-size:88%}.mw-parser-output .navbox .navbar{display:block;font-size:88%}.mw-parser-output .navbox-title .navbar{float:left;text-align:left;margin-right:0.5em}

表示

編集


記事の検索
おまかせリスト
▼オプションを表示
ブックマーク登録
mixiチェック!
Twitterに投稿
オプション/リンク一覧
話題のニュース
列車運行情報
暇つぶしWikipedia

Size:13 KB
出典: フリー百科事典『ウィキペディア(Wikipedia)
担当:undef