lat_pipe.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * lat_pipe.c - pipe transaction test
  3. *
  4. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  5. * additional restriction that results may published only if
  6. * (1) the benchmark is unmodified, and
  7. * (2) the version in the sccsid below is included in the report.
  8. * Support for this development by Sun Microsystems is gratefully acknowledged.
  9. */
  10. char *id = "$Id$\n";
  11. #include "bench.h"
  12. void
  13. doit(int r, int w)
  14. {
  15. char c;
  16. if (write(w, &c, 1) != 1 || read(r, &c, 1) != 1) {
  17. perror("read/write on pipe");
  18. exit(1);
  19. }
  20. }
  21. int
  22. main()
  23. {
  24. int pid, p1[2], p2[2];
  25. char c;
  26. if (pipe(p1) == -1 || pipe(p2) == -1) {
  27. perror("pipe");
  28. exit(1);
  29. }
  30. pid = fork();
  31. if (pid == -1) {
  32. perror("fork");
  33. exit(1);
  34. }
  35. if (pid > 0) {
  36. /*
  37. * One time around to make sure both processes are started.
  38. */
  39. if (write(p1[1], &c, 1) != 1 || read(p2[0], &c, 1) != 1) {
  40. perror("read/write on pipe");
  41. exit(1);
  42. }
  43. BENCH(doit(p2[0], p1[1]), SHORT);
  44. micro("Pipe latency", get_n());
  45. kill(pid, 15);
  46. } else {
  47. for ( ;; ) {
  48. if (read(p1[0], &c, 1) != 1 ||
  49. write(p2[1], &c, 1) != 1) {
  50. perror("read/write on pipe");
  51. exit(1);
  52. }
  53. }
  54. }
  55. return (0);
  56. }