Advanced Programming in the
UNIX Environment

by W. Richard Stevens
Addison-Wesley Professional Computing Series
0-201-56317-7 * Hardcover * 768 pages * ©1992
[Buy this book]

Chapter 15. Advanced Interprocess Communication

Introduction * Stream Pipes * Passing File Descriptors * System V Release 4 * 4.3BSD * 4.3+BSD * An Open Server, Version 1 * Client-Server Connection Functions * System V Release 4 * 4.3+BSD * An Open Server, Version 2 * Summary

Example Relevant Functions
15.1 A program to drive the add2 filter (14.8)
(But this time: using a stream pipe)
signal, s_pipe, fork,
close, fgets, fputs, strlen,
write, read,
err_msg, err_sys, err_quit (Stevens' lib),
ferror,
dup2, printf, execl,
15.2 s_pipe: Create a full-duplex pipe
SVR4 Version (Using pipe(2))
pipe
15.3 s_pipe: Create a full-duplex pipe
SunOs/BSD version (Using socketpair(3N))
socketpair
15.4 send_err:
Send an error message across a stream pipe
followed by a status byte
strlen,
writen (Stevens' lib),
send_fd (Stevens' lib)
15.5 send_fd (SVR4 version) :
Send a file-descriptor via a pipe.
write, ioctl
15.6 recv_fd (SVR4 version) :
Receive a file descriptor from another process
via a pipe. To work with send_fd
getmsg,
err_ret (Stevens' lib),
err_dump (Stevens' lib),
err_sys (Stevens' lib),
ioctl
15.7 send_fd (4.3BSD/SunOs Version) :
Send a file-descriptor via a pipe
sendmsg
15.8 recv_fd (4.3BSD/SunOs version) :
Receive a file descriptor from another process
via a pipe. To work with send_fd.
recvmsg,
err_ret (Stevens' lib),
err_dump (Stevens' lib),
err_sys (Stevens' lib)
15.9 send_fd (4.3BSD Reno/4.4BSD Version) :
Send a file-descriptor via a pipe.
malloc, sendmsg
15.10 recv_fd (4.3BSD Reno/4.4BSD version) :
Receive a file descriptor from another process
via a pipe. To work with send_fd.
malloc, recvmsg,
err_ret (Stevens' lib),
err_dump (Stevens' lib),
err_sys (Stevens' lib)
15.11 The open.h header:
Interface to a general Client/Server remote file open
csopen: Client/Server open
(Stevens' library function)
15.12 Remote get file: a client program that:
  • Reads file names from stdin,
  • Gets them from the remote server, and
  • Prints them to stdout.
  • fgets, read, write, close,
    csopen, err_sys (Stevens' lib)
    15.13 csopen:
    Client/Server open (Stevens' library function)
    Client: open a remote file by sending the server
    a filename and oflag.
    Then, reading back an (open) file descriptor.
    s_pipe, err_sys (Stevens' lib),
    fork, close, dup2, execl,
    sprintf, writev
    15.14 The opend.h header file
    Client/Server indirect open file.
    Server side types/prototypes declarations
    Functions appear below
    15.15 main server side function
    Reads client requests from stream pipe (stdin)
    and calls the request function to serve it
    read, exit,
    err_sys (Stevens' lib),
    request (Stevens' func)
    15.16 request
    The routine that does all the real work:
    Parses client arguments (the request)
    Opens the file, Sends descriptor back.
    Sends back errors if there are any problems.
    buf_args (Stevens' func),
    cli_args (Stevens' func),
    send_err (Stevens' func),
    15.17 The buf_args function
    Parse client request arguments, set options.
    strtok,
    cli_args (Stevens' func. Indirect call)
    15.18 The cli_args function
    Process client request arguments
    that were prepared by buf_args:
    Set the options.
    strcmp, strcpy, atoi
    15.19 serv_listen
    SVR4 version
    Unrelated Client/Server (e.g. no parent/child)
    Server: create a named end connection for a service.
    unlink, creat, close,
    pipe, ioctl, fattach
    15.20 serv_accept
    SVR4 version
    Unrelated Client/Server (e.g. no parent/child)
    Server: wait for a client connection to arrive
    and accept it. Also obtain client's user id.
    ioctl
    15.21 cli_conn
    SVR4 version
    Unrelated Client/Server (e.g. no parent/child)
    Client: create client endpoint + connect to server.
    open, isastream
    15.22 serv_listen
    4.3BSD/SunOs version
    (use Unix domain sockets instead of streams)
    Unrelated Client/Server (e.g. no parent/child)
    Server: create a named end connection for a service.
    socket, unlink,
    memset strcpy, strlen,
    bind, listen
    15.23 cli_conn
    4.3BSD/SunOs version
    Unrelated Client/Server (e.g. no parent/child)
    Client: create client endpoint + connect to server.
    socket, memset, sprintf,
    getpid, strlen,
    err_quit (Stevens' lib),
    unlink, bind,
    chmod strcpy, connect,
    15.24 serv_accept
    4.3BSD/SunOs version
    Unrelated Client/Server (e.g. no parent/child)
    Server: wait for a client connection to arrive
    and accept it. Also obtain client's user id.
    accept, stat, time, unlink
    15.25 csopen:
    Revisit: Client/Server remote file open.
    (A more efficient implementation.)
    One server handles multiple clients (save fork/exec).
    cli_conn (Stevens' func.),
    err_sys (Stevens' lib),
    sprintf, strlen,
    writev recv_fd
    15.26 The opend.h header file
    Server side types/prototypes declarations
    (for the more efficient implementation.)
    cli_args (Stevens' func.)
    client_add (Stevens' func.),
    client_del (Stevens' func.),
    loop (Stevens' func.),
    request
    15.27 client.c:
    functions to manipulate client array in Server
    client_add, client_del
    (for the more efficient implementation.)
    malloc, realloc,
    err_sys (Stevens' lib),
    15.28 Main server function
    Server main (for more efficient implementation.)
    Loop on clients' requests and serve them
    -d option for debugging (running interactively
    rather than as a daemon.)
    getopt, err_quit (Stevens' lib),
    daemon_init (see 13.1), loop
    15.29 loop (Version 1):
    The Server loop function
    Version 1: using select
    (Should work on every modern UNIX)
    select, read, close,
    serv_listen (Stevens' func),
    serv_accept (Stevens' func),
    log_sys (Stevens' lib),
    log_msg (Stevens' lib),
    client_add (Stevens' func),
    client_del (Stevens' func),
    request (Stevens' func)
    15.30 loop (Version 2):
    The Server loop function
      Version 2: using poll
      POLLIN indicating new client request
      POLLHUP indicating client termination
      (SVR4 only)
    read, malloc, close,
    err_sys (Stevens' lib),
    log_sys (Stevens' lib),
    log_msg (Stevens' lib),
    serv_listen (Stevens' func),
    serv_accept (Stevens' func),
    client_add (Stevens' func),
    client_del (Stevens' func),
    request (Stevens' func) poll
    15.31 request:
    Server side handle request.
    (for both 4.3BSD/SunOs or SVR4 versions)
    open, sprintf,
    log_sys (Stevens' lib),
    log_msg (Stevens' lib),
    buf_args (Stevens' func),
    cli_args (Stevens' func.)
    send_err (Stevens' func),
    send_fd (Stevens' lib),
    close,

    [Buy this book]