--- libaitio/src/exec.c 2013/12/08 21:11:54 1.1.2.16 +++ libaitio/src/exec.c 2013/12/12 21:17:53 1.1.2.18 @@ -173,11 +173,64 @@ io_progCloseAt(prog_t * __restrict prg, u_int idx) } /* + * io_progOpen2() - Start program from pool on first unused slot + * + * @prg = program pool + * return: -1 error, >-1 reside at slot + */ +int +io_progOpen2(prog_t * __restrict prg) +{ +#ifdef POPEN_STREAM + FILE *f = NULL; +#else + int f = -1; +#endif + int stat, ret = -1; + register int i; + pid_t pid; + + if (!prg) + return -1; + if (prg->prog_cnum + 1 > prg->prog_maxn) { + io_SetErr(EINVAL, "Requested number for program execution is over pool's limit"); + return -1; + } + + pthread_mutex_lock(&prg->prog_mtx); + for (i = 0; i < array_Size(prg->prog_fds); i++) + if (!array_Get(prg->prog_fds, i)) { + f = e_popen(prg->prog_name, "r+", &pid); +#ifdef POPEN_STREAM + if (!f) { +#else + if (f == -1) { +#endif + LOGERR; + break; + } else if (waitpid(pid, &stat, WNOHANG)) { + io_SetErr(ECHILD, "Program with pid=%d exit with status %d", + pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1); + e_pclose(f); + break; + } else + array_Set(prg->prog_fds, i, f); + clrbit(prg->prog_used, i); + prg->prog_cnum++; + ret = i; + break; + } + pthread_mutex_unlock(&prg->prog_mtx); + + return ret; +} + +/* * io_progOpen() - Execute number of program(s) * * @prg = program pool * @execNum = execute program(s) (0 max) - * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error + * return: -1 error, >0 executed programs */ int io_progOpen(prog_t * __restrict prg, u_int execNum) @@ -192,10 +245,10 @@ io_progOpen(prog_t * __restrict prg, u_int execNum) pid_t pid; if (!prg) - return 0; + return -1; if (prg->prog_cnum + execNum > prg->prog_maxn) { io_SetErr(EINVAL, "Requested number for program execution is over pool's limit"); - return 0; + return -1; } pthread_mutex_lock(&prg->prog_mtx); @@ -213,6 +266,7 @@ io_progOpen(prog_t * __restrict prg, u_int execNum) } else if (waitpid(pid, &stat, WNOHANG)) { io_SetErr(ECHILD, "Program with pid=%d exit with status %d", pid, WIFEXITED(stat) ? WEXITSTATUS(stat) : -1); + e_pclose(f); ret = -1; break; } else @@ -231,7 +285,8 @@ io_progOpen(prog_t * __restrict prg, u_int execNum) * * @prg = program pool * @toNum = execute to number of programs (0 max) - * return: 0 error, >0 executed programs and abs(<0) executed programs with logged error + * return: 0 error or nothing to do, + * >0 executed programs and abs(<0) executed programs with logged error */ int io_progGrow(prog_t * __restrict prg, u_int toNum) @@ -244,7 +299,12 @@ io_progGrow(prog_t * __restrict prg, u_int toNum) } if (!toNum) toNum = prg->prog_maxn; + if (toNum < prg->prog_inin) + toNum = prg->prog_inin; + if ((toNum - prg->prog_cnum) < 1) + return 0; + return io_progOpen(prg, toNum - prg->prog_cnum); } @@ -302,7 +362,7 @@ io_progVacuum(prog_t * __restrict prg, u_int toNum) * io_progCheck() - Check exit status of program pool * * @prg = program pool - * @re = resurrect program + * @re = resurrect program to init number * return: -1 error or >-1 exited programs */ int @@ -336,9 +396,9 @@ io_progCheck(prog_t * __restrict prg, int re) } pthread_mutex_unlock(&prg->prog_mtx); - /* resurrect */ - if (re && ret > 0) - io_progOpen(prg, ret); + /* resurrect to init number */ + if (re && prg->prog_inin - prg->prog_cnum) + io_progOpen(prg, prg->prog_inin - prg->prog_cnum); return ret; } @@ -383,6 +443,15 @@ io_progAttach(prog_t * __restrict prg, int newOne) break; } pthread_mutex_unlock(&prg->prog_mtx); + + /* not found free prog */ + if (!f && (i = io_progOpen2(prg)) > 0) { +#ifdef POPEN_STREAM + f = array(prg->prog_fds, i, FILE*); +#else + f = array(prg->prog_fds, i, intptr_t); +#endif + } /* execute new one program */ if (newOne && f)