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

  • SEARCH
  • Home
  • 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 8867043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:55:58+00:00 2026-06-14T16:55:58+00:00

I need to be able to: fork a process and make it execvp (I

  • 0

I need to be able to:

  1. fork a process and make it execvp (I did that)
  2. check if the child process execvp was successful (don’t know how)
  3. check if the child process finished (having problems)

I’m forking a process and I don’t have any way to check if the childs’s execvp worked or failed. If it failed I need to be able to know that it failed. Currently I’m using

-1 != waitpid( pid, &status, WNOHANG )

But it seems that if the execv of the pid process fails the waitpid does not return -1.

How could I check that? I read the waitpid man page, but it isn’t clear to me; maybe my English isn’t good enough.

EDIT: in order to explain more:
I’m building my own terminal for a Home Work. I need to get as an input a command string, lets say “ls” and then I have to execute the command.
After the child forks, the child calls execvp in order to execute the command ( after I parse the string ), and the parent need to check whether there was a ‘&’ at the end of the command or not.
if the sign ‘&’ does not exist at the end of the command then the parent need to wait for the child to execute.

so I need to know if the execvp failed. If it didn’t failed then the parent use waitpid to wait for the child to finish it execution. If it failed then the parent will not wait for the child.

  • 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-06-14T16:55:59+00:00Added an answer on June 14, 2026 at 4:55 pm

    A common solution to #2 is to open a pipe prior to the fork(), then write to it in the child following the exec. In the parent, a successful read means the exec failed; an unsuccessful read means the exec succeeded and the write never took place.

    // ignoring all errors except from execvp...
    int execpipe[2];
    pipe(execpipe);
    fcntl(execpipe[1], F_SETFD, fcntl(execpipe[1], F_GETFD) | FD_CLOEXEC);
    if(fork() == 0)
    {
        close(execpipe[0]);
        execvp(...); // on success, never returns
        write(execpipe[1], &errno, sizeof(errno));
        // doesn't matter what you exit with
        _exit(0);
    }
    else
    {
        close(execpipe[1]);
        int childErrno;
        if(read(execpipe[0], &childErrno, sizeof(childErrno)) == sizeof(childErrno))
        {
            // exec failed, now we have the child's errno value
            // e.g. ENOENT
        }
    }
    

    This lets the parent unambiguously know whether the exec was successful, and as a byproduct what the errno value was if unsuccessful.

    If the exec was successful, the child process may still fail with an exit code, and examining the status with the WEXITSTATUS macro give you that condition as well.

    NOTE: Calling waitpid with the WNOHANG flag is nonblocking, and you may need to poll the process until a valid pid is returned.

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

Sidebar

Related Questions

I have a iPhone app that must work offline, and need be able to
Right now I'm writing a C program that must execute a child process. I'm
I am planning a Python script that'll use os.fork() to create a bunch of
I need to use the fork() and wait() functions to complete an assignment. We
Lets begin with the final goal - I need to able to paste a
I need to able to block any and all connections to my pc from
I'm trying to write a python program that is able to interact with other
I need to connect via TCP to a port that's behind a firewall, accessible
I have a string expression that I need to do eval on. This expression
I need to able to configure every 6PM and 1 AM in build periodically

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.