[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1039
  • Last Modified:

input/ output redirection and pipelining

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
cybercla
Asked:
cybercla
  • 2
1 Solution
 
mtmikeCommented:
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
 
cyberclaAuthor Commented:
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
 
mtmikeCommented:
Looks good to me.

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

Featured Post

Industry Leaders: 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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now