2
0

testshmproducer.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 void msgput(int boxnum, char *msg)
  29. {
  30. fspr_cpystrn(boxes[boxnum].msg, msg, strlen(msg) + 1);
  31. boxes[boxnum].msgavail = 1;
  32. }
  33. int main(void)
  34. {
  35. fspr_status_t rv;
  36. fspr_pool_t *pool;
  37. fspr_shm_t *shm;
  38. int i;
  39. int sent = 0;
  40. fspr_initialize();
  41. if (fspr_pool_create(&pool, NULL) != APR_SUCCESS) {
  42. exit(-1);
  43. }
  44. rv = fspr_shm_attach(&shm, SHARED_FILENAME, pool);
  45. if (rv != APR_SUCCESS) {
  46. exit(-2);
  47. }
  48. boxes = fspr_shm_baseaddr_get(shm);
  49. /* produce messages on all of the boxes, in descending order,
  50. * Yes, we could just return N_BOXES, but I want to have a double-check
  51. * in this code. The original code actually sent N_BOXES - 1 messages,
  52. * so rather than rely on possibly buggy code, this way we know that we
  53. * are returning the right number.
  54. */
  55. for (i = N_BOXES - 1, sent = 0; i >= 0; i--, sent++) {
  56. msgput(i, MSG);
  57. fspr_sleep(fspr_time_from_sec(1));
  58. }
  59. rv = fspr_shm_detach(shm);
  60. if (rv != APR_SUCCESS) {
  61. exit(-3);
  62. }
  63. return sent;
  64. }
  65. #else /* APR_HAS_SHARED_MEMORY */
  66. int main(void)
  67. {
  68. /* Just return, this program will never be launched, so there is no
  69. * reason to print a message.
  70. */
  71. return 0;
  72. }
  73. #endif /* APR_HAS_SHARED_MEMORY */