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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:11:25+00:00 2026-06-12T15:11:25+00:00

I’m currently diving into creating a backgrounding a job in C with & .

  • 0

I’m currently diving into creating a backgrounding a job in C with &. I need to implement a non-blocking waitpid in order for this to work. I know that. Also, I already am catching the condition if & is entered at the end of the command line. I’m just not sure how to exactly send the process with the end to be a background job and also implement it as executing while another prompt is prompting for the next command.
Anything would help at all, thank you.

    struct bgprocess{
        int pid;
        struct bgprocess * next;
        struct bgprocess * prev;    
    };

    struct bgprocess * bgprocess1;
    bgprocess1 = malloc(sizeof(struct bgprocess));
    bgprocess1->prev = NULL;
    bgprocess1->next = NULL;
    bgprocess1->pid = NULL;

    struct bgprocess * current;
    current = bgprocess1;

    do{
        int bgreturn = 0;
        while (current != NULL){
            if (waitpid(current->pid, &bgreturn, WNOHANG)){
                printf("Child exited");
                current->prev->next = current->next;
                current->next->prev = current->prev;
                current->prev = NULL;
                current->next = NULL;
                free(current);                  
            }
        current = current->next;    
        }
        if (end){
            int pid = fork();
            if (pid < 0){
                exit(1);            
            }       
            if (pid) {
                execvp(args[0], args);          
                exit(0);            
            }

            struct bgprocess * newNode;
            newNode = malloc(sizeof(struct bgprocess));
            newNode->pid = pid;
            newNode->next = NULL;

            if (current->next == NULL){
                current->next = newNode;        
            }
            while (1){
                if (current->next == NULL){
                    current->next = newNode;
                }           
                current = current->next;
            }

        }
    }
    while (current != NULL);

    int bgreturn = 0;
    while (current != NULL){
        if (waitpid(current->pid, &bgreturn,0)){
            printf("Child exited");
            current->prev->next = current->next;
            current->next->prev = current->prev;
            current->prev = NULL;
            current->next = NULL;
            free(current);  
        }
        current = current->next;
    }
    }

Alright, so I’ve been working on this some more and I think I may be starting to understand. I still have a few syntax errors that I’m unaware of how to fix so I’ll probably use gdb or something unless someone else can point them out. Am I going about it the right way or am I completely wrong?

  • 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-12T15:11:26+00:00Added an answer on June 12, 2026 at 3:11 pm

    Sounds like you’re implementing a shell.

    Just use fork to create a child process – which will be concurrent. From there you can use the exec* family to execute whatever executables you like, or simply run C code in the child while the parent goes back and prompts for more information (next command, etc.) Use wait with the WNOHANG option at the top of the loop to check for terminated children, and again (this time without WNOHANG) at the end for the rest of those children.

    I must encourage you not to make this more complicated than it really is. Write out what you want in plain english (or your native language, or pseudocode), then merely translate that into C with the minimum amount of cleverness.

    (pseudo)Code:

    struct child {
        int pid;
        struct child * next;
        struct child * prev;
    }
    struct child * children = null;
    do {
        int return = 0;
        struct child * curr = children;
        while(curr != null){
            if(waitpid(curr->pid, &return, WNOHANG)){
                //Report child exited with return status 'return'
                //Remove child (linked list style)
             }
             curr = curr->next;
         }
         /* PROMPT, ETC */
         if ( doInBackground ){
             int pid = fork();
             if(pid <0 )exit(); //error
             if(pid){
                 //Child
                 execvp(processName, arrayOfArgs);
                 //This should never get executed
                 exit();
             }
             //Add pid (linked list style, again)
         }
    }while(!exitCondition)
    
    int return = 0;
    struct child * curr = children;
    while(curr != null){
        if(waitpid(curr->pid, &return, 0)){
            //Report child exited with return status 'return'
            //Remove child (linked list style)
        }
        curr = curr->next;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

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.