Thread: Child process status when command doesn't exist/path iis worng

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    13

    Child process status when command doesn't exist/path iis worng

    Hi, I have this program which gets two arguments as parameters and runs it in linux:

    Code:
    
     int main(int argc, char *argv[])
    {
     
        int status;
        pid_t pid;
        pid = fork();
        if(pid == 0)
           execvp(argv[1],argv+1);
        else if (pid >0) {
                /*this is the part of the code I'm missing*/
                }
        return 0;
    }
    I need to capture if the parameters passed are not a valid UX command.
    I've tried WIFEXITED and WEXISTSTATUS but they always return "1" whether it's a valid linux command or not. Is there a way to know if the linux command wwhich the child process is running doesn't exist?
    Last edited by Sergi; 05-12-2019 at 05:49 AM.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Sergi View Post
    I need to capture if the parameters passed are not a valid UX command.
    I've tried WIFEXITED and WEXISTSTATUS but they always return "1" whether it's a valid linux command or not. Is there a way to know if the linux command wwhich the child process is running doesn't exist?
    fork() returns the PID of the child process in the forked process, 0 in the child process or -1 in case of error.

  3. #3
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    Quote Originally Posted by flp1969 View Post
    fork() returns the PID of the child process in the forked process, 0 in the child process or -1 in case of error.

    My question is, what status is returned when the UX command the child is executing doesn't exist or can't be executed because it's a wrong path?

    I'm asking because I'm using WIFEXITED(status) and WEXITSTATUS(status) and they return 1, even when the commands I feed to the program are not linux commands.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Sergi View Post
    I'm asking because I'm using WIFEXITED(status) and WEXITSTATUS(status) and they return 1, even when the commands I feed to the program are not linux commands.
    These macros are applied only to the status returned as the first argument of wait() function. wait() returns (in its argument) the status (exit code) of child processes only.

    WIFEXITED() returns a boolean: 1 if the child process has exited, 0 otherwise.
    WEXITSTATUS() returns the exit code of the process.

    fork() will return -1 if it cannot fork the current process (and set errno with the error)... exec() functions will return ONLY if an error occurs (and errno will NOT be changed)... So, if execvp() returns, you can assume the command is wrong (doesn't exist or cannot be spawned)...

    See 'man 3 wait' and "man execvp" for more details.

  5. #5
    Registered User
    Join Date
    Dec 2018
    Posts
    13
    Will look into that, thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generated absolute path via for in doesn't exist
    By awsdert in forum Linux Programming
    Replies: 3
    Last Post: 02-06-2019, 04:31 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Child process status
    By paraglidersd in forum C Programming
    Replies: 8
    Last Post: 07-24-2008, 10:51 AM
  4. get the status of the child process
    By mystic-d in forum C Programming
    Replies: 9
    Last Post: 11-17-2007, 04:59 AM
  5. Doesn't exist?
    By Llam4 in forum C# Programming
    Replies: 7
    Last Post: 04-10-2007, 12:33 PM

Tags for this Thread