home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / ddjcompr / compact / smtest.c < prev   
C/C++ Source or Header  |  1990-11-05  |  2KB  |  111 lines

  1. /**********************************************************************
  2.  *    ex:se ts=4 sw=4 ai:
  3.  *
  4.  *    Shared Memory Test Program
  5.  *
  6.  *    Author: Gene H. Olson
  7.  *        gene@zeno.mn.org
  8.  *
  9.  *    This is FREE software in the PUBLIC DOMAIN.
  10.  *
  11.  *    "Everything free contains no guarantee."
  12.  *
  13.  ***********************************************************************
  14.  *
  15.  *    This is a throwaway test program and example of how
  16.  *    to use the "share.c" library module.  It is not intended
  17.  *    to be a real program and certainly does not behave like one.
  18.  *
  19.  ***********************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <assert.h>
  23. #include <signal.h>
  24.  
  25. /* @(#)smtest.c    1.4 11/3/90 */
  26.  
  27. typedef char *opaque ;
  28.  
  29. extern opaque share_create() ;
  30. extern opaque share_open() ;
  31.  
  32. #define BS 8096
  33.  
  34. opaque rsh, wsh ;
  35.  
  36. quit(sig)
  37. {
  38.     if (wsh) share_close(wsh) ;
  39.     while (wait((int *)0) != -1) ;
  40.     exit(sig) ;
  41. }
  42.  
  43.  
  44.  
  45. main(argc, argv)
  46. int argc ;
  47. char **argv ;
  48. {
  49.     int s ;
  50.     int rcount, wcount ;
  51.     char *rbuf, *wbuf ;
  52.     int rfd[2], wfd[2] ;
  53.  
  54.     wsh = share_create(rfd, wfd, BS, 100) ;
  55.     assert(wsh) ;
  56.  
  57.     if (fork() == 0)
  58.     {
  59.         close(1) ;
  60.         close(wfd[0]) ;
  61.         close(wfd[1]) ;
  62.  
  63.         rsh = share_open(rfd) ;
  64.         assert(rsh) ;
  65.  
  66.         while (1)
  67.         {
  68.             s = share_get(rsh, &rbuf, &rcount) ;
  69.             if (s <= 0) break ;
  70.  
  71.             assert(rcount == 0) ;
  72.  
  73.             rcount = read(0, rbuf, BS) ;
  74.             if (rcount == 0) break ;
  75.  
  76.             s = share_put(rsh, rbuf, rcount) ;
  77.             if (s <= 0) break ;
  78.         }
  79.  
  80.         share_close(rsh) ;
  81.  
  82.         exit(s) ;
  83.     }
  84.     else
  85.     {
  86.         if (signal(SIGHUP, SIG_IGN) != SIG_IGN) signal(SIGHUP, quit) ;
  87.         if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, quit) ;
  88.         if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) signal(SIGQUIT, quit) ;
  89.         if (signal(SIGPIPE, SIG_IGN) != SIG_IGN) signal(SIGPIPE, quit) ;
  90.         if (signal(SIGTERM, SIG_IGN) != SIG_IGN) signal(SIGTERM, quit) ;
  91.  
  92.         close(0) ;
  93.         close(rfd[1]) ;
  94.  
  95.         wsh = share_open(wfd) ;
  96.         assert(wsh) ;
  97.  
  98.         while (1)
  99.         {
  100.             s = share_get(wsh, &wbuf, &wcount) ;
  101.             if (s <= 0) break ;
  102.  
  103.             (void) write(1, wbuf, wcount) ;
  104.  
  105.             (void) share_put(wsh, wbuf, 0) ;
  106.         }
  107.  
  108.         quit(s) ;
  109.     }
  110. }
  111.