0

how can I print the exit status of the child program within the father process? ( after wait() )

the code to get the exit status of the child process works, but I don't know how to print it after

#include <stdio.h>   // printf
#include <unistd.h>  // getpid
#include <stdlib.h>

void childProcess(void);
void fatherProcess(int childpid);

int num1, num2, oper_inizializzata;

int main() {

  int pid=fork();
  if (pid==0) {
    childProcess();
  } else if (pid>0) {
    fatherProcess(pid);
  } else { // pid<0
    printf("Creazione del processo figlio fallita!\n");
  }
}

void childProcess(void)
{
  printf("Inserisci il valore del primo numero\n");
  scanf("%d",&num1);
  printf("Inserisci il valore del secondo numero\n");
  scanf("%d",&num2);
  printf("Scegli tra 1 o 2 per il valore della variabile\n");
  scanf("%d",&oper_inizializzata);
  
  if(oper_inizializzata==1){
    int num1piu2=num1+num2;
    printf("num1 + num2 e' %d\n", num1piu2);
    exit(num1piu2);
  }
  else if(oper_inizializzata==2){
    int num1per2=num1*num2;
    exit(num1per2);}
}

void fatherProcess(int childpid)
{
  wait();
  //print exit status here
}
Akash Dahane
  • 254
  • 2
  • 14
Stee
  • 1
  • *the code to get the exit status of the child process works*. I don't see any code to get the exit status of the child process. Can you please point that out? – kaylum Feb 15 '21 at 00:30
  • @kaylum That's the problem; it isn't there yet. OP wants to know what to do in `fatherProcess` to reap the exit state of the waited-on child. – WhozCraig Feb 15 '21 at 00:31
  • 1
    Please read the [wait manual](https://man7.org/linux/man-pages/man2/wait.2.html). It even has an example there that does exactly what is being asked. – kaylum Feb 15 '21 at 00:32
  • 1
    @WhozCraig: The question states “the code to get the exit status of the child process works,” implying OP does have code to get the exit status but, since it does not appear in the question, that they have not shown it to us. – Eric Postpischil Feb 15 '21 at 00:33
  • See `man 2 wait`. You can do: `void fatherProcess(pid_t childpid) { int status; wait(&status); do { if (WIFEXITED(status)) { printf("exited with code %d\n",WEXITCODE(status)); break; } if (WIFSIGNALED(status)) { printf("exited with signal %d\n",WTERMSIG(status)); break; } ... } while (0); }` – Craig Estey Feb 15 '21 at 00:34
  • 1
    Does this answer your question? [Capturing exit status code of child process](https://stackoverflow.com/questions/27306764/capturing-exit-status-code-of-child-process) – kaylum Feb 15 '21 at 00:41
  • @kaylum thanks, even if being a beginner I wouldn't know how to apply that example to my code ;( – Stee Feb 15 '21 at 00:48

2 Answers2

2

From the man page of wait

pid_t wait(int *wstatus);

If wstatus is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the following macros (which take the integer itself as an argument, not a pointer to it, as is done in wait() and waitpid()!):

...

For example:

void fatherProcess(int childpid)
{
  int status;
  pid_t pid;
  pid = wait(&status);

  if (pid == -1) {
    perror("wait");
    // handle this case 
  }
  if (WIFEXITED(status)) {
    printf("Child %ld exited successfully", pid_t);
  }
  // Handle the rest of the status cases accordingly
}
Zois Tasoulas
  • 1,242
  • 1
  • 11
  • 23
1

You can use waitpid() to get exit status of child process.

Example:

void fatherProcess(int childpid)
{
    int status;
    waitpid(childpid,&status,0);
    if(WIFEXITED(status)){
        print("exit status of child=%d\n",WEXITSTATUS(status));
    }
    return;
}
Akash Dahane
  • 254
  • 2
  • 14