Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Redirecting outputs, Forking/Piping

Posted on 2003-11-21
3
Medium Priority
?
351 Views
Last Modified: 2010-04-15
Hi,
First time playing around with forks and pipes.  Basically say if i have someone input a command "command1 P command2 | command3".   In this case, the key value P will direct the standard error of command1 to standard in of command2 and standard output of command1 directed to the pipe '|' after command2.  I know i will need to create a new pipe, fork it and get the input from parent thru fd 2 somehow.  Thanks.
0
Comment
Question by:aomega
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 9801638
#include <stdio.h>
#include <unistd.h>
char *cmd0[]={"date","-u",0};
char *cmd1[]={"tr"," ","\n",0};
char *cmd2[]={"cat","-ve",0};
char *cmd3[]={"tr","$","_",0};
char **cmd[]={
  (char **)&cmd0,
  (char **)&cmd1,
  (char **)&cmd2,
  (char **)&cmd3,
};

#define N (sizeof(cmd)/sizeof(*cmd))
main(){
    double s0,s1;
    int pid[N];
    int fildes[N-1][2];
    int i;
    for( i=0; i<N; i+=1 ){
        if( i < N-1 ){ pipe(fildes[i]); }
        if( (pid[i]=fork())>0 ){
            if( i > 0 ){
                close(fildes[i-1][1]);
                close(fildes[i-1][0]);
             }
        }else if( pid[i]<0 ){
            perror("fork"); exit(1);
        }else{
            if( i>0 ){
              dup2(fildes[i-1][0],fileno(stdin));
              close(fildes[i-1][1]);
            }
            if( i<N-1 ){
              dup2(fildes[i][1],fileno(stdout));
              close(fildes[i][0]);
            }
            execvp(cmd[i][0],cmd[i]);
            perror(cmd[i][0]); exit(1);
        }
    }
    for( i=0; i<N; i+=1 ){
        int s;
        wait(&s);
    }
}
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question