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
Ruby Systems Programming
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Andy Delcambre
March 08, 2013
Programming
1.2k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby Systems Programming
From Ruby on Ales 2013
Andy Delcambre
March 08, 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
Systems Programming - SoCoded 2013
adelcambre
2
200
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
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
160
Contextとはなにか
chiroruxx
1
290
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
A2UI という光を覗いてみる
satohjohn
1
130
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
190
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
210
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
260
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
500
dRuby over BLE
makicamel
2
330
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
390
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
2
1.4k
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
Featured
See All Featured
The Impact of AI in SEO - AI Overviews June 2024 Edition
aleyda
5
1.1k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.7k
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
Become a Pro
speakerdeck
PRO
31
6k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
730
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
440
The Curse of the Amulet
leimatthew05
1
13k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
860
Building an army of robots
kneath
306
46k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
850
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Leo the Paperboy
mayatellez
7
1.8k
Transcript
Ruby Systems Programming Andy delcambre Monday, March 11, 13
@adelcambre sounds like welcome Andy Delcambre Monday, March 11, 13
Monday, March 11, 13
gist Monday, March 11, 13
Monday, March 11, 13
diploma goes here Monday, March 11, 13
Delcambre Software Monday, March 11, 13
CS 333 Operating systems CS 494 Networking Monday, March 11,
13
Monday, March 11, 13
Ruby Systems Programming Monday, March 11, 13
“operate and control the computer hardware and to provide a
platform for running application software” http://en.wikipedia.org/wiki/System_software Monday, March 11, 13
User Mode Kernel Mode Monday, March 11, 13
User Mode Monday, March 11, 13
User Mode Math Monday, March 11, 13
User Mode Math Access Memory* Monday, March 11, 13
Kernel Mode Monday, March 11, 13
Kernel Mode Anything Monday, March 11, 13
Kernel Mode Anything Everything Monday, March 11, 13
User Mode Kernel Mode Monday, March 11, 13
User Mode Kernel Mode Your Program System Calls Monday, March
11, 13
Obligatory Jessica Monday, March 11, 13
$ strace -p <server pid> Monday, March 11, 13
Monday, March 11, 13
120,602 Calls One Request Monday, March 11, 13
linux$ wc -l syscall_table.S 326 Monday, March 11, 13
mov!eax, 5 mov!ebx, path mov!ecx, flags int!80h open(path, flags) Monday,
March 11, 13
linux$ cat syscall_table.S | grep -c sys_ni_syscal 65 Monday, March
11, 13
linux$ cat syscall_table.S | grep -c sys_ni_syscal 65 “Not Implemented”
Monday, March 11, 13
Monday, March 11, 13
1980 1970 1960 1990 Multics Unix (Unics) BSD BSD 4.2
TCP/IP Monday, March 11, 13
BSD Sockets Monday, March 11, 13
•socket() •bind() •listen() •accept() •send() and recv() •close() Monday, March
11, 13
TCP Client Server SYN SYN/ACK Data ACK Data/ACK FIN ACK
FIN ACK ACK Monday, March 11, 13
TCP Client Server SYN SYN/ACK Data ACK Data/ACK FIN ACK
FIN ACK ACK Monday, March 11, 13
TCP Client Server SYN SYN/ACK Data ACK Data/ACK FIN ACK
FIN ACK ACK Monday, March 11, 13
TCP Client Server SYN SYN/ACK Data ACK Data/ACK FIN ACK
FIN ACK ACK Monday, March 11, 13
$ lsof -p 34866 -a -i COMMAND PID TYPE NODE
NAME ruby 34866 IPv4 TCP 127.0.0.1:80 (LISTEN) ruby 34866 IPv4 TCP 127.0.0.1:80->127.0.0.1:61898 (ESTABLISHED) Monday, March 11, 13
HTTP Request GET /index.html HTTP/1.1 User-Agent: curl/7.24.0 Host: www.google.com Accept:
*/* Monday, March 11, 13
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> Monday, March 11, 13
There Will be Code Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end Require the socket lib Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end Some constants for later Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end socket = Socket.new(:INET, :STREAM) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end sockaddr = Socket.sockaddr_in(11080, '127.0.0.1') socket.bind(sockaddr) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end socket.listen(5) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end while client_socket = socket.accept[0] Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end path = client_socket.recv(1024).split[1] Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end path = client_socket.recv(1024).split[1] GET /index.html HTTP/1.1 Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end filename = File.expand_path("../#{path}", __FILE__) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end if File.file?(filename) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) OK = "HTTP/1.1 200 OK\n" Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end client_socket.send(NOT_FOUND, 0) Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end client_socket.send(NOT_FOUND, 0) NOT_FOUND = "HTTP/1.1 404 Not Found\nContent-Length: 9\n\nNot Found" Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end client_socket.close Monday, March 11, 13
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.recv(1024).split[1] filename = File.expand_path("../#{path}", __FILE__) if File.file?(filename) contents = File.read(filename) client_socket.send(OK, 0) client_socket.send("Content-Length: #{contents.size}\n\n", 0) client_socket.send("#{contents}", 0) else client_socket.send(NOT_FOUND, 0) end client_socket.close end Monday, March 11, 13
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 Monday, March 11, 13
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) Monday, March 11, 13
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)) Monday, March 11, 13
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) Monday, March 11, 13
Thanks! @adelcambre Monday, March 11, 13