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
Systems Programming - SoCoded 2013
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Andy Delcambre
September 20, 2013
Programming
200
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Systems Programming - SoCoded 2013
Andy Delcambre
September 20, 2013
More Decks by Andy Delcambre
See All by Andy Delcambre
Git and GitHub at YAPC:Asia
adelcambre
1
3.4k
Git, GitHub, Etc
adelcambre
3
260
Ruby Systems Programming
adelcambre
4
1.2k
Debugging Production Service Oriented Systems
adelcambre
3
450
Ruby, Rails and Engine Yard - Úll
adelcambre
1
340
SFRuby Nov 2011 - Release Early and Release Often
adelcambre
2
96
Release early and Release often
adelcambre
2
550
Other Decks in Programming
See All in Programming
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
260
Copilot CLI の継戦能力を高める コンテキスト管理
nozomutu
1
1.2k
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
4.9k
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
660
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
230
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
150
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
250
OSもどきOS
arkw
0
530
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
150
The NotImplementedError Problem in Ruby
koic
1
710
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
540
Featured
See All Featured
Visualization
eitanlees
152
17k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
200
Designing Experiences People Love
moore
143
24k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
200
Deep Space Network (abreviated)
tonyrice
0
170
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
830
The Limits of Empathy - UXLibs8
cassininazir
1
350
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.1k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
570
Optimising Largest Contentful Paint
csswizardry
37
3.7k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Transcript
Systems Programming
operate and control the computer hardware and to provide a
platform for running application software http://en.wikipedia.org/wiki/System_software “
application software “
web software “
Building Blocks The Kernel System Calls Sockets File Descriptors HTTP
None
The Kernel
Your Code The Kernel Hardware
Your Code
Your Code User Mode
Your Code Math User Mode
Your Code Math Access Memory User Mode
The Kernel
The Kernel Anything
The Kernel Anything Everything
System Calls
User Mode Kernel Mode
User Mode Kernel Mode Your Program System Calls
$ strace -p <server pid>
None
120,602 Calls One Request
linux$ wc -l syscall_table.S 326
mov! eax, 5 mov! ebx, path mov! ecx, flags int!
80h open(path, flags)
linux$ cat syscall_table.S | grep -c sys_ni_syscal 65 Not Implemented
File Descriptors
Everything is a File
Things that are Files (Non Exhaustive)
Things that are Files (Non Exhaustive) Files
Things that are Files (Non Exhaustive) Links DVDs Hard Drives
USB Keys Sockets* Files Directories Sound Cards Graphics Cards Printers Pipes Terminals UNIX Sockets Shared Memory
read() write() close() lseek()* File API
0 -> /dev/null 1 -> unicorn.log 2 -> unicorn.log 3
-> pipe:[3753224456] 4 -> pipe:[3753224456] 5 -> socket:[19730] 6 -> pipe:[3753224457] 7 -> socket:[19731] 8 -> socket:[19732] 9 -> pipe:[3753224457] 10 -> production.log 11 -> socket:[3766066290] 12 -> socket:[3753228795] 13 -> socket:[3753229266] 14 -> socket:[3753229276] 15 -> socket:[3753229285] 16 -> socket:[3753229307] 17 -> socket:[3753235718] 18 -> socket:[3753235910] 19 -> socket:[3753242775] 20 -> socket:[3753242776] 21 -> socket:[3753242778] 22 -> socket:[3753239097] 23 -> socket:[3753242942] 24 -> socket:[3753242944] 25 -> socket:[3753239202] 26 -> socket:[3753239203] 27 -> socket:[3753239204] 28 -> socket:[3753242981] 29 -> socket:[3753242982] 30 -> socket:[3753243929] 31 -> socket:[3753240046] 32 -> socket:[3753245881] 33 -> socket:[3753248970] 34 -> socket:[3753253476] 35 -> pygments-ruby.log 36 -> /dev/null 37 -> pipe:[3753260503] 38 -> pipe:[3753260504] 39 -> socket:[3753344667] 40 -> pipe:[3753260505] 41 -> socket:[3765810413] 42 -> socket:[3765810440] 43 -> socket:[3765848228] $ ls -l /proc/32660/fd
BSD Sockets
1980 1970 1960 1990 Multics Unix (Unics) BSD BSD 4.2
TCP/IP
socket() bind() listen() accept() read() and write() close()
HTTP
HTTP Request GET /index.html HTTP/1.1 User-Agent: curl/7.24.0 Host: www.google.com Accept:
*/*
HTTP Response HTTP/1.1 200 OK Date: Tue, 05 Mar 2013
23:03:52 GMT Cache-Control: private, max-age=0 Content-Type: text/html Server: gws Transfer-Encoding: chunked <data>
There Will be Code
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end Require the socket lib
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end Some constants for later
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end socket = Socket.new(:INET, :STREAM)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end socket.listen(5)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end while client_socket = socket.accept[0]
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end path = client_socket.readline.split[1]
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end path = client_socket.readline.split[1] GET /index.html HTTP/1.1
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end filename = File.expand_path("../#{path}", __FILE__)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end if File.file?(filename)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}")
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") OK = "HTTP/1.1 200 OK\n"
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.write(NOT_FOUND)
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.write(NOT_FOUND) NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end client_socket.close
require 'socket' NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found"
OK = "HTTP/1.1 200 OK\n" socket = Socket.new(:INET, :STREAM) sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) socket.listen(5) while client_socket = socket.accept[0] path = client_socket.readline.split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.write(OK) client_socket.write("Content-Length: #{contents.size}\n\n") client_socket.write("#{contents}") else client_socket.write(NOT_FOUND) end client_socket.close end
def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)
or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn
def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)
or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn sock = Socket.new(AF_INET6, SOCK_STREAM, 0)
def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)
or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn sock.bind( Socket.pack_sockaddr_in(port, addr))
def new_ipv6_server(addr, port, opt) opt.key?(:ipv6only) or return Kgio::TCPServer.new(addr, port) defined?(IPV6_V6ONLY)
or abort "Socket::IPV6_V6ONLY not defined, upgrade Ruby and/or your OS" sock = Socket.new(AF_INET6, SOCK_STREAM, 0) sock.setsockopt(IPPROTO_IPV6, IPV6_V6ONLY, opt[:ipv6only] ? 1 : 0) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind(Socket.pack_sockaddr_in(port, addr)) IO_PURGATORY << sock Kgio::TCPServer.for_fd(sock.fileno) end ret = IO.select(l, nil, SELF_PIPE, @timeout) and ready = ret[0] Unicorn IO.select(l, nil, SELF_PIPE, @timeout)
The Kernel System Calls Sockets File Descriptors HTTP
Further Reading Linux System Programming Robert Love
Further Reading Unix Network Programming Richard Stevens
Further Reading Advanced Programming in the UNIX Environment Richard Stevens
Andy Delcambre adelcambre @
None
Thanks! adelcambre @