gpt4 book ai didi

c - C中的多级管道

转载 作者:太空宇宙 更新时间:2023-11-04 04:10:05 25 4
gpt4 key购买 nike

我必须在 C 中创建一个多级管道,它像 Unix 控制台一样解释 Linux 命令。我的代码适用于 2 级管道,但我必须实现更多(最多 16 个)。这里的主要问题是我不确定如何获取前两个命​​令的输出,然后将其重新路由为第三个命令的输入(其他级别等)。我知道我需要使用第一个管道的“1”和第二个管道的“0”,因为它们分别是标准输出和标准输入,但我不确定如何在实践中实现这一点。

// tokenize is a separate function that uses strtok repeatedly on cmdline, setting the array 
// segments to the strings separated by | and numTokens to the number of commands
char* segments[MAX_PIPE_SEGMENTS];
int x = 0;
int* numTokens = &x;
tokenize(segments, cmdline, numTokens, "|");

// the code for one command
if (*numTokens == 1) {
pid_t pid = fork();
if (pid == 0) {
char* strings[MAX_SEGMENT_LENGTH];
int y = 0;
int* numStrings = &y;
tokenize(strings, segments[0], numStrings, " ");
execvp(strings[0], strings);
} else {
wait(0);
return;
}
}

pid_t pid = fork();
if (pid == 0) {
int i = 0;
for (i = 0; i < *numTokens-1; i++) {
pid_t pid2 = fork();
if (pid2 == 0) {
int ps[2];
pipe(ps);
pid_t pid3 = fork();
if (pid3 == 0) {
close(1);
dup2(ps[1], 1);
close(ps[0]);
char* strings[MAX_SEGMENT_LENGTH];
int y = 0;
int* numStrings = &y;
tokenize(strings, segments[i], numStrings, " ");
execvp(strings[0], strings);
} else {
close(0);
dup2(ps[0], 0);
close(ps[1]);
wait(0);
char* strings[MAX_SEGMENT_LENGTH];
int y = 0;
int* numStrings = &y;
tokenize(strings, segments[i+1], numStrings, " ");
execvp(strings[0], strings);
}
} else {
wait(0);
}
}
} else {
wait(0);
return;
}

最佳答案

你看,在不同的 if/else block 中处理第一条和第二条命令不能推广到多于两个的命令。可行的是尽可能将管道命令和操作相同地对待,只为第一个和最后一个添加特殊条件:

    int i, in, out = dup(1);    // save standard output descriptor
for (i = 0; i < x; i++)
{
int ps[2];
if (i < x-1) pipe(ps); // if not last in line, make a pipe
pid_t pid = fork();
if (pid == 0)
{
// if not first in line, connect standard input to pipe
if (i) dup2(in, 0), close(in);
// if not last in line, connect standard output to pipe
if (i < x-1) dup2(ps[1], 1), close(ps[1]);
// if last in line, restore standard output to original
else dup2(out, 1), close(out);
char* strings[MAX_SEGMENT_LENGTH];
int y = 0;
int* numStrings = &y;
tokenize(strings, segments[i], numStrings, " ");
execvp(strings[0], strings);
exit(1);
}
if (i) close(in);
close(ps[1]);
in = ps[0]; // the current pipe's read end is the new input
}
close(out);
do ; while (wait(0) > 0);

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

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