Skip to content
Snippets Groups Projects
usertests.c 910 B
Newer Older
rtm's avatar
rtm committed
// simple fork and pipe read/write

rtm's avatar
rtm committed
char buf[2048];
rtm's avatar
rtm committed

void
pipe1()
{
  int fds[2], pid;
rtm's avatar
rtm committed
  int seq = 0, i, n, cc, total;
rtm's avatar
rtm committed

  pipe(fds);
rtm's avatar
rtm committed
  pid = fork();
rtm's avatar
rtm committed
  if(pid == 0){
rtm's avatar
rtm committed
    close(fds[0]);
    for(n = 0; n < 5; n++){
      for(i = 0; i < 1033; i++)
        buf[i] = seq++;
      if(write(fds[1], buf, 1033) != 1033){
        puts("pipe1 oops 1\n");
        exit(1);
      }
    }
    exit(0);
rtm's avatar
rtm committed
  } else {
rtm's avatar
rtm committed
    close(fds[1]);
    total = 0;
    cc = 1;
    while(1){
      n = read(fds[0], buf, cc);
      if(n < 1)
        break;
      for(i = 0; i < n; i++){
        if((buf[i] & 0xff) != (seq++ & 0xff)){
          puts("pipe1 oops 2\n");
          return;
        }
      }
      total += n;
      cc = cc * 2;
      if(cc > sizeof(buf))
        cc = sizeof(buf);
rtm's avatar
rtm committed
    }
rtm's avatar
rtm committed
    if(total != 5 * 1033)
      puts("pipe1 oops 3\n");
    close(fds[0]);
rtm's avatar
rtm committed
  }
  puts("pipe1 ok\n");
}

main()
{
  pipe1();

  while(1)
    ;
}