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
プロセスの生成 fork&exec編
Search
Satoru Takeuchi
PRO
December 29, 2024
Technology
120
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
プロセスの生成 fork&exec編
以下動画のテキストです
https://youtu.be/DLSho8DvdUk
Satoru Takeuchi
PRO
December 29, 2024
More Decks by Satoru Takeuchi
See All by Satoru Takeuchi
119-多機能ファイルシステムBtrfs_のコピー.pdf
sat
PRO
1
67
Linuxカーネルの インタフェースは変わりうる?
sat
PRO
1
92
あなたの知らないバージョン命名規則
sat
PRO
3
890
Rook: Intro and Deep Dive with Ceph
sat
PRO
1
64
Machine Check Exception
sat
PRO
2
65
バイナリダンプの模様を読む
sat
PRO
0
89
cpコマンドはディスク上でデータを コピーしないことがある
sat
PRO
3
87
114-ファイルのshallow_copy.pdf
sat
PRO
2
57
113-Btrfsのスナップショット.pdf
sat
PRO
0
40
Other Decks in Technology
See All in Technology
DEFCON_CHV_CTF_Write-up.pdf
bata_24
0
140
コスト最適化の「めんどくさい」を AWS FinOps Agent でチョット楽にする
classmethod_kaz
0
300
#jawssonic2026 あの時代が悪かった ~動かなかったSageMakerと共に迎えたイベント当日~
ktkn1129
0
140
module Synths; end
asonas
1
130
10分で知る最近のOmarchy
komagata
0
250
GuardDuty 検知対応を DevOps Agent で効率化しようとしている話 / GuardDuty Investigations with DevOps Agent
masahirokawahara
1
350
Slack上でインフラをトラブルシュートする! Agentic Platform Engineeringの第一歩
teru0x1
2
530
作り直せるコードは迅速に 作り直せないDBは慎重に - AI時代のプロダクトエンジニアが「判断の不可逆性」で開発速度を変える話
kinosuke01
0
300
GoCon2026 - Open Source, Open World
sanposhiho
3
3.1k
AIに丸投げしないトイル削減 / Eliminating Toil Without Leaving It All to AI
kohbis
4
1.1k
Code4Lib JAPANカンファレンス2026 開会挨拶 / Code4Lib JAPAN Conference 2026: Opening Remarks
ykiyota
0
330
なぜSRE・セキュリティは評価されないのか?守りの組織を事業成長エンジンに変えた実践
cscengineer
PRO
3
2.4k
Featured
See All Featured
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
490
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
280
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
The agentic SEO stack - context over prompts
schlessera
0
920
How STYLIGHT went responsive
nonsquared
100
6.3k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
230
Believing is Seeing
oripsolob
1
210
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Transcript
プロセスの生成 fork&exec編 Dec. 29th, 2024 Satoru Takeuchi X: satoru_takeuchi 1
はなすこと • あるプログラムを実行しているプロセスから別のプログラムを別プロセスとして実行 する方法 ◦ 要するに「bashからechoコマンドを実行するコードはどうなっているのか?」ということ • forkの後にexecを呼ぶことで実現する 2
fork&exec: CPU上で動作する処理の観点 1. CPU0上で動く親プロセスがfork()を呼ぶ 2. fork()後にCPU0上で親プロセスが、CPU1上で子プロセスが動く 3. 子プロセスがexec()を呼びプログラムBを実行 3 CPU0
CPU1 fork()呼び出し fork()から復帰 fork()から復帰 親プロセス (プログラムA) カーネル 親プロセス (プログラムA) 子プロセス (プログラムA) 処理が分岐(fork) カーネル 子プロセス (プログラムB) exec()呼び出し exec()から復帰
fork&exec: 物理メモリの観点 4 親プロセスの メモリ (プログラムA) (1) fork呼び出し時 カーネルの メモリ
親プロセスの メモリ (プログラムA) カーネルの メモリ 子プロセスの メモリ (プログラムA) CPU0上で 実行中のコード (2) fork()復帰後 (3) exec()復帰後 CPU0上で 実行中のコード CPU1上で 実行中のコード 親プロセスの メモリ (プログラムA) カーネルの メモリ 子プロセスの メモリ (プログラムB) CPU1上で 実行中のコード CPU0上で 実行中のコード
コード: fork-and-exec.py #!/usr/bin/python3 import os, sys ret = os.fork() if
ret == 0: print("子プロセス: pid={}, 親プロセスのpid={}".format(os.getpid(), os.getppid())) os.execve("/bin/echo", ["echo", "pid={} からこんにちは ".format(os.getpid())], {}) exit() elif ret > 0: print("親プロセス: pid={}, 子プロセスのpid={}".format(os.getpid(), ret)) exit() sys.exit(1) 5
実行結果 $ ./fork-and-exec.py 親プロセス: pid=137998, 子プロセスのpid=137999 子プロセス: pid=137999, 親プロセスのpid=137998 pid=137999
からこんにちは 6
まとめ • あるプログラムを実行しているプロセスから別のプログラムを別プロセスとして実行 するにはforkとexecを併用する ◦ まわりくどいうようだが本当にこうなっている ◦ こういうことをするposix_spawnという関数もあるが、内部的には fork,execを順番に呼び出している だけ
• 過去動画で説明した「CoWを使った実際のfork」の挙動がわかればうれしさがわ かってくる。これについては後日紹介 7