This is an assignment so I will not ask specifics.
这是一项任务,所以我不会问具体细节。
For this part of the assignment, I need to use pipes to "pipe" sort -r to ls -l.
对于这部分的赋值,我需要使用管道“管道”排序-r到ls -l。
I know how to do execl, but I can't fathom the idea of what it means to pass "output of an execl" to another execl using pipe.
我知道如何做execl,但我无法理解将“execl的输出”传递给另一个使用管道的execl意味着什么。
Can somebody reccomend some readings on how to do accomplish this?
有人可以推荐一些关于如何做到这一点的读物吗?
I tried googling many different things but I just can't understand it.
我试过谷歌搜索许多不同的东西,但我无法理解它。
I also tried looking at different explanations of pipes but all of them make no sense to me.
我也试着看管道的不同解释,但所有这些对我来说都没有意义。
If somebody can suggest any good readings that can help somebody who has no idea about how pipes work(other than passing things via command line with |) understand pipes, I would appreciate it.
如果有人可以提出任何好的读数,可以帮助那些不知道管道如何工作的人(除了通过命令行通过|)了解管道,我会很感激。
Thanks ahead of time.
提前谢谢。
2 个解决方案
#1
1
Basically you can see a pipeline like a producer and consumer. Look this simple example. In this code, the parent process writes some string in the pipeline and the child process reads it and print it in the output. What you want to do is close to this, the parent expect the first command and the child execute the second. But, you can make it more advanced by making that the child execute more than one command. other example
基本上你可以看到像生产者和消费者这样的管道。看看这个简单的例子。在此代码中,父进程在管道中写入一些字符串,子进程读取它并在输出中打印它。你想要做的就是接近这一点,父母期望第一个命令,孩子执行第二个命令。但是,通过使子进程执行多个命令,可以使其更高级。其他例子
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/*
Read characters from the pipe and echo them to stdout. */
void
read_from_pipe (int file)
{
FILE *stream;
int c;
stream = fdopen (file, "r");
while ((c = fgetc (stream)) != EOF)
putchar (c);
fclose (stream);
}
/* Write some random text to the pipe. */
void
write_to_pipe (int file)
{
FILE *stream;
stream = fdopen (file, "w");
fprintf (stream, "hello, world!\n");
fprintf (stream, "goodbye, world!\n");
fclose (stream);
}
int
main (void)
{
pid_t pid;
int mypipe[2];
/* Create the pipe. */
if (pipe (mypipe))
{
fprintf (stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}
/* Create the child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process.
Close other end first. */
close (mypipe[1]);
read_from_pipe (mypipe[0]);
return EXIT_SUCCESS;
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
else
{
/* This is the parent process.
Close other end first. */
close (mypipe[0]);
write_to_pipe (mypipe[1]);
return EXIT_SUCCESS;
}
}
#2
1
A pair of pipe contains two ends, whatever the program writes into one end end will be received at another end.
一对管道包含两个端点,无论程序写入一个端到端都将在另一端接收。
Your duty is make ls -l
write to an end, and sort -r
read from another end.
你的职责是让ls -l写到最后,然后排序 - 从另一端读取。
int p[2];
pipe(p);
if (fork() == 0) {
// first child, run ls, so its output(fd 1) should be redirected to a pipe end
dup2(p[0], 1);
close(p[0]); // just for safety
close(p[1]); // just for safety
execvl("ls", ...);
}
if (fork() == 0) {
// second child, run sort, so its input(fd 0) should be redirected to another pipe end
dup2(p[1], 0);
close(p[0]); // just for safety
close(p[1]); // just for safety
execvl("sort", ...);
}
close(p[0]);
close(p[1]);
#1
1
Basically you can see a pipeline like a producer and consumer. Look this simple example. In this code, the parent process writes some string in the pipeline and the child process reads it and print it in the output. What you want to do is close to this, the parent expect the first command and the child execute the second. But, you can make it more advanced by making that the child execute more than one command. other example
基本上你可以看到像生产者和消费者这样的管道。看看这个简单的例子。在此代码中,父进程在管道中写入一些字符串,子进程读取它并在输出中打印它。你想要做的就是接近这一点,父母期望第一个命令,孩子执行第二个命令。但是,通过使子进程执行多个命令,可以使其更高级。其他例子
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/*
Read characters from the pipe and echo them to stdout. */
void
read_from_pipe (int file)
{
FILE *stream;
int c;
stream = fdopen (file, "r");
while ((c = fgetc (stream)) != EOF)
putchar (c);
fclose (stream);
}
/* Write some random text to the pipe. */
void
write_to_pipe (int file)
{
FILE *stream;
stream = fdopen (file, "w");
fprintf (stream, "hello, world!\n");
fprintf (stream, "goodbye, world!\n");
fclose (stream);
}
int
main (void)
{
pid_t pid;
int mypipe[2];
/* Create the pipe. */
if (pipe (mypipe))
{
fprintf (stderr, "Pipe failed.\n");
return EXIT_FAILURE;
}
/* Create the child process. */
pid = fork ();
if (pid == (pid_t) 0)
{
/* This is the child process.
Close other end first. */
close (mypipe[1]);
read_from_pipe (mypipe[0]);
return EXIT_SUCCESS;
}
else if (pid < (pid_t) 0)
{
/* The fork failed. */
fprintf (stderr, "Fork failed.\n");
return EXIT_FAILURE;
}
else
{
/* This is the parent process.
Close other end first. */
close (mypipe[0]);
write_to_pipe (mypipe[1]);
return EXIT_SUCCESS;
}
}
#2
1
A pair of pipe contains two ends, whatever the program writes into one end end will be received at another end.
一对管道包含两个端点,无论程序写入一个端到端都将在另一端接收。
Your duty is make ls -l
write to an end, and sort -r
read from another end.
你的职责是让ls -l写到最后,然后排序 - 从另一端读取。
int p[2];
pipe(p);
if (fork() == 0) {
// first child, run ls, so its output(fd 1) should be redirected to a pipe end
dup2(p[0], 1);
close(p[0]); // just for safety
close(p[1]); // just for safety
execvl("ls", ...);
}
if (fork() == 0) {
// second child, run sort, so its input(fd 0) should be redirected to another pipe end
dup2(p[1], 0);
close(p[0]); // just for safety
close(p[1]); // just for safety
execvl("sort", ...);
}
close(p[0]);
close(p[1]);