Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
共有メモリ
Search
Satoru Takeuchi
PRO
March 22, 2025
Technology
160
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
82
Linuxカーネルの インタフェースは変わりうる?
sat
PRO
1
110
あなたの知らないバージョン命名規則
sat
PRO
3
930
Rook: Intro and Deep Dive with Ceph
sat
PRO
1
82
Machine Check Exception
sat
PRO
2
73
バイナリダンプの模様を読む
sat
PRO
0
120
cpコマンドはディスク上でデータを コピーしないことがある
sat
PRO
3
120
114-ファイルのshallow_copy.pdf
sat
PRO
2
70
113-Btrfsのスナップショット.pdf
sat
PRO
0
50
Other Decks in Technology
See All in Technology
The future of architectural work
ufried
0
170
デザインレビューは自動化できるのか?─ Flutter × Figma で UI 品質保証を考える
techiro
2
800
eBPF 簡介 2026 版
shunghsiyu
0
190
Maestro MCPとClaude Code Skillsによる自律的なE2Eテスト支援
asmz
0
420
複雑な業務システムを高速でつくるため、チーム全員をつくれる人へ
ayakurabuchi
0
140
バックオフィスに染み出すTypeScript ── clasp で安全に仕組みを作る
nealle
1
180
DBTS2026 盛り上がってますか!?Databricksが実現するAI時代のデータ基盤
akuwano
1
170
E2Eってつらくないですか?E2Eの管理コストを下げる手段とその実践
akaboshinit
6
3k
「いつか使う」の「いつか」は来ない 〜AI駆動開発における「作らない」判断について〜
ikki0006
0
130
TSKaigi Sendai 2026 - tsgo時代のTypeScript Playgroundはどう動くか
teamlab
PRO
0
100
OneLakeをデータソースにした AI Search(Foundry IQ)の ライフサイクル管理
ryuseiiida
0
230
SLOを阻む不安
sraku2159
0
220
Featured
See All Featured
WCS-LA-2024
lcolladotor
0
970
Darren the Foodie - Storyboard
khoart
PRO
4
4.1k
ラッコキーワード サービス紹介資料
rakko
5
5.7M
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
920
Joys of Absence: A Defence of Solitary Play
codingconduct
1
570
Un-Boring Meetings
codingconduct
0
490
Embracing the Ebb and Flow
colly
88
5.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
23k
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
6
1.3k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
Speed Design
sergeychernyshev
33
2.2k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
400
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