user/usertests.c
void
pipe1(void)
{
  int fds[2], pid;
  int seq, i, n, cc, total;
  enum { N=5, SZ=1033 };
  
  if(pipe(fds) != 0){
    printf("pipe() failed\n");
    exit(1);
  }
  pid = fork();
  seq = 0;
  if(pid == 0){
    close(fds[0]);
    for(n = 0; n < N; n++){
      for(i = 0; i < SZ; i++)
        buf[i] = seq++;
      if(write(fds[1], buf, SZ) != SZ){
        printf("pipe1 oops 1\n");
        exit(1);
      }
    }
    exit(0);
  } else if(pid > 0){
    close(fds[1]);
    total = 0;
    cc = 1;
    while((n = read(fds[0], buf, cc)) > 0){
      for(i = 0; i < n; i++){
        if((buf[i] & 0xff) != (seq++ & 0xff)){
          printf("pipe1 oops 2\n");
          return;
        }
      }
      total += n;
      cc = cc * 2;
      if(cc > sizeof(buf))
        cc = sizeof(buf);
    }
    if(total != N * SZ){
      printf("pipe1 oops 3 total %d\n", total);
      exit(1);
    }
    close(fds[0]);
    wait(0);
  } else {
    printf("fork() failed\n");
    exit(1);
  }
  printf("pipe1 ok\n");
}
// simple fork and pipe read/write
simple fork and pipe read/write
simple fork and pipe read / write
void
pipe1(void)
{
  int fds[2], pid;
  int seq, i, n, cc, total;
  enum { N=5, SZ=1033 };
  if(pipe(fds) != 0){
    printf("pipe() failed\n");
    exit(1);
  }
  pid = fork();
  seq = 0;
  if(pid == 0){
    close(fds[0]);
    for(n = 0; n < N; n++){
      for(i = 0; i < SZ; i++)
        buf[i] = seq++;
      if(write(fds[1], buf, SZ) != SZ){
        printf("pipe1 oops 1\n");
        exit(1);
      }
    }
    exit(0);
  } else if(pid > 0){
    close(fds[1]);
    total = 0;
    cc = 1;
    while((n = read(fds[0], buf, cc)) > 0){
      for(i = 0; i < n; i++){
        if((buf[i] & 0xff) != (seq++ & 0xff)){
          printf("pipe1 oops 2\n");
          return;
        }
      }
      total += n;
      cc = cc * 2;
      if(cc > sizeof(buf))
        cc = sizeof(buf);
    }
    if(total != N * SZ){
      printf("pipe1 oops 3 total %d\n", total);
      exit(1);
    }
    close(fds[0]);
    wait(0);
  } else {
    printf("fork() failed\n");
    exit(1);
  }
  printf("pipe1 ok\n");
}
void pipe1 ( void ) { int fds [ 2 ] , pid ; int seq , i , n , cc , total ; enum { N = 5 , SZ = 1033 } ; if ( pipe ( fds ) != 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } pid = fork ( ) ; seq = 0 ; if ( pid == 0 ) { close ( fds [ 0 ] ) ; for ( n = 0 ; n < N ; n ++ ) { for ( i = 0 ; i < SZ ; i ++ ) buf [ i ] = seq ++ ; if ( write ( fds [ 1 ] , buf , SZ ) != SZ ) { printf ( " \n " ) ; exit ( 1 ) ; } } exit ( 0 ) ; } else if ( pid > 0 ) { close ( fds [ 1 ] ) ; total = 0 ; cc = 1 ; while ( ( n = read ( fds [ 0 ] , buf , cc ) ) > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( ( buf [ i ] & 0xff ) != ( seq ++ & 0xff ) ) { printf ( " \n " ) ; return ; } } total += n ; cc = cc * 2 ; if ( cc > sizeof ( buf ) ) cc = sizeof ( buf ) ; } if ( total != N * SZ ) { printf ( " \n " , total ) ; exit ( 1 ) ; } close ( fds [ 0 ] ) ; wait ( 0 ) ; } else { printf ( " \n " ) ; exit ( 1 ) ; } printf ( " \n " ) ; }
simple fork and pipe read/write
simple fork and pipe read / write