home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / byteunix.lzh / byte.1 / context1.c < prev    next >
C/C++ Source or Header  |  1990-05-11  |  2KB  |  84 lines

  1.  
  2. /*******************************************************************************
  3.  *  The BYTE UNIX Benchmarks - Release 2
  4.  *          Module: context1.c   SID: 2.4 4/17/90 16:45:32
  5.  *          
  6.  *******************************************************************************
  7.  * Bug reports, patches, comments, suggestions should be sent to:
  8.  *
  9.  *    Ben Smith or Rick Grehan at BYTE Magazine
  10.  *    bensmith@bixpb.UUCP    rick_g@bixpb.UUCP
  11.  *
  12.  *******************************************************************************
  13.  *  Modification Log:
  14.  *  $Header: context1.c,v 3.4 87/06/22 14:22:59 kjmcdonell Beta $
  15.  *
  16.  ******************************************************************************/
  17. char SCCSid[] = "@(#) @(#)context1.c:2.4 -- 4/17/90 16:45:32";
  18. /*
  19.  *  Context switching via synchronized unbuffered pipe i/o
  20.  *
  21.  */
  22.  
  23. main(argc, argv)
  24. int    argc;
  25. char    *argv[];
  26. {
  27.     int    check;
  28.     int    iter;
  29.     int    p1[2], p2[2];
  30.  
  31.     if (argc != 2) {
  32.         printf("Usage: context count\n");
  33.         exit(1);
  34.     }
  35.  
  36.     iter = atoi(argv[1]);
  37.     if (pipe(p1) || pipe(p2)) {
  38.         perror("pipe create failed");
  39.         exit(1);
  40.     }
  41.  
  42.     if (fork()) {
  43.         /* master, write p1 & read p2 */
  44.         close(p1[0]); close(p2[1]);
  45.         while (iter > 0) {
  46.             if (write(p1[1], (char *)&iter, sizeof(iter)) != sizeof(iter)) {
  47.                 perror("master write failed");
  48.                 exit(1);
  49.             }
  50.             if (read(p2[0], (char *)&check, sizeof(check)) != sizeof(check)) {
  51.                 perror("master read failed");
  52.                 exit(1);
  53.             }
  54.             if (check != iter) {
  55.                 printf("Master sync error: expect %d, got %d\n",
  56.                     iter, check);
  57.                 exit(2);
  58.             }
  59.             iter--;
  60.         }
  61.     }
  62.     else {
  63.         /* slave, read p1 & write p2 */
  64.         close(p1[1]); close(p2[0]);
  65.         while (iter > 0) {
  66.             if (read(p1[0], (char *)&check, sizeof(check)) != sizeof(check)) {
  67.                 perror("slave read failed");
  68.                 exit(1);
  69.             }
  70.             if (check != iter) {
  71.                 printf("Slave sync error: expect %d, got %d\n",
  72.                     iter, check);
  73.                 exit(2);
  74.             }
  75.             if (write(p2[1], (char *)&iter, sizeof(iter)) != sizeof(check)) {
  76.                 perror("slave write failed");
  77.                 exit(1);
  78.             }
  79.             iter--;
  80.         }
  81.     }
  82.     exit(0);
  83. }
  84.