An (abridged) Ruby Plumber's Guide to *nix

1,349 views

Published on

The Lightning-Talk version of A Ruby Plumber's Guide to *nix.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,349
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

An (abridged) Ruby Plumber's Guide to *nix

  1. 1. An (abridged) Ruby Guide to *nix Plumbing The fun of a little knowledge applied unwisely http://slides.games-with-brains.net
  2. 2. unix favours agility only build what you need reuse what you already have change your tools as [des | requ]ired use scripts and text files where possible
  3. 3. processes Kernel.system Kernel.spawn Kernel.fork Kernel.exec IO.popen
  4. 4. files IO.for_fd IO.sys[open | read | write | seek] Kernel.select Fcntl
  5. 5. wrapping a file descriptor require 'fcntl' filemode = Fcntl::O_CREAT | Fcntl::O_RDWR | Fcntl::O_APPEND descriptor = IO.sysopen “test.dat”, filemode file = IO.for_fd descriptor file.syswrite “hello” file.sysseek 0 file.sysread 10 produces: hello
  6. 6. interprocess communication IO.pipe sockets Signal
  7. 7. accessing the *nix kernel Kernal.syscall ruby/dl
  8. 8. a fragile approach the native syscall is a very primitive interface error codes file descriptors & pointer addresses modifiable buffers ruby syscall doesn’t support modifiable buffers but it does wrap errors as Errno exceptions
  9. 9. IO.sysx the hard way On MacOS X file = syscall 5, “test.dat”, 1 # open O_WRONLY (fcntl.h) syscall 4, file, “some textn”, 10 # write syscall 3, file, “”, 5 # read syscall 6, file # close produces: Errno::EFAULT: Bad address should be Errno::EBADF
  10. 10. posix semaphores process 1 process 2 require ‘fcntl’ Open, Wait, TryWait, Post = 268, 271, 272, 273 Open, Wait, Post, Close = 268, 271, 273, 269 s = syscall Open, “/tmp/s” s = syscall Open, “/tmp/s”, Fcntl::O_CREAT, 1911 begin syscall Wait, s t = Time.now puts “locked at #{Time.now}” syscall TryWait, s sleep 50 puts “locked at #{t}” puts “posted at #{Time.now}” rescue Exception => e syscall Post, s puts “busy at #{t}” syscall Close, s syscall Wait, s puts “waited #{Time.now - t} seconds” end produces: produces: locked at Thu May 28 01:03:23 +0100 2009 busy at Thu May 28 01:03:36 +0100 2009 posted at Thu May 28 01:04:13 +0100 2009 waited 47.056508 seconds
  11. 11. ruby/dl part of the standard library since ruby 1.8 access to dynamically loaded libraries .dll on windows .so and .dylib on unix supports C-style memory access and callback functions written in Ruby
  12. 12. memory management memory pointers encapsulated by DL::PtrData garbage collected uses free() or a custom deallocator handles realloc() automatically plays nicely with ruby strings and arrays [String | Array].to_ptr PtrData.struct! and PtrData.union!
  13. 13. require ‘dl’ file = open “test.dat”, 0x0209 CRT = DL.dlopen ‘libc.dylib’ write file, “textn” F = ‘syscall’ close file def open file, mode file = open “test.dat”, 0x0000 CRT[F, ‘IISI’].call(5, file, mode)[0] text = read file, 10 end close file def write fd, string, bytes = string.length CRT[F, ‘IIISI’].call(4, fd, string, bytes)[0] end def read fd, bytes = 1 buffer = DL.malloc(bytes) CRT[F, ‘IIIsI’].call(3, fd, buffer, bytes)[1][2] end def close fd CRT[F, ‘III’].call(6, fd)[0] end
  14. 14. further reading http://slides.games-with-brains.net http://www.jbrowse.com/text/rdl_en.html http://www.kegel.com/c10k.html http://www.ecst.csuchico.edu/~beej/guide/ipc/ http://beej.us/guide/bgnet/ http://wiki.netbsd.se/kqueue_tutorial

×