Redirecting input from a file
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Redirecting input from a file




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Redirecting input from a file  
Ramon F Herrera


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-07 06:25 AM


I'd like to review my notes, to perform a sanity check, if you will.

I have programmed many cases in which one program sends its output to
another program. In these cases, since at a given time a process needs
to be alive producing the data while the other one is consuming it, we
need to create a pipe. The code goes like this:

pipe(pipefd);

if (fork()) {
close(pipefd[READ]);
close(STDOUT);
dup(pipefd[WRITE]);
}
else {
close(pipefd[WRITE]);
close(STDIN);
dup(pipefd[READ]);
execv(program, arguments);
}

printf("This goes through the pipe to the child\n");


But my question is actually about redirecting the input from a file.
In that case, the pipe should be unnecessary, because the file in
question remains accessible, right? So the code should be something
like:

fd = open("somedata.txt", "r");
close(STDIN);
dup(fd);
execv(program, arguments);

The redirecting is not working for me, so I guess there is something
wrong with my reasoning.

In other words, how does a shell perform something like this:

% program < somedata.txt

-Ramon






[ Post a follow-up to this message ]



    Re: Redirecting input from a file  
Logan Shaw


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-15-07 06:25 AM

Ramon F Herrera wrote:
> But my question is actually about redirecting the input from a file.
> In that case, the pipe should be unnecessary, because the file in
> question remains accessible, right? So the code should be something
> like:
>
>       fd = open("somedata.txt", "r");
>       close(STDIN);
>       dup(fd);
>       execv(program, arguments);
>
> The redirecting is not working for me, so I guess there is something
> wrong with my reasoning.
>
> In other words, how does a shell perform something like this:
>
> % program < somedata.txt

I ran this command on Solaris 8 and looked at the output:

truss -f sh -c 'cat < /etc/resolv.conf'

The trace of the system calls shows that it does an open(),
which returns 3, followed by a close(0) and a fnctl(3, F_DUPFD, 0),
which I believe is exactly equivalent to a dup(), and then
finally an execve().

So I think your general theory is right.  I guess my first
question is whether you're really using open().  Your example
gives very fopen()-like arguments, and fopen() and open() are
very different beasts, returning very different types.

If that's not it (and you were just taking a bit of poetic
license), have you thought about using dup2() instead, in order
to guarantee that you are putting the opened file into fd 0
as intended?

- Logan





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:06 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register