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 7952231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:47:52+00:00 2026-06-04T02:47:52+00:00

This is a cleaned code that I’m using in order to execute shell commands.

  • 0

This is a cleaned code that I’m using in order to execute shell commands.
Though isExit is always 0 and loop should run as long as it is !isExit, my program terminates after one loop with the command ls as argument to execute.
Does anyone have any idea?
The output is OK (ls) but then program terminates.
The code is written in C, on Eclipse.
Running on Ubuntu 12 which is running on VM over Windows 7.

int main() {
    int numberOfCommands, numOfWords, i, isExit = 0, isBackGround = 0, isSucces;
    char input[256];
    char *cmdAndArgs[256];
    char *commands[256];
do{
    // gets and parses user command...
    ExecuteCmd(cmdAndArgs);
    } while (!isExit);
    return EXIT_SUCCESS;
}

void ExecuteCmd(char *cmdAndArgs[]){
    pid_t pid;
    pid = fork();
    if (pid != 0) {
        execvp(cmdAndArgs[0], cmdAndArgs);
        exit(0);
    } else {
        waitpid(pid, &status, 0);
    } 
}
  • 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-04T02:47:54+00:00Added an answer on June 4, 2026 at 2:47 am

    You’re running the execvp in the parent process, not in the child. the logic:

    pid_t pid;
    pid = fork();
    if (pid != 0) {
        execvp(cmdAndArgs[0], cmdAndArgs);
        exit(0);
    } else {
        waitpid(pid, &status, 0);
    }
    

    should be reversed to:

    pid_t pid;
    pid = fork();
    if (pid == 0) { /* Child */
        execvp(cmdAndArgs[0], cmdAndArgs);
        exit(0);
    } else if (pid == -1) {
        perror("fork");
    } else {
        waitpid(pid, &status, 0);
    }
    

    the return codes from fork() are: -1 == fork failed (use errno to determine the reason). 0 == I am the child. > 0 == I am the parent. See the reference for fork under the RETURN VALUE section.

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

Sidebar

Related Questions

this is my code that I write it but I want to use LINQ
Here's a fragment of code I'm prototyping that should, by all accounts, never see
I am using suds client for WSDL in our project. i have this code
I am writing some code that checks that my resources are properly cleaned up.
I've got code that I am using from another team and I have spent
I am looking for a cleaner way to do this. My code works, but
This should be simple, but the answer is eluding me. If I've got a
I am using jquery validate and the jquery version of tinymce. I found this
I have a form that is using jQuery Validate plugin for client side validation,
I have a nested loop that I would like to break out of. After

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.