IPC via named pipe

Revision as of 13:01, 29 January 2013 by rosettacode>Markhobley (the task)

Named pipe, or FIFO, is a way of providing inter-process communications (IPC). The task is to demonstrate how it works, create two pipes, say, "in" and "out" (choose suitable names for your system), and write a program that works the two pipes such that:

  1. Data written to the "in" FIFO will be discarded except the byte count, which will be added to a total tally kept by the program;
  2. Whenever another process reads the "out" FIFO, it should receive the total count so far.
IPC via named pipe is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Possible issues:

  • Chances are you don't already have "in" and "out" pipes lying around. Create them within your program or without, at your discretion. You may assume they are already created for you.
  • Your program may assume it's the sole reader on "in" and the sole writer on "out".
  • Read/write operations on pipes are generally blocking. Make your program responsive to both pipes, so that it won't block trying to read the "in" pipe while leaving another process hanging on the other end of "out" pipe indefinitely -- or vice versa. You probably need to either poll the pipes or use multi-threading.
  • You may assume other processes using the pipes behave; specificially, your program may assume the process at the other end of a pipe will not unexpectedly break away before you finish reading or writing.

C

Library: pthread

<lang c>#include <stdio.h>

  1. include <sys/stat.h>
  2. include <unistd.h>
  3. include <fcntl.h>
  4. include <limits.h>
  5. include <pthread.h>

/* Flag for systems where a blocking open on a pipe will block

  entire process instead of just current thread.  Ideally this
  kind of flags should be automatically probed, but not before
  we are sure about how each OS behaves.  It can be set to 1
  even if not needed to, but that would force polling, which I'd
  rather not do.
    Linux:	won't block all threads (0)
    OpenBSD:	will block all (1)
  Other OSes: ?
  • /
  1. define WILL_BLOCK_EVERYTHING 0
  1. if WILL_BLOCK_EVERYTHING
  2. include <poll.h>
  3. endif

size_t tally = 0;

void* write_loop(void *a) { int fd; char buf[32]; while (1) {

  1. if WILL_BLOCK_EVERYTHING

/* try to open non-block. sleep and retry if no reader */ fd = open("out", O_WRONLY|O_NONBLOCK); if (fd < 0) { /* assume it's ENXIO, "no reader" */ usleep(200000); continue; }

  1. else

/* block open, until a reader comes along */ fd = open("out", O_WRONLY);

  1. endif

write(fd, buf, snprintf(buf, 32, "%d\n", tally)); close(fd);

/* Give the reader a chance to go away. We yeild, OS signals reader end of input, reader leaves. If a new reader comes along while we sleep, it will block wait. */ usleep(10000); } }

void read_loop() { int fd; size_t len; char buf[PIPE_BUF];

  1. if WILL_BLOCK_EVERYTHING

struct pollfd pfd; pfd.events = POLLIN;

  1. endif

while (1) {

  1. if WILL_BLOCK_EVERYTHING

fd = pfd.fd = open("in", O_RDONLY|O_NONBLOCK); fcntl(fd, F_SETFL, 0); /* disable O_NONBLOCK */ poll(&pfd, 1, INFTIM); /* poll to avoid reading EOF */

  1. else

fd = open("in", O_RDONLY);

  1. endif

while ((len = read(fd, buf, PIPE_BUF)) > 0) tally += len; close(fd); } }

int main() { pthread_t pid;

/* haphazardly create the fifos. It's ok if the fifos already exist, but things won't work out if the files exist but are not fifos; if we don't have write permission; if we are on NFS; etc. Just pretend it works. */ mkfifo("in", 0666); mkfifo("out", 0666);

/* because of blocking on open O_WRONLY, can't select */ pthread_create(&pid, 0, write_loop, 0); read_loop();

return 0; }</lang>

PicoLisp

<lang PicoLisp>(call 'mkfifo "in" "out") # Create pipes

(zero *Cnt) # Initialize byte counter

(unless (fork) # Handle "out" pipe

  (loop
     (out "out"
        (sync)
        (tell)
        (prinl *Cnt) ) ) )

(unless (fork) # Handle "in" pipe

  (let P (open "in")
     (loop
        (in P                         # Open twice, to avoid broken pipes
           (while (rd 1)                 # (works on Linux, perhaps not POSIX)
              (tell 'inc *Cnt) ) ) ) ) )

(push '*Bye '(call 'rm "in" "out")) # Remove pipes upon exit (wait) # (Terminate with Ctrl-C)</lang> Test:

$ line <out
0
$ echo abc >in
$ line <out
4
$ echo äöü >in
$ line <out
11

Ruby

This example is in need of improvement:
  • Find a way to report errors from inside open_sesame, such as Errno::ENOENT.
  • Check that open file is a FIFO: foopipe.stat.pipe?

With OpenBSD, we observe that open(2) a named pipe blocks all threads in a process. (This must be bug in thread library.) So, we fork(2) other process to call open(2), and apply UNIXSocket to send IO object.

Works with: Unix

<lang ruby>require 'socket'

  1. Ruby has no direct access to mkfifo(2). We use a shell script.

system '/bin/sh', '-c', <<EOF or abort test -p in || mkfifo in || exit test -p out || mkfifo out || exit EOF

  1. Forks a process to open _path_. Returns a _socket_ to receive the open
  2. IO object (by UNIXSocket#recv_io).

def open_sesame(path, mode)

 reader, writer = UNIXSocket.pair
 pid = fork do
   begin
     reader.close
     file = File.open(path, mode)
     writer.send_io file
   ensure
     exit!
   end
 end
 Process.detach pid
 writer.close
 return reader

end

insock = open_sesame("in", "rb") outsock = open_sesame("out", "w") inpipe, outpipe = nil count = 0 readers = [insock, outsock] writers = [] loop do

 selection = select(readers, writers)
 selection[0].each do |reader|
   case reader
   when insock
     inpipe = insock.recv_io
     puts "-- Opened 'in' pipe."
     insock.close
     readers.delete insock
     readers.push inpipe
   when outsock
     outpipe = outsock.recv_io
     puts "-- Opened 'out' pipe."
     outsock.close
     readers.delete outsock
     writers.push outpipe
   when inpipe
     count += (inpipe.read_nonblock(4096).size rescue 0)
   end
 end
 selection[1].each do |writer|
   case writer
   when outpipe
     outpipe.puts count
     puts "-- Counted #{count} bytes."
     exit
   end
 end

end</lang>

Example run:

$ ruby count.rb  
-- Opened 'in' pipe.
-- Opened 'out' pipe.
-- Counted 32 bytes.
$
$ echo 'This is line 1.' > in
$ echo 'This is line 2.' > in
$ cat out
32
$

Tcl

<lang tcl># Make the pipes by calling a subprocess... exec sh -c {test -p in || mkfifo in || exit 1;test -p out || exec mkfifo out}

  1. How many bytes have we seen so far?

set count 0

  1. Read side; uses standard fileevent mechanism (select() under the covers)

set in [open in {RDONLY NONBLOCK}] fconfigure $in -translation binary fileevent $in readable consume proc consume {} {

   global count in
   # Reads only 4kB at a time
   set data [read $in 4096]
   incr count [string length $data]

}

  1. Writer side; relies on open() throwing ENXIO on non-blocking open of write side

proc reportEveryHalfSecond {} {

   global count
   catch {

set out [open out {WRONLY NONBLOCK}] puts $out $count close $out

   }
   # Polling nastiness!
   after 500 reportEveryHalfSecond

} reportEveryHalfSecond

  1. Run the event loop until done

vwait forever</lang>