#include #include #include #include #include pthread_t threads[255]; void cleanup_handler(void *pipe) { FILE *cast_pipe; cast_pipe = (FILE *) pipe; pclose(pipe); } void *exec_newthread(void *input) { char *cast_input, *out_buf; FILE *out_pipe; out_buf = (char *) calloc(1, 512); cast_input = (char *) input; out_pipe = popen(cast_input, "r"); pthread_cleanup_push(cleanup_handler, (void *) out_pipe); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); for(;;) { fgets(out_buf, 255, out_pipe); if(feof(out_pipe)) break; printf("%s", out_buf); } pthread_cleanup_pop(1); return 0x0; } int main(int argc, char *argv[]) { int thread_counter; char *input_buf; thread_counter = 0; input_buf = (char *) calloc(1, 512); while(strncmp(input_buf, "quit", 4)) { fgets(input_buf, 255, stdin); if(!strncmp(input_buf, "quit", 4)) break; pthread_create(&threads[thread_counter], NULL, exec_newthread, input_buf); pthread_detach(threads[thread_counter]); ++thread_counter; } return 0; }