Prex Home / Browse Source - Prex Version: 0.9.0

root/usr/test/pipe/pipe.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. test1
  2. test2
  3. main

   1 /*
   2  * Copyright (c) 2008, Kohsuke Ohtani
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of any co-contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * pipe.c - test unix pipe
  32  */
  33 
  34 #include <sys/prex.h>
  35 #include <sys/fcntl.h>
  36 #include <sys/stat.h>
  37 #include <sys/errno.h>
  38 
  39 #include <stdio.h>
  40 #include <stdlib.h>
  41 #include <string.h>
  42 #include <unistd.h>
  43 
  44 static void
  45 test1(void)
  46 {
  47         int fd[2];
  48         char str[] = "test1";
  49         char buf[256];
  50 
  51         printf("pipe test program\n");
  52 
  53         if (pipe(fd) == -1) {
  54                 perror("pipe");
  55                 exit(1);
  56         }
  57         write(fd[1], str, sizeof(str));
  58         read(fd[0], buf, sizeof(buf));
  59         printf("str=%s\n", buf);
  60 }
  61 
  62 static void
  63 test2(void)
  64 {
  65         int fd[2];
  66         char str[] = "test2: hello!";
  67         char buf[256];
  68 
  69         printf("pipe test program\n");
  70 
  71         if (pipe(fd) == -1) {
  72                 perror("pipe");
  73                 exit(1);
  74         }
  75         switch(vfork()) {
  76         case -1:
  77                 perror("fork");
  78                 break;
  79 
  80         case 0: /* child */
  81                 close(fd[1]);
  82                 read(fd[0], buf, 256);
  83                 close(fd[0]);
  84                 printf("str=%s\n", buf);
  85                 exit(0);
  86                 break;
  87 
  88         default: /* parent */
  89                 close(fd[0]);
  90                 write(fd[1], str, sizeof(str));
  91                 close(fd[1]);
  92                 exit(0);
  93                 break;
  94         }
  95 }
  96 
  97 int
  98 main(int argc, char *argv[])
  99 {
 100         test1();
 101         test2();
 102         return 0;
 103 }

/* [<][>][^][v][top][bottom][index][help] */