Placing a process in the background in C

2019-03-22 00:34发布

问题:

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:

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;
}