Solved

input/ output redirection and pipelining

Posted on 2003-10-22
3
993 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux  : how to view CPU info and  its usage details 7 248
understanding an rpm spec file 5 54
automating commands in linux on start up 2 45
Linux 10 106
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now