int main(void) { apr_status_t rv; apr_pool_t *pool; apr_shm_t *shm; int recvd; apr_initialize(); if (apr_pool_create(&pool, NULL) != APR_SUCCESS) { exit(-1); } rv = apr_shm_attach(&shm, SHARED_FILENAME, pool); if (rv != APR_SUCCESS) { exit(-2); } boxes = apr_shm_baseaddr_get(shm); /* consume messages on all of the boxes */ recvd = msgwait(30, 0, N_BOXES); /* wait for 30 seconds for messages */ rv = apr_shm_detach(shm); if (rv != APR_SUCCESS) { exit(-3); } return recvd; }
static void test_anon(abts_case *tc, void *data) { apr_proc_t proc; apr_status_t rv; apr_shm_t *shm; apr_size_t retsize; int cnt, i; int recvd; rv = apr_shm_create(&shm, SHARED_SIZE, NULL, p); APR_ASSERT_SUCCESS(tc, "Error allocating shared memory block", rv); ABTS_PTR_NOTNULL(tc, shm); retsize = apr_shm_size_get(shm); ABTS_INT_EQUAL(tc, SHARED_SIZE, retsize); boxes = apr_shm_baseaddr_get(shm); ABTS_PTR_NOTNULL(tc, boxes); rv = apr_proc_fork(&proc, p); if (rv == APR_INCHILD) { /* child */ int num = msgwait(5, 0, N_BOXES); /* exit with the number of messages received so that the parent * can check that all messages were received. */ exit(num); } else if (rv == APR_INPARENT) { /* parent */ i = N_BOXES; cnt = 0; while (cnt++ < N_MESSAGES) { if ((i-=3) < 0) { i += N_BOXES; /* start over at the top */ } msgput(i, MSG); apr_sleep(apr_time_make(0, 10000)); } } else { ABTS_FAIL(tc, "apr_proc_fork failed"); } /* wait for the child */ rv = apr_proc_wait(&proc, &recvd, NULL, APR_WAIT); ABTS_INT_EQUAL(tc, N_MESSAGES, recvd); rv = apr_shm_destroy(shm); APR_ASSERT_SUCCESS(tc, "Error destroying shared memory block", rv); }
static apr_status_t test_anon(apr_pool_t *parpool) { apr_status_t rv; apr_pool_t *pool; apr_shm_t *shm; apr_size_t retsize; pid_t pid; int cnt, i, exit_int; rv = apr_pool_create(&pool, parpool); if (rv != APR_SUCCESS) { fprintf(stderr, "Error creating child pool\n"); return rv; } printf("Creating anonymous shared memory block (%" APR_SIZE_T_FMT " bytes)........", SHARED_SIZE); rv = apr_shm_create(&shm, SHARED_SIZE, NULL, pool); if (rv != APR_SUCCESS) { fprintf(stderr, "Error allocating shared memory block\n"); return rv; } fprintf(stdout, "OK\n"); printf("Checking size...%" APR_SIZE_T_FMT " bytes...", retsize = apr_shm_size_get(shm)); if (retsize != SHARED_SIZE) { fprintf(stderr, "Error allocating shared memory block\n"); return rv; } fprintf(stdout, "OK\n"); printf("Allocating shared mbox memory for %d boxes ..............", N_BOXES); boxes = apr_shm_baseaddr_get(shm); if (boxes == NULL) { fprintf(stderr, "Error creating message boxes.\n"); return rv; } fprintf(stdout, "OK\n"); printf("Shared Process Test (child/parent)\n"); pid = fork(); if (pid == 0) { /* child */ msgwait(5, 0, N_BOXES); exit(0); } else if (pid > 0) { /* parent */ i = N_BOXES; cnt = N_MESSAGES; while (--cnt > 0) { if ((i-=3) < 0) { i += N_BOXES; /* start over at the top */ } msgput(i, "Sending a message\n"); apr_sleep(apr_time_make(0, 10000)); } } else { printf("Error creating a child process\n"); return errno; } /* wait for the child */ printf("Waiting for child to exit.\n"); if (waitpid(pid, &exit_int, 0) < 0) { return errno; } printf("Destroying shared memory segment..."); rv = apr_shm_destroy(shm); if (rv != APR_SUCCESS) { printf("FAILED\n"); return rv; } printf("OK\n"); apr_pool_destroy(pool); return APR_SUCCESS; }