Solved

input/ output redirection and pipelining

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Linux centos 5.7 3 94
benchmark write speed to disk on linux 2 70
How to trace the cause of Out Of Memory issues 8 74
awk file 6 78
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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