您也可以制作管道;查看命令是如何构建的并查看结构,它们是以 0 结尾的数组并且引号被剥离:
/* who | awk '{print $1}' | sort | uniq -c | sort -n */
/*static char *cmd0[] = {"who", 0};
static char *cmd1[] = {"awk", "{print $1}", 0};
static char *cmd2[] = {"sort", 0};
static char *cmd3[] = {"uniq", "-c", 0};
static char *cmd4[] = {"sort", "-n", 0};*/
当您创建带有参数的管道时,这里有一些实用函数。它们经过了良好的测试并且没有错误。
管道.c
#define _XOPEN_SOURCE 500
/* One way to create a pipeline of N processes */
#ifndef STDERR_H_INCLUDED
#define STDERR_H_INCLUDED
static void err_sysexit(char const *fmt, ...);
static void err_syswarn(char const *fmt, ...);
#endif /* STDERR_H_INCLUDED */
/* pipeline.c */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include "openshell.h"
#include <errno.h>
/* exec_nth_command() and exec_pipe_command() are mutually recursive */
static void exec_pipe_command(int ncmds, char ***cmds, Pipe output);
/* With the standard output plumbing sorted, execute Nth command */
static void exec_nth_command(int ncmds, char ***cmds) {
assert(ncmds >= 1);
if (ncmds > 1) {
pid_t pid;
Pipe input;
if (pipe(input) != 0)
err_sysexit("Failed to create pipe");
if ((pid = fork()) < 0)
err_sysexit("Failed to fork");
if (pid == 0) {
/* Child */
exec_pipe_command(ncmds - 1, cmds, input);
}
/* Fix standard input to read end of pipe */
dup2(input[0], 0);
close(input[0]);
close(input[1]);
}
execvp(cmds[ncmds - 1][0], cmds[ncmds - 1]);
err_sysexit("Failed to exec %s", cmds[ncmds - 1][0]);
/*NOTREACHED*/
}
/* Given pipe, plumb it to standard output, then execute Nth command */
static void exec_pipe_command(int ncmds, char ***cmds, Pipe output) {
assert(ncmds >= 1);
/* Fix stdout to write end of pipe */
dup2(output[1], 1);
close(output[0]);
close(output[1]);
exec_nth_command(ncmds, cmds);
}
/* who | awk '{print $1}' | sort | uniq -c | sort -n */
/*static char *cmd0[] = {"who", 0};
static char *cmd1[] = {"awk", "{print $1}", 0};
static char *cmd2[] = {"sort", 0};
static char *cmd3[] = {"uniq", "-c", 0};
static char *cmd4[] = {"sort", "-n", 0};*/
/*static char **cmds[] = {cmd0, cmd1, cmd2, cmd3, cmd4};*/
/*static int ncmds = sizeof(cmds) / sizeof(cmds[0]);*/
/* Execute the N commands in the pipeline */
void exec_pipeline(int ncmds, char ***cmds) {
assert(ncmds >= 1);
pid_t pid;
if ((pid = fork()) < 0)
err_syswarn("Failed to fork");
if (pid != 0)
return;
exec_nth_command(ncmds, cmds);
}
#include <stdarg.h>
static const char *arg0 = "<undefined>";
static void err_vsyswarn(char const *fmt, va_list args) {
int errnum = errno;
fprintf(stderr, "%s:%d: ", arg0, (int) getpid());
vfprintf(stderr, fmt, args);
if (errnum != 0)
fprintf(stderr, " (%d: %s)", errnum, strerror(errnum));
putc('\n', stderr);
}
static void err_syswarn(char const *fmt, ...) {
va_list args;
va_start(args, fmt);
err_vsyswarn(fmt, args);
va_end(args);
}
static void err_sysexit(char const *fmt, ...) {
va_list args;
va_start(args, fmt);
err_vsyswarn(fmt, args);
va_end(args);
exit(1);
}
该代码来自先前问题的先前 shell 项目,在该问题中,上述代码获得了赏金。已经有一个详细的答案 - How to fix these errors in my code。C minishell也有较早的答案:添加包含大部分引用代码的管道。
您还可以阅读pipe(2)、pipe(7)和Advanced Linux Programming