小编s.g*_*ang的帖子

同时分叉和执行多个流程

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

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

ls ; echo hello ; cat shell.c
Run Code Online (Sandbox Code Playgroud)

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

详细地说,我想解释一下我的程序如何工作:

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.
Run Code Online (Sandbox Code Playgroud)

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

int launch(char …
Run Code Online (Sandbox Code Playgroud)

c linux shell fork exec

1
推荐指数
1
解决办法
3799
查看次数

标签 统计

c ×1

exec ×1

fork ×1

linux ×1

shell ×1