同时分叉和执行多个流程 [英] Fork and exec multiple processes simultaneously

查看:49
本文介绍了同时分叉和执行多个流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个自制的外壳(非常简单的外壳).我决定采用使用execvp的方法,因为我的路径对于我的shell来说是不可更改的元素.我遇到了一个关于如何一次分叉和执行多个进程的逻辑问题.

I am working on a home made shell (very simple shell). I have decided to take the route of using execvp as my path is not a changeable element for my shell. I am running into an issue with coming up with the logic on how to fork and exec multiple processes at once.

我的程序应使用如下命令:

My program should work with a command as such:

ls ; echo hello ; cat shell.c

其中每个;"表示我们希望同时运行这些过程.因此,在终端输出中,我们应该同时使用这些命令.

Where each ";" indicates that we would like to run these processes at once simultaneously. So on our terminal output we should get a mix of these commands working at once.

为了详细说明,我想解释一下我的程序是如何工作的:

To elaborate I'd like to explain how my program works:

A. Intake full command line into char array with a grab line function
B. Split the char array received from the last function by delimiters and place into an array of char arrays (pointer to pointer).
C. If one of the elements in our array of char arrays is ";" we can assume that multi commands are necessary. This is where I have trouble.

我已经知道到底需要分叉多少个进程,但是似乎无法将所有这些函数及其参数立即传递给execvp函数.我应该使用临时数组吗?我知道这不应该那么复杂,但是由于某种原因我无法弄清楚.我将在下面提交启动函数,该函数将获取一个char数组并根据我的"multiCommand"变量相应地执行该变量,该变量在需要多个命令时设置(通过我的分割线函数)

I have gotten as far as to know exactly how many processes I need to fork and such, but I cannot seem to wrap my head around how to pass all of these functions plus their arguments to the execvp function at once. Should I use a temp array? I know this shouldn't be this complicated but for some reason I cannot figure it out. I'm submitting my launch function below, which intakes an array of char arrays and executes accordingly based on my "multiCommand" variable which is set when multi commands are needed (by my split line function)

int launch(char **args){

    pid_t pid;
    int status;
    int i = 0;

    if(strcmp(args[0], "quit") == 0){
        exit(EXIT_SUCCESS);
    }

    if(strcmp(args[0], ";") != 0){
        printf("Essential Command Found : %s\n", args[0]);
        numFork++;
    }


    if(multiCommand == 1){
        //Handle Multicommands here
        printf("Multi Commands Handling Here\n");

        for(; i < elements - 1; i++){
            if(strcmp(args[i], ";") == 0){
                if((i + 1) < elements){
                    printf("Essential Command Found : %s\n", args[i + 1]);
                    numFork++;
                }
            }
        }

        //This is where I need to figure out what to do

        printf("Fork: %d times\n", numFork);


    }else if (multiCommand == 0){
        pid = fork();
        if(pid == 0){
            execvp(args[0], args);

        }else{
            wait(&status);
        }
    }

    multiCommand = 0;   
    elements = 0;

    return 1;
}

推荐答案

通常的想法是对不同的命令进行for循环,然后对每个命令进行分叉.

The general idea would be to have a for loop over the different commands and fork each of them.

例如

for(int i = 0; i < commandCount; i++) {
    int pid = fork();
    if(pid == 0) { //this is the child (don't forget to check for errors and what-not)
        execCommand(all, of, the, info, needed);
    }
}

您可以使用 strtok()轻松获得不同的命令.
这是一个示例:

You can easily get the different commands using strtok().
Here's an example:

#include <string.h>
#include <stdio.h>
int main() {
    char input[] = "abc;def;ghi";
    char *token = strtok(input, ";");
    while(token != NULL) {
        printf("%s\n", token);
        token = strtok(NULL, ";");
    }
    return 0;
}

输出:

abc
def
ghi


最终功能将如下所示:


The final function will look something like this:

char *token = strtok(input, ";");
while(token != NULL) {
    int pid = fork();
    if(pid == 0) {
        //token is one command
        //parse the different parts here
        execCommand(args, to, exec);
    }
    token = strtok(NULL, ";");
}

这篇关于同时分叉和执行多个流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆