user/usertests.c
void
sharedfd(void)
{
  int fd, pid, i, n, nc, np;
  enum { N = 1000, SZ=10};
  char buf[SZ];

  printf("sharedfd test\n");

  unlink("sharedfd");
  fd = open("sharedfd", O_CREATE|O_RDWR);
  if(fd < 0){
    printf("fstests: cannot open sharedfd for writing");
    return;
  }
  pid = fork();
  memset(buf, pid==0?'c':'p', sizeof(buf));
  for(i = 0; i < N; i++){
    if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
      printf("fstests: write sharedfd failed\n");
      break;
    }
  }
  if(pid == 0)
    exit(0);
  else
    wait(0);
  close(fd);
  fd = open("sharedfd", 0);
  if(fd < 0){
    printf("fstests: cannot open sharedfd for reading\n");
    return;
  }
  nc = np = 0;
  while((n = read(fd, buf, sizeof(buf))) > 0){
    for(i = 0; i < sizeof(buf); i++){
      if(buf[i] == 'c')
        nc++;
      if(buf[i] == 'p')
        np++;
    }
  }
  close(fd);
  unlink("sharedfd");
  if(nc == N*SZ && np == N*SZ){
    printf("sharedfd ok\n");
  } else {
    printf("sharedfd oops %d %d\n", nc, np);
    exit(1);
  }
}
// two processes write to the same file descriptor
// is the offset shared? does inode locking work?
two processes write to the same file descriptor
is the offset shared.
two processes write to the same file descriptor is the offset shared .
void
sharedfd(void)
{
  int fd, pid, i, n, nc, np;
  enum { N = 1000, SZ=10};
  char buf[SZ];
  printf("sharedfd test\n");
  unlink("sharedfd");
  fd = open("sharedfd", O_CREATE|O_RDWR);
  if(fd < 0){
    printf("fstests: cannot open sharedfd for writing");
    return;
  }
  pid = fork();
  memset(buf, pid==0?'c':'p', sizeof(buf));
  for(i = 0; i < N; i++){
    if(write(fd, buf, sizeof(buf)) != sizeof(buf)){
      printf("fstests: write sharedfd failed\n");
      break;
    }
  }
  if(pid == 0)
    exit(0);
  else
    wait(0);
  close(fd);
  fd = open("sharedfd", 0);
  if(fd < 0){
    printf("fstests: cannot open sharedfd for reading\n");
    return;
  }
  nc = np = 0;
  while((n = read(fd, buf, sizeof(buf))) > 0){
    for(i = 0; i < sizeof(buf); i++){
      if(buf[i] == 'c')
        nc++;
      if(buf[i] == 'p')
        np++;
    }
  }
  close(fd);
  unlink("sharedfd");
  if(nc == N*SZ && np == N*SZ){
    printf("sharedfd ok\n");
  } else {
    printf("sharedfd oops %d %d\n", nc, np);
    exit(1);
  }
}
void sharedfd ( void ) { int fd , pid , i , n , nc , np ; enum { N = 1000 , SZ = 10 } ; char buf [ SZ ] ; printf ( " \n " ) ; unlink ( " " ) ; fd = open ( " " , O_CREATE | O_RDWR ) ; if ( fd < 0 ) { printf ( " " ) ; return ; } pid = fork ( ) ; memset ( buf , pid == 0 ? ' ' : ' ' , sizeof ( buf ) ) ; for ( i = 0 ; i < N ; i ++ ) { if ( write ( fd , buf , sizeof ( buf ) ) != sizeof ( buf ) ) { printf ( " \n " ) ; break ; } } if ( pid == 0 ) exit ( 0 ) ; else wait ( 0 ) ; close ( fd ) ; fd = open ( " " , 0 ) ; if ( fd < 0 ) { printf ( " \n " ) ; return ; } nc = np = 0 ; while ( ( n = read ( fd , buf , sizeof ( buf ) ) ) > 0 ) { for ( i = 0 ; i < sizeof ( buf ) ; i ++ ) { if ( buf [ i ] == ' ' ) nc ++ ; if ( buf [ i ] == ' ' ) np ++ ; } } close ( fd ) ; unlink ( " " ) ; if ( nc == N * SZ && np == N * SZ ) { printf ( " \n " ) ; } else { printf ( " \n " , nc , np ) ; exit ( 1 ) ; } }
two processes write to the same file descriptor
is the offset shared?
two processes write to the same file descriptor is the offset shared?