lat_fifo.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * lat_fifo.c - named 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. */
  9. char *id = "$Id$\n";
  10. #include "bench.h"
  11. #define F1 "/tmp/lmbench_f1"
  12. #define F2 "/tmp/lmbench_f2"
  13. void
  14. doit(int r, int w)
  15. {
  16. char c;
  17. if (write(w, &c, 1) != 1 || read(r, &c, 1) != 1) {
  18. perror("read/write on pipe");
  19. exit(1);
  20. }
  21. }
  22. int
  23. main()
  24. {
  25. int pid, rd, wr;
  26. char c;
  27. unlink(F1); unlink(F2);
  28. if (mknod(F1, S_IFIFO|0664, 0) || mknod(F2, S_IFIFO|0664, 0)) {
  29. perror("mknod");
  30. exit(1);
  31. }
  32. switch (pid = fork()) {
  33. case -1:
  34. perror("fork");
  35. exit(1);
  36. case 0:
  37. wr = open(F1, O_WRONLY);
  38. rd = open(F2, O_RDONLY);
  39. for ( ;; ) {
  40. if (read(rd, &c, 1) != 1 ||
  41. write(wr, &c, 1) != 1) {
  42. perror("read/write on FIFO");
  43. exit(1);
  44. }
  45. }
  46. exit(1);
  47. default:
  48. break;
  49. }
  50. rd = open(F1, O_RDONLY);
  51. wr = open(F2, O_WRONLY);
  52. /*
  53. * One time around to make sure both processes are started.
  54. */
  55. if (write(wr, &c, 1) != 1 || read(rd, &c, 1) != 1) {
  56. perror("read/write on FIFO");
  57. exit(1);
  58. }
  59. BENCH(doit(rd, wr), SHORT);
  60. micro("FIFO latency", get_n());
  61. kill(pid, 15);
  62. return (0);
  63. }