Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
共有メモリ
Search
Satoru Takeuchi
PRO
March 22, 2025
Technology
3
110
共有メモリ
以下動画のテキストです
https://youtu.be/dNdledEIJsE
Satoru Takeuchi
PRO
March 22, 2025
Tweet
Share
More Decks by Satoru Takeuchi
See All by Satoru Takeuchi
様々なファイルシステム
sat
PRO
0
240
ソースを読む時の思考プロセスの例-MkDocs
sat
PRO
1
170
ソースを読むプロセスの例
sat
PRO
15
9.9k
メモリマップトファイル
sat
PRO
1
130
「Linux」という言葉が指すもの
sat
PRO
4
210
APIとABIの違い
sat
PRO
5
190
ファイルシステムへのアクセス方法
sat
PRO
0
77
ファイルシステム
sat
PRO
1
74
低レイヤソフトウェア技術者が YouTuberとして食っていこうとした話
sat
PRO
7
6.2k
Other Decks in Technology
See All in Technology
IBC 2025 動画技術関連レポート / IBC 2025 Report
cyberagentdevelopers
PRO
2
150
現場データから見える、開発生産性の変化コード生成AI導入・運用のリアル〜 / Changes in Development Productivity and Operational Challenges Following the Introduction of Code Generation AI
nttcom
1
470
オブザーバビリティと育てた ID管理・認証認可基盤の歩み / The Journey of an ID Management, Authentication, and Authorization Platform Nurtured with Observability
kaminashi
1
660
SQLAlchemy の select(User).where(User.id =="123") を理解してみる/sqlalchemy deep dive
3l4l5
3
350
Okta Identity Governanceで実現する最小権限の原則 / Implementing the Principle of Least Privilege with Okta Identity Governance
tatsumin39
0
170
[VPoE Global Summit] サービスレベル目標による信頼性への投資最適化
satos
0
240
オブザーバビリティが育むシステム理解と好奇心
maruloop
2
1.1k
ゼロコード計装導入後のカスタム計装でさらに可観測性を高めよう
sansantech
PRO
1
390
もう外には出ない。より快適なフルリモート環境を目指して
mottyzzz
13
10k
Azure Well-Architected Framework入門
tomokusaba
1
130
RemoteFunctionを使ったコロケーション
mkazutaka
1
110
AWS UG Grantでグローバル20名に選出されてre:Inventに行く話と、マルチクラウドセキュリティの教科書を執筆した話 / The Story of Being Selected for the AWS UG Grant to Attending re:Invent, and Writing a Multi-Cloud Security Textbook
yuj1osm
1
130
Featured
See All Featured
Docker and Python
trallard
46
3.6k
Side Projects
sachag
455
43k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.6k
Making the Leap to Tech Lead
cromwellryan
135
9.6k
Producing Creativity
orderedlist
PRO
347
40k
Designing Experiences People Love
moore
142
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Being A Developer After 40
akosma
91
590k
GitHub's CSS Performance
jonrohan
1032
470k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
Building Adaptive Systems
keathley
44
2.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Transcript
共有メモリ Mar. 22nd, 2025 Satoru Takeuchi X: satoru_takeuchi 1
はじめに • プロセスは通常別のプロセスのメモリは見えない • プロセス間で情報をやりとりするプロセス間通信のため、あえて一部のメモリ領域を お互いに見えるようにすることもある。これが共有メモリ 2
共有メモリの概念図 3 プロセスAの仮想アドレス 0 200 物理メモリ 0 プロセスAのメモリ 500 400
300 仮想アドレス 物理アドレス 0-100 200-300 100-200 300-400 プロセスAのページテーブル プロセスBの仮想アドレス 0 200 仮想アドレス 物理アドレス 0-100 400-500 100-200 300-400 プロセスBのページテーブル 100 プロセスBのメモリ 共有メモリ 200 100 600 700
実現方法 • System V共有メモリ • POSIX共有メモリ • mmap(今回はこれを使う) ◦ 📝
過去動画: その75 プロセスへのメモリ割り当て (1) mmap 4
実験プログラム: shared-memory.py 1. 共有メモリ領域を作成 2. 共有メモリ領域にデータを書き込み 3. forkする 4. 子プロセスが共有メモリ領域のデータを書き換えて終了
5. 親プロセスが共有メモリ領域のデータを読み出し a. 期待値: データが書き換わっている 5
コード例(1/2): 共有メモリの作成&fork PAGE_SIZE = 4096 data = 1000 print(" 子プロセス生成前のデータの値:
{}".format(data)) shared_memory = mmap.mmap(-1, PAGE_SIZE, flags=mmap.MAP_SHARED) shared_memory[0:8] = data.to_bytes(8, byteorder) pid = os.fork() if pid < 0: print("fork() に失敗しました", file=os.stderr) sys.exit(1) # 次ページに続く 6
コード例(2/2): 子プロセスでデータを書き換え 7 # 前ページから続く if pid == 0: #
子プロセス data = int.from_bytes(shared_memory[0:8], byteorder) data *= 2 shared_memory[0:8] = data.to_bytes(8, byteorder) sys.exit(0) # 親プロセス os.wait() data = int.from_bytes(shared_memory[0:8], byteorder) print(" 子プロセス終了後のデータの値: {}".format(data))
実行結果 8 $ ./shared-memory 子プロセス生成前のデータの値: 1000 子プロセス終了後のデータの値: 2000
共有メモリに複数プロセスが同時にアクセスしたら? • 恐らく期待した通りに動作しない(例外あり) ◦ 見えるべきデータが見えない ◦ 書けるべきデータが書かれない • 一般に排他制御によって問題を解決する(例外あり) ◦
📝 過去動画: その18 シェルスクリプトで学ぶ排他制御 9
おわりに • 共有メモリを使うとプロセス間で通信ができる • 整合性を保つために共有メモリへのアクセスは排他制御する必要がある • 他にも色々な方法がある: シグナル、パイプ、ソケット、メッセージキュー ◦ 📝
過去動画: その88 シグナル 10