とにかく進むねん。
勉強サークルのブログです。 もがきながらでも進むこと。 これがモットーです。
ブログ内検索
リンク
管理画面
新しい記事を書く
カレンダー
03
2025/04
05
S
M
T
W
T
F
S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
アーカイブ
2012 年 12 月 ( 3 )
2012 年 11 月 ( 11 )
2012 年 10 月 ( 4 )
2012 年 09 月 ( 5 )
2012 年 08 月 ( 4 )
プロフィール
HN:
betweens
性別:
非公開
カウンター
カテゴリー
宿題 ( 10 )
colinux 設定 ( 16 )
colinux network設定 ( 4 )
検索エンジン付き合い方 ( 9 )
C++ ( 2 )
未選択 ( 102 )
shell script ( 1 )
code golf ( 66 )
Haskell ( 14 )
Python Challenge ( 2 )
Project Euler ( 2 )
Python Study ( 3 )
英語 ( 1 )
JAVA ( 3 )
ゴミ情報 ( 1 )
RSS
RSS 0.91
RSS 1.0
RSS 2.0
最新CM
無題
[11/07 betweens]
無題
[11/07 元左席人]
無題
[08/30 betweens]
無題
[08/30 元左席人]
無題
[10/02 元左席人]
最新TB
最新記事
anarchy golf (Haskell) [Number lines]
(12/13)
anarchy golf (Haskell) [spirals] 【Vol.4】
(12/06)
anarchy golf (Haskell) [ two kinds of digit ] 読解
(12/05)
anarchy golf (Haskell) [Reduce fractions]
(11/13)
anarchy golf (Haskell) [Fill in the blanks]
(11/11)
最古記事
宿題 [2008.01a] colinux をインストールしよう
(05/10)
[2008.01a] colinux をインストールしよう(解答編1)
(05/15)
[2008.01a] colinux をインストールしよう(解答編2)
(05/17)
[2008.01a] colinux をインストールしよう(解答編3)
(05/18)
Google のノウハウ
(05/19)
フリーエリア
2025
04,12
08:09
[PR]
CATEGORY[]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
2009
11,25
00:11
Peterson の解法(OS)
CATEGORY[未選択]
アップしてなかったけど、Peterson の解法もアップしておきます。
要するに、ビジーウェイトで タネンバウムのOS本では奨めてなかった方法。
排他はできていると思うけど、実行するとCPUが100%になりました。
Ctrl + Alt + Delete で パフォーマンス見れば、CPUの度合いは分かります。
=========================================
#include <iostream>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/time.h>
pid_t enter_region(int process, bool* interested, int &turn)
{
interested[process] = true;
turn = process;
// other = 1-process
while ( turn == process && interested[1-process] == true ) ;
std::cout << "enter_region => process: " << process
<< "interested: " << interested[process] << std::endl;
fflush(stdout);
sleep(5);
}
pid_t leave_region(bool* interested, int process)
{
interested[process] = false;
std::cout << "leave_region => process: " << process
<< "interested: " << interested[process] << std::endl;
fflush(stdout);
}
int main (int argc, char* argv[])
{
const int segment = shmget(IPC_PRIVATE, 2, S_IRUSR|S_IWUSR);
bool *interested = (bool*)shmat(segment, NULL, 0);
const int segment2 = shmget(IPC_PRIVATE, 1, S_IRUSR|S_IWUSR);
int *turn = (int*)shmat(segment2, NULL, 0);
interested[0] = false; // Child is not interested.
interested[1] = false; // Parent is not interested.
const pid_t result_pid = fork();
std::cout << "pid : " << result_pid << std::endl;
fflush(stdout);
// Enter
if (result_pid == 0){
enter_region( 0, interested, *turn);
}else{
enter_region( 1, interested, *turn);
}
// Leave
if (result_pid == 0){
leave_region( interested, 0);
}else{
leave_region( interested, 1);
kill(result_pid, SIGTERM);
}
shmctl (segment, IPC_RMID, NULL);
shmctl (segment2, IPC_RMID, NULL);
return 0;
}
PR
コメント[0]
TB[]
<<
anarchy golf (Haskell) [ repeat line ]
|
HOME
|
メッセージパッシング (OS本)
>>
コメント
コメント投稿
NAME:
MAIL:
URL:
COLOR:
Black
LimeGreen
SeaGreen
Teal
FireBrick
Tomato
IndianRed
BurlyWood
SlateGray
DarkSlateBlue
LightPink
DeepPink
DarkOrange
Gold
DimGray
Silver
SUBJECT:
COMMENT:
PASS:
trackback
トラックバックURL
<<
anarchy golf (Haskell) [ repeat line ]
|
HOME
|
メッセージパッシング (OS本)
>>
忍者ブログ
[PR]