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

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

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

    1. An (abridged) Ruby Guide to *nix Plumbing The fun of a little knowledge applied unwisely http://slides.games-with-brains.net
    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. processes Kernel.system Kernel.spawn Kernel.fork Kernel.exec IO.popen
    4. files IO.for_fd IO.sys[open | read | write | seek] Kernel.select Fcntl
    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. interprocess communication IO.pipe sockets Signal
    7. accessing the *nix kernel Kernal.syscall ruby/dl
    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. IO.sysx the hard way On MacOS X file = syscall 5, “test.dat”, 1 # open O_WRONLY (fcntl.h) syscall 4, file, “some text\\n”, 10 # write syscall 3, file, “”, 5 # read syscall 6, file # close produces: Errno::EFAULT: Bad address should be Errno::EBADF
    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. 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. 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. require ‘dl’ file = open “test.dat”, 0x0209 CRT = DL.dlopen ‘libc.dylib’ write file, “text\\n” 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. 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

    + Eleanor McHughEleanor McHugh, 5 months ago

    custom

    464 views, 1 favs, 0 embeds more stats

    The Lightning-Talk version of A Ruby Plumber's Guid more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 464
      • 464 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 5
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories