Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 421955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:02:02+00:00 2026-05-12T19:02:02+00:00

I do the regular thing: fork() execvp(cmd, ) in child If execvp fails because

  • 0

I do the regular thing:

  • fork()
  • execvp(cmd, ) in child

If execvp fails because no cmd is found, how can I notice this error in parent process?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:02:02+00:00Added an answer on May 12, 2026 at 7:02 pm

    The well-known self-pipe trick can be adapted for this purpose.

    #include <errno.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/wait.h>
    #include <sysexits.h>
    #include <unistd.h>
    
    int main(int argc, char **argv) {
        int pipefds[2];
        int count, err;
        pid_t child;
    
        if (pipe(pipefds)) {
            perror("pipe");
            return EX_OSERR;
        }
        if (fcntl(pipefds[1], F_SETFD, fcntl(pipefds[1], F_GETFD) | FD_CLOEXEC)) {
            perror("fcntl");
            return EX_OSERR;
        }
    
        switch (child = fork()) {
        case -1:
            perror("fork");
            return EX_OSERR;
        case 0:
            close(pipefds[0]);
            execvp(argv[1], argv + 1);
            write(pipefds[1], &errno, sizeof(int));
            _exit(0);
        default:
            close(pipefds[1]);
            while ((count = read(pipefds[0], &err, sizeof(errno))) == -1)
                if (errno != EAGAIN && errno != EINTR) break;
            if (count) {
                fprintf(stderr, "child's execvp: %s\n", strerror(err));
                return EX_UNAVAILABLE;
            }
            close(pipefds[0]);
            puts("waiting for child...");
            while (waitpid(child, &err, 0) == -1)
                if (errno != EINTR) {
                    perror("waitpid");
                    return EX_SOFTWARE;
                }
            if (WIFEXITED(err))
                printf("child exited with %d\n", WEXITSTATUS(err));
            else if (WIFSIGNALED(err))
                printf("child killed by %d\n", WTERMSIG(err));
        }
        return err;
    }
    

    Here’s a complete program.

    $ ./a.out foo
    child's execvp: No such file or directory
    $ (sleep 1 && killall -QUIT sleep &); ./a.out sleep 60
    waiting for child...
    child killed by 3
    $ ./a.out true
    waiting for child...
    child exited with 0
    

    How this works:

    Create a pipe, and make the write endpoint CLOEXEC: it auto-closes when an exec is successfully performed.

    In the child, try to exec. If it succeeds, we no longer have control, but the pipe is closed. If it fails, write the failure code to the pipe and exit.

    In the parent, try to read from the other pipe endpoint. If read returns zero, then the pipe was closed and the child must have exec successfully. If read returns data, it’s the failure code that our child wrote.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 222k
  • Answers 222k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Closing a file also flushes the writes to disk --… May 13, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer Here's An Introduction To The SQLite C/C++ Interface. Here's the… May 13, 2026 at 12:23 am
  • Editorial Team
    Editorial Team added an answer A single control cannot use multiple sources. You could create… May 13, 2026 at 12:23 am

Related Questions

I know this is normally rather stupid, but don't shoot me before reading the
My site is all php pages, since it's all database stuff. However, I'm having
I want to reset an object. Can I do it in the following way?
Hi I am using a lot of temporary files in java and my problem

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.