2
0

testshmconsumer.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_shm.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "fspr_strings.h"
  21. #include "fspr_time.h"
  22. #include "testshm.h"
  23. #include "fspr.h"
  24. #if APR_HAVE_STDLIB_H
  25. #include <stdlib.h>
  26. #endif
  27. #if APR_HAS_SHARED_MEMORY
  28. static int msgwait(int sleep_sec, int first_box, int last_box)
  29. {
  30. int i;
  31. int recvd = 0;
  32. fspr_time_t start = fspr_time_now();
  33. fspr_interval_time_t sleep_duration = fspr_time_from_sec(sleep_sec);
  34. while (fspr_time_now() - start < sleep_duration) {
  35. for (i = first_box; i < last_box; i++) {
  36. if (boxes[i].msgavail && !strcmp(boxes[i].msg, MSG)) {
  37. recvd++;
  38. boxes[i].msgavail = 0; /* reset back to 0 */
  39. memset(boxes[i].msg, 0, 1024);
  40. }
  41. }
  42. fspr_sleep(fspr_time_from_sec(1));
  43. }
  44. return recvd;
  45. }
  46. int main(void)
  47. {
  48. fspr_status_t rv;
  49. fspr_pool_t *pool;
  50. fspr_shm_t *shm;
  51. int recvd;
  52. fspr_initialize();
  53. if (fspr_pool_create(&pool, NULL) != APR_SUCCESS) {
  54. exit(-1);
  55. }
  56. rv = fspr_shm_attach(&shm, SHARED_FILENAME, pool);
  57. if (rv != APR_SUCCESS) {
  58. exit(-2);
  59. }
  60. boxes = fspr_shm_baseaddr_get(shm);
  61. /* consume messages on all of the boxes */
  62. recvd = msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
  63. rv = fspr_shm_detach(shm);
  64. if (rv != APR_SUCCESS) {
  65. exit(-3);
  66. }
  67. return recvd;
  68. }
  69. #else /* APR_HAS_SHARED_MEMORY */
  70. int main(void)
  71. {
  72. /* Just return, this program will never be called, so we don't need
  73. * to print a message
  74. */
  75. return 0;
  76. }
  77. #endif /* APR_HAS_SHARED_MEMORY */