user/usertests.c
void
fourfiles(void)
{
  int fd, pid, i, j, n, total, pi;
  char *names[] = { "f0", "f1", "f2", "f3" };
  char *fname;
  enum { N=12, NCHILD=4, SZ=500 };
  
  printf("fourfiles test\n");

  for(pi = 0; pi < NCHILD; pi++){
    fname = names[pi];
    unlink(fname);

    pid = fork();
    if(pid < 0){
      printf("fork failed\n");
      exit(1);
    }

    if(pid == 0){
      fd = open(fname, O_CREATE | O_RDWR);
      if(fd < 0){
        printf("create failed\n");
        exit(1);
      }

      memset(buf, '0'+pi, SZ);
      for(i = 0; i < N; i++){
        if((n = write(fd, buf, SZ)) != SZ){
          printf("write failed %d\n", n);
          exit(1);
        }
      }
      exit(0);
    }
  }

  for(pi = 0; pi < NCHILD; pi++){
    wait(0);
  }

  for(i = 0; i < NCHILD; i++){
    fname = names[i];
    fd = open(fname, 0);
    total = 0;
    while((n = read(fd, buf, sizeof(buf))) > 0){
      for(j = 0; j < n; j++){
        if(buf[j] != '0'+i){
          printf("wrong char\n");
          exit(1);
        }
      }
      total += n;
    }
    close(fd);
    if(total != N*SZ){
      printf("wrong length %d\n", total);
      exit(1);
    }
    unlink(fname);
  }

  printf("fourfiles ok\n");
}
// four processes write different files at the same
// time, to test block allocation.
four processes write different files at the same
time, to test block allocation.
four processes write different files at the same time to test block allocation .
void
fourfiles(void)
{
  int fd, pid, i, j, n, total, pi;
  char *names[] = { "f0", "f1", "f2", "f3" };
  char *fname;
  enum { N=12, NCHILD=4, SZ=500 };
  printf("fourfiles test\n");
  for(pi = 0; pi < NCHILD; pi++){
    fname = names[pi];
    unlink(fname);
    pid = fork();
    if(pid < 0){
      printf("fork failed\n");
      exit(1);
    }
    if(pid == 0){
      fd = open(fname, O_CREATE | O_RDWR);
      if(fd < 0){
        printf("create failed\n");
        exit(1);
      }
      memset(buf, '0'+pi, SZ);
      for(i = 0; i < N; i++){
        if((n = write(fd, buf, SZ)) != SZ){
          printf("write failed %d\n", n);
          exit(1);
        }
      }
      exit(0);
    }
  }
  for(pi = 0; pi < NCHILD; pi++){
    wait(0);
  }
  for(i = 0; i < NCHILD; i++){
    fname = names[i];
    fd = open(fname, 0);
    total = 0;
    while((n = read(fd, buf, sizeof(buf))) > 0){
      for(j = 0; j < n; j++){
        if(buf[j] != '0'+i){
          printf("wrong char\n");
          exit(1);
        }
      }
      total += n;
    }
    close(fd);
    if(total != N*SZ){
      printf("wrong length %d\n", total);
      exit(1);
    }
    unlink(fname);
  }
  printf("fourfiles ok\n");
}
void fourfiles ( void ) { int fd , pid , i , j , n , total , pi ; char * names [ ] = { " " , " " , " " , " " } ; char * fname ; enum { N = 12 , NCHILD = 4 , SZ = 500 } ; printf ( " \n " ) ; for ( pi = 0 ; pi < NCHILD ; pi ++ ) { fname = names [ pi ] ; unlink ( fname ) ; pid = fork ( ) ; if ( pid < 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } if ( pid == 0 ) { fd = open ( fname , O_CREATE | O_RDWR ) ; if ( fd < 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } memset ( buf , ' ' + pi , SZ ) ; for ( i = 0 ; i < N ; i ++ ) { if ( ( n = write ( fd , buf , SZ ) ) != SZ ) { printf ( " \n " , n ) ; exit ( 1 ) ; } } exit ( 0 ) ; } } for ( pi = 0 ; pi < NCHILD ; pi ++ ) { wait ( 0 ) ; } for ( i = 0 ; i < NCHILD ; i ++ ) { fname = names [ i ] ; fd = open ( fname , 0 ) ; total = 0 ; while ( ( n = read ( fd , buf , sizeof ( buf ) ) ) > 0 ) { for ( j = 0 ; j < n ; j ++ ) { if ( buf [ j ] != ' ' + i ) { printf ( " \n " ) ; exit ( 1 ) ; } } total += n ; } close ( fd ) ; if ( total != N * SZ ) { printf ( " \n " , total ) ; exit ( 1 ) ; } unlink ( fname ) ; } printf ( " \n " ) ; }
four processes write different files at the same
time, to test block allocation.
four processes write different files at the same time to test block allocation .