5

我目前正在潜心研究C使用&. 我需要实现非阻塞waitpid才能使其正常工作。我知道。&另外,如果在命令行末尾输入,我已经掌握了条件。我只是不确定如何准确地将进程最终发送为后台作业,并将其实现为正在执行,而另一个提示正在提示输入下一个命令。
任何事情都会有所帮助,谢谢。

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

好吧,所以我一直在努力解决这个问题,我想我可能开始理解了。我仍然有一些我不知道如何修复的语法错误,所以我可能会使用 gdb 或其他东西,除非其他人能指出它们。我是以正确的方式去做还是我完全错了?

4

1 回答 1

11

听起来你正在实现一个外壳。

只需使用fork创建一个子进程 - 这将是并发的。从那里您可以使用exec* 系列来执行您喜欢的任何可执行文件,或者在父级返回并提示更多信息(下一个命令等)时在子级中运行 C 代码。在顶部使用带有 WNOHANG 选项的等待循环检查终止的孩子,并再次(这次没有 WNOHANG)在最后检查其余的孩子。

我必须鼓励你不要把它弄得比实际更复杂。用简单的英语(或你的母语,或伪代码)写出你想要的东西,然后用最少的聪明把它翻译成 C。

(伪)代码:

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;
}
于 2012-10-10T17:23:29.623 回答