lat_fcntl.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "bench.h"
  2. /*
  3. * lat_pipe.c - pipe transaction test
  4. *
  5. * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
  6. * additional restriction that results may published only if
  7. * (1) the benchmark is unmodified, and
  8. * (2) the version in the sccsid below is included in the report.
  9. * Support for this development by Sun Microsystems is gratefully acknowledged.
  10. */
  11. char *id = "$Id: lat_pipe.c,v 1.8 1997/06/16 05:38:58 lm Exp $\n";
  12. #include "bench.h"
  13. struct flock lock, unlock;
  14. struct flock s1, s2;
  15. int fd1, fd2;
  16. /*
  17. * Create two files, use them as a ping pong test.
  18. * Process A:
  19. * lock(1)
  20. * unlock(2)
  21. * Process B:
  22. * unlock(1)
  23. * lock(2)
  24. * Initial state:
  25. * lock is locked
  26. * lock2 is locked
  27. */
  28. #define waiton(fd) fcntl(fd, F_SETLKW, &lock)
  29. #define release(fd) fcntl(fd, F_SETLK, &unlock)
  30. void
  31. procA()
  32. {
  33. if (waiton(fd1) == -1) {
  34. perror("lock of fd1 failed\n");
  35. exit(1);
  36. }
  37. if (release(fd2) == -1) {
  38. perror("unlock of fd2 failed\n");
  39. exit(1);
  40. }
  41. if (waiton(fd2) == -1) {
  42. perror("lock of fd2 failed\n");
  43. exit(1);
  44. }
  45. if (release(fd1) == -1) {
  46. perror("unlock of fd1 failed\n");
  47. exit(1);
  48. }
  49. }
  50. void
  51. procB()
  52. {
  53. if (release(fd1) == -1) {
  54. perror("unlock of fd1 failed\n");
  55. exit(1);
  56. }
  57. if (waiton(fd2) == -1) {
  58. perror("lock of fd2 failed\n");
  59. exit(1);
  60. }
  61. if (release(fd2) == -1) {
  62. perror("unlock of fd2 failed\n");
  63. exit(1);
  64. }
  65. if (waiton(fd1) == -1) {
  66. perror("lock of fd1 failed\n");
  67. exit(1);
  68. }
  69. }
  70. int
  71. main()
  72. {
  73. char buf[10000];
  74. int pid;
  75. unlink("/tmp/lmbench-fcntl.1");
  76. unlink("/tmp/lmbench-fcntl.2");
  77. if ((fd1 = open("/tmp/lmbench-fcntl.1", O_CREAT|O_RDWR, 0666)) == -1) {
  78. perror("create");
  79. exit(1);
  80. }
  81. if ((fd2 = open("/tmp/lmbench-fcntl.2", O_CREAT|O_RDWR, 0666)) == -1) {
  82. perror("create");
  83. exit(1);
  84. }
  85. unlink("/tmp/lmbench-fcntl.1");
  86. unlink("/tmp/lmbench-fcntl.2");
  87. write(fd1, buf, sizeof(buf));
  88. write(fd2, buf, sizeof(buf));
  89. lock.l_type = F_WRLCK;
  90. lock.l_whence = 0;
  91. lock.l_start = 0;
  92. lock.l_len = 1;
  93. unlock = lock;
  94. unlock.l_type = F_UNLCK;
  95. if (waiton(fd1) == -1) {
  96. perror("lock1");
  97. exit(1);
  98. }
  99. if (waiton(fd2) == -1) {
  100. perror("lock2");
  101. exit(1);
  102. }
  103. switch (pid = fork()) {
  104. case -1:
  105. perror("fork");
  106. exit(1);
  107. case 0:
  108. for ( ;; ) {
  109. procB();
  110. }
  111. exit(0);
  112. default:
  113. break;
  114. }
  115. BENCH(procA(), SHORT);
  116. micro("Fcntl lock latency", 2 * get_n());
  117. kill(pid, 15);
  118. return (0);
  119. }