C-program does not return from wait-statement -


i have migrate c-program openvms linux, , have difficulties program generating subprocesses. subprocess generated (fork works fine), execve fails (which correct, wrong program name given).

but reset number of active subprocesses, afterwards call wait() not return. when @ process via ps, see there no more subprocesses, wait() not return echild had thought.

while (jobs_to_be_done) {    if (running_process_cnt < max_process_cnt)    {       if ((pid = vfork()) == 0)       {          params[0] = param1 ;          params[1] = null ;          if ((cstatus = execv(command, params)) == -1)          {             perror("child - exec failed") ;   // happens             exit(exit_failure) ;          }       }       else if (pid < 0)       {          printf("\nmain - child process failed") ;       }       else       {          running_process_cnt++ ;       }    }    else   // no more free process slot, wait    {       if ((pid = wait(&cstatus)) == -1)   // not return statement       {          if (errno != echild)          {             perror("main: wait failed") ;          }          anz_sub = 0 ;       }       else       {          ...       }    } } 

is has done tell wait-command there no more subprocesses? openvms program works fine.

thanks lot in advance help

i don't recommend using vfork these days on linux, since fork(2) efficient enough, lazy copy-on-write techniques in linux kernel.

you should check result of fork. unless failing, process has been created, , wait (or waitpid(2), perhaps wnohang if don't want wait, find out ended child processes ...) should not fail (even if exec function in child has failed, fork did succeed).

you might carefully use sigchld signal, see signal(7). defensive way of using signals set volatile sigatomic_t flag in signal handlers, , test , clear these flags inside loop. recall async signal safe functions (and there quite few of them) can called -even indirectly- inside signal handler. read posix signals.

take time read advanced linux programming wider picture in mind. don't try mimic openvms on posix, think in posix or linux way!

you may want waitpid in loop, perhaps (sometimes or always) wnohang. waitpid should not called in else part of if (running_process_cnt < max_process_cnt) in every iteration of loop.

you might want compile warnings & debug info (gcc -wall -wextra -g) use gdb debugger. strace(1) program (probably -f)

you might want learn memory overcommitment. dislike feature , disable (e.g. running echo 0 > /proc/sys/vm/overcommit_memory root). see proc(5) -which useful know about...


Comments