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 msgput(int boxnum, char *msg)
44 fprintf(stdout, "Producer: Sending message to box %d\n", boxnum);
45 apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg));
46 boxes[boxnum].msgavail = 1;
59 printf("APR Shared Memory Test: PRODUCER\n");
61 printf("Initializing the pool............................");
62 if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
63 printf("could not initialize pool\n");
68 printf("Producer attaching to name-based shared memory....");
69 rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
70 if (rv != APR_SUCCESS) {
71 printf("Producer unable to attach to name-based shared memory "
72 "segment: [%d] %s \n", rv,
73 apr_strerror(rv, errmsg, sizeof(errmsg)));
78 boxes = apr_shm_baseaddr_get(shm);
80 /* produce messages on all of the boxes, in descending order */
81 for (i = N_BOXES - 1; i > 0; i--) {
82 msgput(i, "Sending a message\n");
83 apr_sleep(apr_time_from_sec(1));
86 printf("Producer detaching from name-based shared memory....");
87 rv = apr_shm_detach(shm);
88 if (rv != APR_SUCCESS) {
89 printf("Producer unable to detach from name-based shared memory "
90 "segment: [%d] %s \n", rv,
91 apr_strerror(rv, errmsg, sizeof(errmsg)));
99 #else /* APR_HAS_SHARED_MEMORY */
103 printf("APR SHMEM test not run!\n");
104 printf("shmem is not supported on this platform\n");
108 #endif /* APR_HAS_SHARED_MEMORY */