View of xos/tests/pipe.c


XOS | Parent Directory | View | Download

/* Copyright (C) 2008  Emmanuel Varoquaux
 
   This file is part of XOS.
 
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
 
int main()
{
  int fd[2];
  char buf[4097];
  int n;
 
  /* lecture dans un tuyau vide */
  pipe(fd);
  if (!fork()) {
    close(fd[1]);
    printf("  Lecture dans un tuyau vide\n");
    n = read(fd[0], buf, 1);
    printf("  J'ai lu: %d\n", n);
    _exit(0);
  }
  close(fd[0]);
  sleep(1);
  printf("Ecriture\n");
  write(fd[1], buf, 1);
  sleep(1);
  printf("OK.\n\n");
 
  /* lecture dans un tuyau vide, pas de redacteur (1) */
  pipe(fd);
  if (!fork()) {
    close(fd[1]);
    sleep(1);
    printf("  Lecture dans un tuyau vide, pas de redacteur (1)\n");
    n = read(fd[0], buf, 1);
    printf("  J'ai lu: %d\n", n);
    _exit(0);
  }
  close(fd[0]);
  close(fd[1]);
  sleep(2);
  printf("OK.\n\n");
 
  /* lecture dans un tuyau vide, pas de redacteur (2) */
  pipe(fd);
  if (!fork()) {
    close(fd[1]);
    printf("  Lecture dans un tuyau vide, pas de redacteur (2)\n");
    n = read(fd[0], buf, 1);
    printf("  J'ai lu: %d\n", n);
    _exit(0);
  }
  close(fd[0]);
  sleep(1);
  close(fd[1]);
  sleep(1);
  printf("OK.\n\n");
 
  /* ecriture dans un tuyau plein */
  pipe(fd);
  if (!fork()) {
    close(fd[0]);
    printf("  Ecriture dans un tuyau plein\n");
    n = write(fd[1], buf, 4097);
    printf("  J'ai ecrit: %d\n", n);
    _exit(0);
  }
  close(fd[1]);
  sleep(1);
  printf("Lecture\n");
  read(fd[0], buf, 1);
  sleep(1);
  printf("OK.\n\n");
 
  /* ecriture dans un tuyau plein, pas de lecteur (1) */
  pipe(fd);
  if (!fork()) {
    close(fd[0]);
    sleep(1);
    printf("  Ecriture dans un tuyau plein, pas de lecteur (1)\n");
    n = write(fd[1], buf, 4097);
    printf("  J'ai ecrit: %d\n", n);
    _exit(0);
  }
  close(fd[1]);
  close(fd[0]);
  sleep(2);
  printf("OK.\n\n");
 
  /* ecriture dans un tuyau plein, pas de lecteur (2) */
  pipe(fd);
  if (!fork()) {
    close(fd[0]);
    printf("  Ecriture dans un tuyau plein, pas de lecteur (2)\n");
    n = write(fd[1], buf, 4097);
    printf("  J'ai ecrit: %d\n", n);
    _exit(0);
  }
  close(fd[1]);
  sleep(1);
  close(fd[0]);
  sleep(1);
  printf("OK.\n\n");
 
  return 0;
}