Solved

input/ output redirection and pipelining

Posted on 2003-10-22
3
1,014 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 125 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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