void filter_changesets (database_t * db, changeset_t ** serial, changeset_t ** serial_end, const char * filter_command) { // Set up a pipeline for running the subprocess and sending the data to it. struct filter_context context = { db, serial, serial_end }; pipeline * pl = pipeline_new(); pipeline_command ( pl, pipecmd_new_function ("filter source", filter_output, NULL, &context)); pipeline_command_argstr (pl, filter_command); pipeline_want_out (pl, -1); fflush (NULL); // We're forking... pipeline_start (pl); filter_input (db, pipeline_get_outfile (pl)); int res = pipeline_wait (pl); if (res != 0) fatal ("filter subprocess gave error: %i\n", res); pipeline_free (pl); }
int main(int argc, char **argv) { pipeline_t *pl; char line[1024]; int n; int i; int stages; pipeline_stage_process_pf *stages_proc; int max_random; struct timeval start; struct timeval end; int start_num = 0; stages = atoi(argv[1]); max_random = atoi(argv[2]); if (argc == 4) { start_num = atoi(argv[3]); if (start_num <= 0) { printf(" usage: test_pipeline <stage_num> <max_random> [start_num]\n"); return 1; } } if (stages<1 || max_random<1) { printf(" usage: test_pipeline <stage_num> <max_random> [start_num]\n"); return 1; } stages_proc = malloc(stages * sizeof(pipeline_stage_process_pf)); for (i = 0; i < stages-1; ++i) stages_proc[i] = _stage; stages_proc[stages-1] = _stage_tail; pl = pipeline_create(stages, stages_proc); free(stages_proc); if (start_num == 0) printf("input an integer, and end with a command named 'q'\n"); while (1) { if (start_num == 0) { printf("data> "); if (fgets(line, sizeof(line), stdin) == NULL) break; line[strlen(line)-1] = 0; if (strcmp(line, "q") == 0) break; if (sscanf(line, "%d", &n) != 1) { printf("please input valid integer\n"); continue; } } // int num = random() % max_random; g_random_count += max_random; gettimeofday(&start, NULL); for (i = max_random; i > 0; --i) pipeline_start(pl, (void *) (long) (n + i)); pipeline_wait(pl, 0); gettimeofday(&end, NULL); test_time("pipeline", &start, &end); if (start_num != 0) break; } printf("\n\n---------------count is: %d[%d]\n\n", g_count, g_random_count); assert(g_count == g_random_count); pipeline_wait(pl, 1); pipeline_destroy(pl); return EXIT_SUCCESS; }