/** * @brief writes data from the stream to the ring buffer-based shared memory * * @param shm_size the size of the shared memory * @param stream the stream to take as input * * @returns 0 if everything went well and -1 in case of error */ int shared_send(long shm_size, FILE *stream) { shared_t data; int input = EOF; int position = 0; if (shared_init(shm_size, &data) == -1) { /* error is printed by shared_init() */ shared_cleanup(&data); return -1; } do { /* decrement the free space and wait if 0 */ if (sem_wait(data.sem_w_id) == -1) { /* try again after a signal interrupt */ if (errno == EINTR) { continue; } else { warn("sem_wait"); shared_cleanup(&data); return -1; } } /* stop if other process triggered shared_cleanup() */ if (data.shm_buffer[data.shm_size] == EOF) { warnx("Receiver exited unexpectedly"); shared_close(&data); return -1; } input = fgetc(stream); if (input == EOF && ferror(stream) != 0) { warn("fgetc"); shared_cleanup(&data); return -1; } data.shm_buffer[position] = input; position++; /* stay within bounds of the ring buffer */ if (position == shm_size) { position = 0; } /* increment the number of characters */ if (sem_post(data.sem_r_id) == -1) { warn("sem_post"); shared_cleanup(&data); return -1; } } while (input != EOF); shared_close(&data); return 0; }
int main (int argc, char **argv) { int i; char *target = NULL; char *mxdir = NULL; char *propfile = NULL; FILE *file; shared_init(32*1024*1024); err_init(); log_init(LOG_LEVEL); mman_init(); while (1) { char c = getopt(argc, argv, "n:d:p:"); if (c == -1) { break; } switch (c) { case 'd': mxdir = optarg; break; case 'n': target = optarg; break; case 'p': propfile = optarg; break; } } if (argc < 2) { usage(); } else { c = mxc_create(); c->mxdir = mxdir; c->target = target; prop_addFromFile(c->props, propfile); for (i = optind; i < argc; i++) { c->filename = argv[i]; file = fopen(argv[i], "r"); err_assert(file != NULL, err_f("IOError")); yyin = file; yylineno = 1; yyparse(); } mxc_emitIncludes(c); mxc_emitExports(c); mxc_emitInterface(c); mxc_emitCode(c); mxc_emitBuildScript(c); //mxc_free(c); } shared_cleanup(); return 0; }
/** * @brief handles signals by performing a cleanup first * * @param signum the signal number */ static void shared_signal(int signum) { warnx("Caught signal %d", signum); shared_cleanup(data_ptr); /* send the signal again, but perform the default action (reset by SA_RESETHAND) */ if (raise(signum) != 0) { warn("raise"); } }
/*! \brief Code shutdown and memory deallocation * * Called at the end of main(), closes everything */ void cleanup (void) { free (b_map); free (f_map); free (map); free (o_map); free (sh_map); free (z_map); free (c_map); free (cb_map); free (cf_map); free (co_map); free (csh_map); free (cz_map); //free (strbuf); shared_cleanup (); } /* cleanup () */
/** * @brief prints data from the ring buffer-based shared memory * * @param shm_size the size of the shared memory * @param stream the stream to take as output * * @returns 0 if everything went well and -1 in case of error */ int shared_receive(long shm_size, FILE *stream) { shared_t data; int output = EOF; int position = 0; if (shared_init(shm_size, &data) == -1) { /* error is printed by shared_init() */ shared_cleanup(&data); return -1; } do { /* decrement the number of characters and wait if 0 */ if (sem_wait(data.sem_r_id) == -1) { /* try again after a signal interrupt */ if (errno == EINTR) { continue; } else { warn("sem_wait"); shared_cleanup(&data); return -1; } } /* stop if other process triggered shared_cleanup() */ if (data.shm_buffer[data.shm_size] == EOF) { warnx("Sender exited unexpectedly"); shared_close(&data); return -1; } output = data.shm_buffer[position]; position++; if (output != EOF) { if (fputc(output, stream) == EOF) { warn("fputc"); shared_cleanup(&data); return -1; } } /* stay within bounds of the ring buffer */ if (position == shm_size) { position = 0; } /* increment the free space */ if (sem_post(data.sem_w_id) == -1) { warn("sem_post"); shared_cleanup(&data); return -1; } } while (output != EOF); /* force a write of the possibly buffered stream */ if (fflush(stream) == EOF) { warn("fflush"); return -1; } shared_cleanup(&data); return 0; }
int smem_shutdown(void) { if (shared_init_called) shared_cleanup(); return(0); }