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
150
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
共有メモリ
以下動画のテキストです
https://youtu.be/dNdledEIJsE
Satoru Takeuchi
PRO
March 22, 2025
More Decks by Satoru Takeuchi
See All by Satoru Takeuchi
119-多機能ファイルシステムBtrfs_のコピー.pdf
sat
PRO
1
66
Linuxカーネルの インタフェースは変わりうる?
sat
PRO
1
92
あなたの知らないバージョン命名規則
sat
PRO
3
890
Rook: Intro and Deep Dive with Ceph
sat
PRO
1
63
Machine Check Exception
sat
PRO
2
63
バイナリダンプの模様を読む
sat
PRO
0
88
cpコマンドはディスク上でデータを コピーしないことがある
sat
PRO
3
86
114-ファイルのshallow_copy.pdf
sat
PRO
2
57
113-Btrfsのスナップショット.pdf
sat
PRO
0
40
Other Decks in Technology
See All in Technology
2026-09-04 SRE Tech Talk #15 怠惰なTerraform / Lazy Terraform
masasuzu
0
220
コスト最適化の「めんどくさい」を AWS FinOps Agent でチョット楽にする
classmethod_kaz
0
270
生成AI時代の クレデンシャルとパーミッション設計
nrinetcom
PRO
4
1.6k
Bet AI Day 2026丨バクラク Autopilot、業務システムの再設計
layerx
PRO
2
1.6k
AI時代におけるプロダクト横断勉強会の設計
zozotech
PRO
0
170
Azure App Service / Container Apps の組み込み認証
kuniteru
0
210
20260903 Tokyo Jazug Night #62 | Azure エンジニアよ、 その環境は本当にセキュアか?
olivia_0707
1
630
Sony-DroidKaigi2026
sony
1
400
enechainの内製セルフサービスプラットフォーム
hiyosi
0
130
AIエージェントの開発・提供におけるセキュリティリスクの論点と対策
flatt_security
2
610
プロダクト思考 × 基盤思考を AIで実現する Compound Engineering
tkc66buzz
1
290
DGX Sparkを2台使って いろいろ動かす話
sonoda_mj
1
140
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Test your architecture with Archunit
thirion
2
2.4k
WCS-LA-2024
lcolladotor
0
820
Paper Plane (Part 1)
katiecoart
PRO
1
11k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Odyssey Design
rkendrick25
PRO
2
800
A designer walks into a library…
pauljervisheath
211
25k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
440
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
660
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