?
Solved

input/ output redirection and pipelining

Posted on 2003-10-22
3
Medium Priority
?
1,036 Views
Last Modified: 2012-05-04
I am writing a mini shell, but I am having trouble with input/ output redirection when it comes to reading the input from one file, execute a command on that input and then redirect the output to another file.

An example would be:

tac <myInput >myOutput

I was able to write the code if there was either one of the two (input or output), but I'm having trouble with the code for using both of them at the same time.

I tried creating a pipe but I was unsuccessful.
This is what I did (the program is pretty big so I will just show the pipe part)
int sd1[2];
int sd2[2];

pipe(sd1);
pipe(sd2);
sd1[0] = open(files[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRWXU);
sd2[1] = open(files[0],O_RDONLY,S_IRUSR|S_IWUSR);

if (fork()==0)
{
             close(sd1[0]);          
             close(sd2[1]);          

             dup2(sd1[1],0);    
             dup2(sd2[0],1);    
                execvp(comm[0],comm); // this is the command line within my shell
}
else if (fork()>0)
{
             close(sd1[0]);              
             close(sd2[1]);
}

Any help would be appreciated.

Thanks
 
0
Comment
Question by:cybercla
[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
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 500 total points
ID: 9602050
I don't think you need a pipe. Pipes are used for two-ended communication, you can't reconnect one end to a file. I think the algorithm should be:

- open the input and output file
- duplicate the opened files to the stdin (0) and stdout (1) file descriptors.
- call exec to execute the new process

You can open the files in the child process (after forking) so you don't need to close them in the parent.

Calling fork twice doesn't seem right. Perhaps you meant:

pid = fork();
if (pid == 0) {

} else if (pid > 0) {

}
0
 

Author Comment

by:cybercla
ID: 9602802
You are right, I didnt need a pipe. I managed to get it to work

int fd1, fd2;
  int dummy;
  char *newargv[2];

  if (fork() == 0) {
    fd1 = open("f1", O_RDONLY);

    if (fd1 < 0) {perror("catf1f2: f1"); exit(1);}
 
    if (dup2(fd1, 0) != 0) {perror("catf1f2: dup2(f1, 0)");exit(1);}

    close(fd1);
 
    fd2 = open("f2", O_WRONLY | O_TRUNC | O_CREAT, 0644);

    if (fd2 < 0) {perror("catf1f2: f2");exit(2);}
 
    if (dup2(fd2, 1) != 1) {perror("catf1f2: dup2(f2, 1)");exit(1);}

    close(fd2);

    newargv[0] = "tac";
    newargv[1] = (char *) 0;

    execvp("tac", newargv);
    perror("execve(bin/cat, newargv, envp)");
    exit(1);  

  } else {wait(&dummy);
  }
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9602901
Looks good to me.

And you do need a pipe if you're going to implement pipelining (eg tac | cat).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

649 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