放置过程中在C背景

放置过程中在C背景

本文介绍了放置过程中在C背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前潜入创造 C &放一个一个后台作业; 。我需要为了这个工作来实现无阻塞 waitpid函数。我知道。另外,我已经很醒目,如果&安培的条件; 在命令行的末尾输入。我只是不知道如何准确地与终端发送的过程是一个后台作业,并实现为执行而另一个提示提示下一个命令。结果
任何有助于在所有的,谢谢你。

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

好了,所以我一直在做这个多一些,我想我可能会开始理解。我仍然有我不知道如何解决,所以我可能会使用GDB什么的一些语法错误,除非有人能指出来。我要对正确的方式还是我完全错了?

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?

推荐答案

听起来你实现一个壳。

只需使用来创建子进程 - 这将是并行。从那里,你可以使用执行任何你喜欢的可执行文件,或者干脆运行C code在孩子,而家长可以追溯到并提示更多的信息(下一个命令等)使用与在在结束这些儿童的其余部分的环以检查终止儿童,并再次(这次没有WNOHANG)顶部WNOHANG选项。

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.

我要鼓励大家不要让这个复杂得多,它确实是。写出你用简单的英语(或者你的母语,或者伪code)想要什么,然后只翻译成下与聪明的最低金额。

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.

(伪)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;
}

这篇关于放置过程中在C背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 11:48