Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

471
Views
WEXITSTATUS devuelve 1 (en padre), aunque el hijo devuelve 0

Estoy usando el siguiente código para ejecutar algunos comandos usando padre/hijo:

 int nStatus = 0; int nRet = 0; pid_t pid = -1; char *envp[] = { "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/tmp", 0 }; if(pnChildExitStatus == NULL || pcBuf == NULL) { nRet = -EINVAL; goto returnHandler; } *pnChildExitStatus = 0; pid = fork(); switch(pid) { case -1: nRet = -errno; //can't fork break; case 0: //child nRet = execl("/bin/sh","sh","-c", pcBuf, envp,NULL); if (-1 == nRet) { nRet = -errno; goto returnHandler; } break; default: //parent /*After this call 'nStatus' is an encoded exit value. WIF macros will extract how it exited*/ if( waitpid(pid, &nStatus, 0) < 0 ) { nRet = -errno; *pnChildExitStatus = errno; } if(WIFEXITED(nStatus)) { nRet = EXIT_SUCCESS; *pnChildExitStatus = WEXITSTATUS(nStatus); } goto returnHandler; break; } returnHandler: return nRet; }

Mi problema es el siguiente:

En algunos casos, Parent recibe WEXITSTATUS(nStatus) != 0, aunque mi proceso secundario siempre devuelve 0 (lo verifiqué con impresiones de depuración).

¿Alguien puede ofrecer alguna idea sobre cómo es esto posible?

Gracias a todos de antemano.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su uso de waitpid() es defectuoso.

Si se interrumpe waitpid() , devolverá -1,
sin embargo, el valor en nStatus puede no estar definido, por lo tanto, if (WIFEXITED(nStatus)) aún podría ser cierto
y WEXITSTATUS(nStatus) podría ser cualquier cosa.

¿Qué puede interrumpir una llamada al sistema (como waitpid)?
Una señal, como, SIGCHLD, para un niño que sale.

El patrón normal es algo como:

 while ((wpid = waitpid(pid, &nStatus, 0)) != pid) { if (wpid == -1) { /* see below */ if (errno == EINTR) continue; /* else handle the other error (pretty unlikely) */ /* probably by returning something */ } } if (wpid == -1) { /* error */ } /* if you choose to `break' from the loop on error */ else { /* process exited */ } /* you need to check the situation here */

El if (wpid == -1) es un poco superfluo aquí, ya que el único valor que wpid podría tener dentro de este bucle es -1 , pero otras variantes de este patrón no restringen wpid con tanta fuerza, por lo que es una programación defensiva contra un cambio.

De manera similar, con if (errno == EINTR) como los únicos 3 errno s de la mayoría (¿todos?) waitpid s son: EINTR, ECHILD (sin hijos) y EFAULT (se equivocó en el segundo argumento). Pero, de nuevo, programación defensiva.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!