2016-09-14 3 views
1

C로 파이핑하기 위해 자체 함수를 작성하려고합니다. 먼저 아이가 포크를 잡고 파이프에 입력 된 위치를 확인합니다. 나는 (char) cmd1 [64] (이전에 초기화 된)과 char2 (char2) [64] 로의 토큰을 복사 한 후 자신의 StringArray를 반복했다. int 파이핑에는 다음 단계의 값이 제공됩니다.내 자신의 파이핑 함수를 C로 작성하기

if(strcmp(sa[i], "|") == 0) 
     { 
      printf("got to the pipe\n"); 
      sa[i]=NULL; 
      strcpy(cmd1, sa[i-1]); 
      strcpy(cmd2, sa[i+1]); 
      piping=2; 
     } 

그런 다음 프로그램이이 문에 도달 :

if (piping) 
{ 
    if (pipe(fd) != 0){ 
     perror("Failed to create pipe"); 
    } 
    if ((cpPid = fork()) == -1){ 
     perror("Failed to fork"); 
    } 
    if (cpPid == 0){ 
     dup2(fd[1], 1); 
     close(fd[0]); 
     close(fd[1]); 
     execvp(cmd1, sa); 
     error("failed to exec command 1"); 
    } 
    else 
    { 
     dup2(fd[0], 0); 
     close(fd[0]); 
     close(fd[1]); 
     execvp(cmd2, sa); 
     error("failed to exec command 2"); 
    } 
} 

내 프로그램을 완전히 충돌하고 사람 만 잘못 무슨 일이 일어나고 있는지 알아 내기 위해 좀 도와 줄래 알 수없는 오류 10275024.을 준다?

+4

없다. –

+0

나는 숙제를 위해 똑같은 것을 만들었고, 이것은 나를 도왔다 http://stackoverflow.com/questions/948221/does-this-multiple-pipes-code-in-c-makes-sense –

+0

@EugeneSh. MCVE 란 무엇입니까? –

답변

0

당신이 C.

를 사용하여 파이프 라인을 실행 도움이 될 것입니다이 작업 예에서 참조하시기 바랍니다
#include <assert.h> 
#include <stdio.h> 
#include <sys/wait.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <memory.h> 
#include <errno.h> 

typedef int Pipe[2]; 

/* exec_nth_command() and exec_pipe_command() are mutually recursive */ 
static void exec_pipe_command(int ncmds, char ***cmds, Pipe output); 

static void err_vsyswarn(char const *fmt, va_list args) { 
    int errnum = errno; 
    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; 
    err_vsyswarn(fmt, args);; 
} 

static void err_sysexit(char const *fmt, ...) { 
    va_list args; 
    err_vsyswarn(fmt, args); 
    exit(1); 
} 

/* 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*/ 
} 
/* exec_nth_command() and exec_pipe_command() are mutually recursive */ 
/* 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); 
} 

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

int main(int argc, char **argv) { 
    exec_pipeline(ncmds, cmds); 
    return (0); 
} 

코드에서 파이프 라인의 예는

who | awk '{print $1}' | sort | uniq -c | sort -n

당신은 어떤 파이프 라인을 사용할 수있다 .

시험 : 말할 것도 MCVE없이

[email protected] ~/C/> 
who | awk '{print $1}' | sort | uniq -c | sort -n 
     1 dac 
[email protected] ~/C/> gcc main.c 
[email protected] ~/C/> ./a.out 
    1 dac 
관련 문제