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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:28:23+00:00 2026-06-17T22:28:23+00:00

I am trying to mimic the bash feature of running process in the background

  • 0

I am trying to mimic the bash feature of running process in the background if “&” is found at the end of the command. I have the following function…and I don’t think it’s doing what I want it to do

int execute(char* args[],int background,int *cstatus){
    pid_t   child;
    pid_t   ch;                         /*Pid of child returned by wait*/
    if ((child = fork()) == 0){                 /*Child Process*/
        execvp(args[0],args);       
        fprintf(stderr, "RSI: %s: command not found\n",args[0]); /*If execvp failes*/
        exit(1);

    }else{          /*Parent process*/
        if (child== (pid_t)(-1)) {
            fprintf(stderr,"Fork failed\n"); exit(1);
        }else{
            if (background==0){             /*If not running in background..wait for process to finish*/
                ch = wait(cstatus);
            }else{
                printf("%ld Started\n",(long)getpid());
        /*  printf("Parent: Child %ld exited with status = %ld\n", (long) ch, (long)cstatus);
    */  }}
    }
return 0;
}
int wait_and_poll(int *cstatus){
    pid_t status;
    status = waitpid(-1,cstatus,WNOHANG);
    if (status>0){
        fprintf(stdout,"%ld Terminated.\n",(long) status);
    }
return 0;
}

If i just run “ls -l ” it works as expected..but if I want to run ls in the background..and have the program keep accepting new commands I call the function with background flag set as 1 and I want it to run the process in the background, tell me that it has created the process..and then prompt to accept next command.

  • 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-17T22:28:24+00:00Added an answer on June 17, 2026 at 10:28 pm

    I don’t think waitpid(-1, &cstatus, WNOHANG); does what you think it does. You need to check its return value. If it is > 0, that’s the PID of the child process that has exited. If it’s 0 or -1, no child process has changed state.

    You can just call waitpid(-1, &cstatus, WNOHANG); before and/or after each command you run. Call it in a loop to catch more than one child exit.

    You can also handle SIGCHILD. Your process will receive this signal immediately after a child exit, which is good if you want to report child process termination immediately, without waiting for user’s input.

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

Sidebar

Related Questions

I'm trying to mimic static variables on a JavaScript function, with the following purpose:
i am trying to mimic an effect found usually on mobile devices where you
I am trying to mimic the functionality of this curl command in Java: curl
I'm trying to mimic a reddit feature. When you link to a specific comment
I am trying to mimic a feature of C# in Typescript. Let say I
I am trying to mimic the following PHP code in C# <?php if (
I am trying to mimic UINavigationController's pushViewController using UIView animation but I have seem
I'm trying to mimic the ghosting functionality from Scriptaculous with jQuery UI. I've found
I'm trying to mimic the Google suggestions list with this: function el(tid) { return
I have javascript code that i am trying to mimic in an android application:

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.