c程序用管道来执行“ps aux | grep firefox | tee processes.txt”

时间:2013-09-03 00:34:46

标签: c linux pipe

这是我的第一个问题。希望它不会是dumm。我有一个问题,编写一个生成“ps aux | grep firefox | tee processes.txt”的程序。我成功使用了像“ps aux | grep firefox”这样的1个管道,但是当我试图概括时我遇到了问题。我的意图是使用管道来理解它们。我知道开放是可能的。任何提示都会很棒!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h> 
#include <sys/wait.h>
#include <sys/types.h>
int main(void)
{
    char *** args = NULL;
    pid_t fork_id = -1;
    int ** pipes = NULL;
    int status = 0;
    int i = 0, k = 0;
    int check = 0;
    args = calloc(3, sizeof(char **));
    args [0] = calloc(3, sizeof(char *));
    args [1] = calloc(3, sizeof(char *));
    args [2] = calloc(3, sizeof(char *));
    args[0][0]= "ps";
    args[0][1]="aux";
    args[0][2]= NULL;
    args[1][0]= "grep";
    args[1][1]= "firefox";
    args[1][2]= NULL;
    args[2][0]= "tee";
    args[2][1]= "processes.txt";
    args[2][2]= NULL;

    pipes=calloc(2,sizeof(int *));
    for(i=0;i<2;++i){
        pipes[i]=calloc(2,sizeof(int));
    }
    for(i=0;i<2;++i){
        pipes[i]=calloc(2,sizeof(int));
        check=pipe(pipes[i]);
        if(check<0){
            perror("pipe");
            exit(EXIT_FAILURE);
        }
    }
    for(i=0;i<3;++i){
        if ((fork_id = fork()) < 0) {
            perror("fork()");
            exit(1);
        }
        if ((i ==0)&& (fork_id == 0)){ 
        close(pipes[i][0]);
        close(1);
        dup(pipes[i][1]);
        close(pipes[i][1]);
        execvp(args[i][0], args[0]);
        } else if ((i!= 0)&&(i  != 2) && (fork_id == 0)){
            close(0);
            dup(pipes[i][0]);
            close(pipes[i][0]);
            close(1);
            dup(pipes[i][1]);
            close(pipes[i][1]);
            execvp(args[i][0], args[i]);
        } else if ((i==2)&&(fork_id != 0)){
            close(pipes[i-1][1]);
            close(0);
            dup(pipes[i-1][0]);
            close(pipes[i-1][0]);
            execvp(args[i][0], args[i]);
        }
        wait(&status);
    }
    for(i=0;i<2;++i){
        for(k=0;k<2;++k){
            check=close(pipes[i][k]);
            if(check<0){
                perror("close pipe");
                    exit(EXIT_FAILURE);
            }
        }
    }
    return(0);
}

THX !!

3 个答案:

答案 0 :(得分:1)

这可能是您正在寻找的 - 不需要所有那些高管和叉子和管道:

#include <stdio.h>                                                              
#include <stdlib.h>

int main() {
  FILE *p; 
  int status;
  char line[1024];

  p = popen("ps aux | grep firefox | tee processes.txt", "r");
  if (!p) {
    fprintf(stderr, "Error.");
    exit(1);
  }

  while (fgets(line, sizeof(line) - 1, p)) {
    puts(line);
  }
  pclose(p);
}

答案 1 :(得分:0)

Popen可能对您感兴趣。

FILE *p = popen("ps aux | grep firefox | tee processes.txt", "r");

答案 2 :(得分:0)

SO 13636252的公认解决方案可以轻松适应这个管道:

/*
** How to create a pipeline of N processes?
** SO 18582446: ps aux | grep firefox | tee processes.txt
** Adaptation of accepted answer to:
** SO 13636252 C Minishell adding pipelines
*/

/* stderr.h */
#ifndef STDERR_H_INCLUDED
#define STDERR_H_INCLUDED

static void err_setarg0(const char *argv0);
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 "stderr.h"*/

typedef int Pipe[2];

/* Now: ps aux | grep firefox | tee processes.txt */
/* Was: who | awk '{print $1}' | sort | uniq -c | sort -n */
static char *cmd0[] = { "ps",   "aux",           0 };
static char *cmd1[] = { "grep", "firefox",       0 };
static char *cmd2[] = { "tee",  "processes.txt", 0 };

static char **cmds[] = { cmd0, cmd1, cmd2, };
static int   ncmds = sizeof(cmds) / sizeof(cmds[0]);

/* 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);
}

/* Execute the N commands in the pipeline */
static 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);
}

/* Collect dead children until there are none left */
static void corpse_collector(void)
{
    pid_t parent = getpid();
    pid_t corpse;
    int   status;
    while ((corpse = waitpid(0, &status, 0)) != -1)
    {
        fprintf(stderr, "%d: child %d status 0x%.4X\n",
                (int)parent, (int)corpse, status);
    }
}

static void exec_arguments(int argc, char **argv)
{
    /* Split the command line into sequences of arguments */
    /* Break at pipe symbols as arguments on their own */
    char **cmdv[argc/2];            // Way too many
    char  *args[argc+1];
    int cmdn = 0;
    int argn = 0;

    cmdv[cmdn++] = &args[argn];
    for (int i = 1; i < argc; i++)
    {
        char *arg = argv[i];
        if (strcmp(arg, "|") == 0)
        {
            if (i == 1)
                err_sysexit("Syntax error: pipe before any command");
            if (args[argn-1] == 0)
                err_sysexit("Syntax error: two pipes with no command between");
            arg = 0;
        }
        args[argn++] = arg;
        if (arg == 0)
            cmdv[cmdn++] = &args[argn];
    }
    if (args[argn-1] == 0)
        err_sysexit("Syntax error: pipe with no command following");
    args[argn] = 0;
    exec_pipeline(cmdn, cmdv);
}

#include <signal.h>

typedef void (*SigHandler)(int signum);

static void sigchld_status(void)
{
    const char *handling = "Handler";
    SigHandler sigchld = signal(SIGCHLD, SIG_IGN);
    signal(SIGCHLD, sigchld);
    if (sigchld == SIG_IGN)
        handling = "Ignored";
    else if (sigchld == SIG_DFL)
        handling = "Default";
    printf("SIGCHLD set to %s\n", handling);
}

int main(int argc, char **argv)
{
    err_setarg0(argv[0]);
    sigchld_status();
    if (argc == 1)
    {
        /* Run the built in pipe-line */
        exec_pipeline(ncmds, cmds); 
    }
    else
    {
        /* Run command line specified by user */
        exec_arguments(argc, argv);
    }
    corpse_collector();
    return(0);
}

/* stderr.c */
/*#include "stderr.h"*/
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

static const char *arg0 = "<undefined>";

static void err_setarg0(const char *argv0)
{
    arg0 = argv0;
}

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);
}