user/usertests.c
void
createdelete(void)
{
  enum { N = 20, NCHILD=4 };
  int pid, i, fd, pi;
  char name[32];

  printf("createdelete test\n");

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

    if(pid == 0){
      name[0] = 'p' + pi;
      name[2] = '\0';
      for(i = 0; i < N; i++){
        name[1] = '0' + i;
        fd = open(name, O_CREATE | O_RDWR);
        if(fd < 0){
          printf("create failed\n");
          exit(1);
        }
        close(fd);
        if(i > 0 && (i % 2 ) == 0){
          name[1] = '0' + (i / 2);
          if(unlink(name) < 0){
            printf("unlink failed\n");
            exit(1);
          }
        }
      }
      exit(0);
    }
  }

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

  name[0] = name[1] = name[2] = 0;
  for(i = 0; i < N; i++){
    for(pi = 0; pi < NCHILD; pi++){
      name[0] = 'p' + pi;
      name[1] = '0' + i;
      fd = open(name, 0);
      if((i == 0 || i >= N/2) && fd < 0){
        printf("oops createdelete %s didn't exist\n", name);
        exit(1);
      } else if((i >= 1 && i < N/2) && fd >= 0){
        printf("oops createdelete %s did exist\n", name);
        exit(1);
      }
      if(fd >= 0)
        close(fd);
    }
  }

  for(i = 0; i < N; i++){
    for(pi = 0; pi < NCHILD; pi++){
      name[0] = 'p' + i;
      name[1] = '0' + i;
      unlink(name);
    }
  }

  printf("createdelete ok\n");
}
// four processes create and delete different files in same directory
four processes create and delete different files in same directory
four processes create and delete different files in same directory
void
createdelete(void)
{
  enum { N = 20, NCHILD=4 };
  int pid, i, fd, pi;
  char name[32];
  printf("createdelete test\n");
  for(pi = 0; pi < NCHILD; pi++){
    pid = fork();
    if(pid < 0){
      printf("fork failed\n");
      exit(1);
    }
    if(pid == 0){
      name[0] = 'p' + pi;
      name[2] = '\0';
      for(i = 0; i < N; i++){
        name[1] = '0' + i;
        fd = open(name, O_CREATE | O_RDWR);
        if(fd < 0){
          printf("create failed\n");
          exit(1);
        }
        close(fd);
        if(i > 0 && (i % 2 ) == 0){
          name[1] = '0' + (i / 2);
          if(unlink(name) < 0){
            printf("unlink failed\n");
            exit(1);
          }
        }
      }
      exit(0);
    }
  }
  for(pi = 0; pi < NCHILD; pi++){
    wait(0);
  }
  name[0] = name[1] = name[2] = 0;
  for(i = 0; i < N; i++){
    for(pi = 0; pi < NCHILD; pi++){
      name[0] = 'p' + pi;
      name[1] = '0' + i;
      fd = open(name, 0);
      if((i == 0 || i >= N/2) && fd < 0){
        printf("oops createdelete %s didn't exist\n", name);
        exit(1);
      } else if((i >= 1 && i < N/2) && fd >= 0){
        printf("oops createdelete %s did exist\n", name);
        exit(1);
      }
      if(fd >= 0)
        close(fd);
    }
  }
  for(i = 0; i < N; i++){
    for(pi = 0; pi < NCHILD; pi++){
      name[0] = 'p' + i;
      name[1] = '0' + i;
      unlink(name);
    }
  }
  printf("createdelete ok\n");
}
void createdelete ( void ) { enum { N = 20 , NCHILD = 4 } ; int pid , i , fd , pi ; char name [ 32 ] ; printf ( " \n " ) ; for ( pi = 0 ; pi < NCHILD ; pi ++ ) { pid = fork ( ) ; if ( pid < 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } if ( pid == 0 ) { name [ 0 ] = ' ' + pi ; name [ 2 ] = ' \0 ' ; for ( i = 0 ; i < N ; i ++ ) { name [ 1 ] = ' ' + i ; fd = open ( name , O_CREATE | O_RDWR ) ; if ( fd < 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } close ( fd ) ; if ( i > 0 && ( i % 2 ) == 0 ) { name [ 1 ] = ' ' + ( i / 2 ) ; if ( unlink ( name ) < 0 ) { printf ( " \n " ) ; exit ( 1 ) ; } } } exit ( 0 ) ; } } for ( pi = 0 ; pi < NCHILD ; pi ++ ) { wait ( 0 ) ; } name [ 0 ] = name [ 1 ] = name [ 2 ] = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( pi = 0 ; pi < NCHILD ; pi ++ ) { name [ 0 ] = ' ' + pi ; name [ 1 ] = ' ' + i ; fd = open ( name , 0 ) ; if ( ( i == 0 || i >= N / 2 ) && fd < 0 ) { printf ( " \n " , name ) ; exit ( 1 ) ; } else if ( ( i >= 1 && i < N / 2 ) && fd >= 0 ) { printf ( " \n " , name ) ; exit ( 1 ) ; } if ( fd >= 0 ) close ( fd ) ; } } for ( i = 0 ; i < N ; i ++ ) { for ( pi = 0 ; pi < NCHILD ; pi ++ ) { name [ 0 ] = ' ' + i ; name [ 1 ] = ' ' + i ; unlink ( name ) ; } } printf ( " \n " ) ; }
four processes create and delete different files in same directory
four processes create and delete different files in same directory