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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "apr_errno.h"
19 #include "apr_general.h"
21 #include "apr_strings.h"
30 #if APR_HAS_SHARED_MEMORY
39 #define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
40 #define SHARED_FILENAME "/tmp/apr.testshm.shm"
42 static void msgwait(int sleep_sec, int first_box, int last_box)
45 apr_time_t start = apr_time_now();
46 apr_interval_time_t sleep_duration = apr_time_from_sec(sleep_sec);
47 while (apr_time_now() - start < sleep_duration) {
48 for (i = first_box; i < last_box; i++) {
49 if (boxes[i].msgavail) {
50 fprintf(stdout, "Consumer: received a message in box %d, message was: %s\n",
52 boxes[i].msgavail = 0; /* reset back to 0 */
55 apr_sleep(apr_time_from_sec(1));
57 fprintf(stdout, "Consumer: done waiting on mailboxes...\n");
69 printf("APR Shared Memory Test: CONSUMER\n");
71 printf("Initializing the pool............................");
72 if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
73 printf("could not initialize pool\n");
78 printf("Consumer attaching to name-based shared memory....");
79 rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
80 if (rv != APR_SUCCESS) {
81 printf("Consumer unable to attach to name-based shared memory "
82 "segment: [%d] %s \n", rv,
83 apr_strerror(rv, errmsg, sizeof(errmsg)));
88 boxes = apr_shm_baseaddr_get(shm);
90 /* consume messages on all of the boxes */
91 msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */
93 printf("Consumer detaching from name-based shared memory....");
94 rv = apr_shm_detach(shm);
95 if (rv != APR_SUCCESS) {
96 printf("Consumer unable to detach from name-based shared memory "
97 "segment: [%d] %s \n", rv,
98 apr_strerror(rv, errmsg, sizeof(errmsg)));
106 #else /* APR_HAS_SHARED_MEMORY */
110 printf("APR SHMEM test not run!\n");
111 printf("shmem is not supported on this platform\n");
115 #endif /* APR_HAS_SHARED_MEMORY */