bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / testshmproducer.c
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
17 #include "apr_shm.h"
18 #include "apr_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "apr_time.h"
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #if APR_HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #if APR_HAS_SHARED_MEMORY
31
32 typedef struct mbox {
33     char msg[1024]; 
34     int msgavail; 
35 } mbox;
36 mbox *boxes;
37
38 #define N_BOXES 10
39 #define SHARED_SIZE (apr_size_t)(N_BOXES * sizeof(mbox))
40 #define SHARED_FILENAME "/tmp/apr.testshm.shm"
41
42 static void msgput(int boxnum, char *msg)
43 {
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;
47 }
48
49 int main(void)
50 {
51     apr_status_t rv;
52     apr_pool_t *pool;
53     apr_shm_t *shm;
54     int i;
55     char errmsg[200];
56
57     apr_initialize();
58     
59     printf("APR Shared Memory Test: PRODUCER\n");
60
61     printf("Initializing the pool............................"); 
62     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
63         printf("could not initialize pool\n");
64         exit(-1);
65     }
66     printf("OK\n");
67
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)));
74         exit(-2);
75     }
76     printf("OK\n");
77
78     boxes = apr_shm_baseaddr_get(shm);
79
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));
84     }
85
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)));
92         exit(-3);
93     }
94     printf("OK\n");
95
96     return 0;
97 }
98
99 #else /* APR_HAS_SHARED_MEMORY */
100
101 int main(void)
102 {
103     printf("APR SHMEM test not run!\n");
104     printf("shmem is not supported on this platform\n"); 
105     return -1;
106 }
107
108 #endif /* APR_HAS_SHARED_MEMORY */
109