[英]Triple pipe in C using pipe(), execlp() and dup()
Consider a new operator '|||'. 考虑一个新的运算符“ |||”。 It will take the output of the previous program and pass it on as input to three different programs.
它将获取先前程序的输出,并将其作为输入传递给三个不同的程序。 Give a program triplepipe.c for implementing the following command: ls –l |
给出一个程序Triplepipe.c以实现以下命令:ls –l | uniq |||
uniq ||| grep ^d, grep ^-, grep ^p.
grep ^ d,grep ^-,grep ^ p。 Don't use popen() library call, system() library call, or temporary files.
不要使用popen()库调用,system()库调用或临时文件。
I am using the following logic to solve first part of the question. 我正在使用以下逻辑来解决问题的第一部分。 'ls -l' is done in child1, 'uniq' in child2 using child1's stdout as stdin and 'grep ^d' in parent using child2's stdout as stdin.
在child1中执行'ls -l',在child2中执行'uniq',使用child1的标准输出作为stdin,在父级中使用'grep ^ d',将child2的标准输出作为stdin。 I am able to see the output.
我能够看到输出。
But I don't know how to give the output from child2 to three separate grep satisfying the limitations set. 但是我不知道如何将child2的输出提供给三个单独的grep来满足限制集。 Can anyone please help?
谁能帮忙吗?
Part of this assignment is basically to implement a 3-way tee
. 这项工作的一部分基本上是实施
tee
。 After setting up the pipes and child processes, you'll have to enter a loop which reads once and writes 3 times, until the read hits EOF. 设置完管道和子进程后,您必须进入一个循环,该循环读取一次并写入3次,直到读取达到EOF。
In Linux there is also a tee
syscall which isn't in your forbidden list. 在Linux中,还有一个
tee
syscall,它不在您的禁止列表中。 It would be perfect for this program, but it's fairly new, so there's a good chance your teacher doesn't know about it yet. 对于该程序而言,它是完美的选择,但是它是一个相当新的东西,因此很有可能您的老师还不知道它。
Quote "Pipes are just files " 引用“管道只是文件”
Think you dup2'ed and currently have your data in a pipe in one of the childs. 认为您dup2'ed并且当前在一个孩子中将数据存储在管道中。 Now just read from the pipe and use Write System call and write to 3 different pipes.
现在只需从管道读取并使用Write System调用并写入3个不同的管道即可。
From there you can process the data differently in 3 different processes 从那里您可以在3个不同的过程中对数据进行不同的处理
Think thats what you require ;) 认为那就是您所需要的;)
Attaching the answer to my question. 附上我的问题的答案。
void main() {
int p1[2]; // pipe between ls | uniq and grep
int p2[2]; // pipt between ls and uniq
int p3[2]; // pipt between ls and uniq
int p4[2]; // pipt between ls and uniq
int p5[2]; // pipt between ls and uniq
char buff[200];
char data[10 * 1024];
int count;
int teelen;
pid_t ret;
pid_t chd;
pipe (p1);
ret = fork();
if (ret == 0) {
pipe (p2);
chd = fork();
if (chd == 0) {
close (1); // close stdout
dup (p2[1]); // dup stdout to p2's out
close (p2[0]); // close p2's in
execlp ("ls", "ls", "-l", NULL); // execute command
exit (0);
}
if (chd > 0) {
close (0); // close stdin
dup (p2[0]); // dup stdin to p2's in
close (p2[1]); // close p2's out
wait(NULL); // wait for child to finish
close (1); // close stdout
dup (p1[1]); // dup stdout to p1's out
close (p1[0]); // close p1's in
execlp ("uniq", "uniq", NULL); // command
exit (0);
}
}
if (ret > 0) {
close (0); // close stdin
dup (p1[0]); // dup stdin to p1's in
close(p1[1]);
wait(NULL); // wait for child to complete
memset (data, '\0', sizeof(data));
do {
memset(buff, '\0', sizeof(buff));
count = read(p1[0], buff, 199);
buff[count] = '\0';
if (count == 0)
break;
strcat (data, buff);
} while(1);
pipe (p3);
close(p1[0]);
write (p3[1], data, sizeof (data));
fsync(p3[1]);
if (fork() == 0) {
int c;
close(0);
dup(p3[0]);
close(p3[1]);
execlp ("grep", "grep", "-a", "^d", NULL);
exit (0);
}
close(p3[1]);
wait(NULL);
pipe (p4);
write (p4[1], data, sizeof (data));
if (fork() == 0) {
close(0);
dup(p4[0]);
close(p4[1]);
execlp ("grep", "grep", "-a", "^-", NULL);
exit (0);
}
close(p4[1]);
wait(NULL);
pipe (p5);
write (p5[1], data, sizeof (data));
if (fork() == 0) {
close(0);
dup(p5[0]);
close(p5[1]);
execlp ("grep", "grep", "-a", "^c", NULL);
exit (0);
}
close(p5[1]);
wait(NULL);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.