Annotation of prex/usr/test/pipe/pipe.c, Revision 1.1
1.1 ! nbrk 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 <prex/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:
! 45: static void
! 46: test1(void)
! 47: {
! 48: int fd[2];
! 49: char str[] = "test1";
! 50: char buf[256];
! 51:
! 52: printf("pipe test program\n");
! 53:
! 54: if (pipe(fd) == -1) {
! 55: perror("pipe");
! 56: exit(1);
! 57: }
! 58: write(fd[1], str, sizeof(str));
! 59: read(fd[0], buf, sizeof(buf));
! 60: printf("str=%s\n", buf);
! 61: }
! 62:
! 63: static void
! 64: test2(void)
! 65: {
! 66: int fd[2];
! 67: char str[] = "test2: hello!";
! 68: char buf[256];
! 69:
! 70: printf("pipe test program\n");
! 71:
! 72: if (pipe(fd) == -1) {
! 73: perror("pipe");
! 74: exit(1);
! 75: }
! 76: switch(vfork()) {
! 77: case -1:
! 78: perror("fork");
! 79: break;
! 80:
! 81: case 0: /* child */
! 82: close(fd[1]);
! 83: read(fd[0], buf, 256);
! 84: close(fd[0]);
! 85: printf("str=%s\n", buf);
! 86: exit(0);
! 87: break;
! 88:
! 89: default: /* parent */
! 90: close(fd[0]);
! 91: write(fd[1], str, sizeof(str));
! 92: close(fd[1]);
! 93: exit(0);
! 94: break;
! 95: }
! 96: }
! 97:
! 98: int
! 99: main(int argc, char *argv[])
! 100: {
! 101: test2();
! 102: return 0;
! 103: }
CVSweb