gpt4 book ai didi

在C中创建多个管道

转载 作者:行者123 更新时间:2023-11-30 17:02:13 24 4
gpt4 key购买 nike

我正在编写一个简单的 shell,我想更改我的程序以添加多个管道命令的可能性,例如“echo foo | cat | cat | cat | cat | wc”。我已经为两个命令编写了,但我不能为多个命令编写。

这是我的程序的源代码:

if (pid == 0) // in the child process 
{
for (i = 0; i < command; i++) // for each cmd
{
if (argv[i][0] == '|')
{
j = i;
}
}
if (j > 0)
{
if (pipe(p))
{
fprintf(stderr, "pipe");
exit(1);
}
argv[j] = NULL;
if (fork() == 0) // child
{
j = -1;
close(p[0]);
dup2(p[1],1);
close(p[1]);
}

// parent
close(p[1]);
dup2(p[0], 0);
close(p[0]);
}
for (i = 0; dirs[i] != 0; i++)
{
snprintf(pathname, sizeof(pathname), "%s/%s", dirs[i], argv[j+1]);
execv(pathname, &argv[j+1]);
}
}
else
{
while (wait(0) != pid) // parent: wait child
}

提前感谢您的帮助。

最佳答案

我举了一个你正在尝试做的事情的例子。我使用常量作为命令,我把命令行解析留给你。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>

static char *my_command0[] = {"cat", "stackoverflow.c", NULL};
static char *my_command1[] = {"grep", "return", NULL};
static char *my_command2[] = {"sed", "s/^ *//g", NULL};

static char **const my_commands[] = {
my_command0,
my_command1,
my_command2,
NULL
};

int create_sub_process(char *const command[], int input_stream)
{
int pipefd[2] = {-1, -1};
pid_t fk;

if (pipe(pipefd) < 0)
{
perror("pipe");
close(input_stream);
return -1;
}

if ((fk = fork()) < 0)
{
perror("fork");
close(pipefd[0]);
close(pipefd[1]);
close(input_stream);
return -1;
}

if (fk == 0)
{
close(pipefd[0]);

close(0);
dup(input_stream);
close(input_stream);

close(1);
dup(pipefd[1]);
close(pipefd[1]);

execvp(command[0], command);
perror("execvp");
exit(1);
}

close(input_stream);
close(pipefd[1]);

return pipefd[0];
}

int main()
{
int fd = dup(0);

for (int i = 0; my_commands[i] != NULL; i++)
{
fd = create_sub_process(my_commands[i], fd); // replace my_commands[i] by whatever you need to execute - check: man execvp
if (fd < 0)
{
exit(1);
}
}

// Also adapt the following lines to whatever you want to do with last child results
close(0);
dup(fd);
close(fd);

execlp("cat", "cat", (char *)NULL);
perror("execlp");

return 1;
}

create_sub_process() 创建一个管道并创建一个子进程来执行给定的命令,从给定的输入流获取输入并将输出发送到它返回父级的流。

关于在C中创建多个管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36662054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com