conc-io.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Tim Brecht:
  2. * Added to uw-testbin : Sat 5 Jan 2013 14:36:26 EST
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. /*
  8. * This tests concurrent access to a file.
  9. */
  10. #define PROCS (4)
  11. #define BUF_SIZE (10)
  12. #define NUM_WRITES (500)
  13. #define TOTAL_WRITES (NUM_WRITES * PROCS)
  14. char const *to_write = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  15. static void do_writes(char c);
  16. int
  17. main()
  18. {
  19. char buffer[BUF_SIZE];
  20. int status = -1;
  21. int rc[PROCS];
  22. pid_t pid[PROCS];
  23. int rval = -1;
  24. int id = -1;
  25. int i,k;
  26. rval = open("TESTFILE", O_RDWR | O_CREAT | O_TRUNC);
  27. if (rval < 0)
  28. {
  29. printf("### TEST FAILED: Unable to create file\n");
  30. exit(0);
  31. }
  32. close(rval);
  33. /* do concurrent writes */
  34. for (i=0; i<PROCS; i++) {
  35. pid[i] = fork();
  36. if (pid[i] < 0) {
  37. printf("Fork %d failed\n", i);
  38. exit(0);
  39. }
  40. /* This is the child have it do something */
  41. if (pid[i] == 0) {
  42. do_writes(to_write[i]);
  43. printf("Process number %d is done\n", i);
  44. exit(0);
  45. }
  46. }
  47. /* Check that all processes were created */
  48. for (i=0; i<PROCS; i++) {
  49. if (pid[i] < 0) {
  50. printf("### TEST FAILED: Unable to create processes\n");
  51. }
  52. }
  53. /* Wait for all processes to finish */
  54. for (i=0; i<PROCS; i++) {
  55. rc[i] = waitpid(pid[i], &status, 0);
  56. if (rc[i] != pid[i]) {
  57. printf("### TEST FAILED: wait for processes failed\n");
  58. }
  59. printf("Done waiting for process number %d\n", i);
  60. }
  61. /* Now check that the file contains the proper contents */
  62. id = open("TESTFILE", O_RDWR);
  63. if (id < 0)
  64. {
  65. printf("### TEST FAILED: Unable to open file\n");
  66. exit(1);
  67. }
  68. /* verify writes were atomic */
  69. for (i=0; i<TOTAL_WRITES; i++)
  70. {
  71. rval = read(id, buffer, BUF_SIZE);
  72. for (k=0; k<(BUF_SIZE-1); k++) {
  73. if (buffer[k] != buffer[k+1]) {
  74. printf("### TEST FAILED; Writes were not atomic\n");
  75. printf("buffer[%d] = %c != buffer[%d] = %c\n",
  76. k, buffer[k], k+1, buffer[k+1]);
  77. close(id);
  78. exit(1);
  79. }
  80. }
  81. }
  82. rval = close(id);
  83. if (rval < 0)
  84. {
  85. printf("### TEST FAILED: Unable to close file\n");
  86. } else {
  87. printf("PASSED\n");
  88. }
  89. exit(0);
  90. }
  91. void
  92. do_writes(char c)
  93. {
  94. int rval = 0;
  95. int id = -1;
  96. int i = 0;
  97. int j = 0;
  98. char buffer[BUF_SIZE];
  99. int volatile total = 0;
  100. for (j=0; j<BUF_SIZE; j++) {
  101. buffer[j] = c;
  102. }
  103. id = open("TESTFILE", O_RDWR);
  104. if (id < 0) {
  105. printf("### TEST FAILED: Unable to open file\n");
  106. _exit(1);
  107. }
  108. for (i = 0; i < NUM_WRITES; i++) {
  109. rval = write(id, buffer, BUF_SIZE);
  110. if (rval != BUF_SIZE) {
  111. printf("### TEST FAILED: Unable to write %d bytes to file\n", BUF_SIZE);
  112. close(id);
  113. _exit(1);
  114. }
  115. /* Do something else to try to give other processes a change to run */
  116. for (j=0; j<BUF_SIZE; j++) {
  117. buffer[j] = c;
  118. total += j;
  119. }
  120. }
  121. rval = close(id);
  122. if (rval < 0) {
  123. printf("### TEST FAILED: Unable to close file\n");
  124. _exit(1);
  125. }
  126. }