ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Mandrake Linux (9.1) , Red Hat Linux (8)
Posts: 32
Rep:
File Descriptors during I/O Redirection
Hi.
I am reading the UNIX Time Sharing paper by Ritchie and Thompson. There is a section (Implementation of a shell) that I do not understand.
What I get from it is that the shell is a process that keeps reading for new commands. Once it has received one it spawns a new process (using fork()) and that new process calls execute. So far so good. However, it also states that if an argument with > or < is given, then the offspring changes the standard file descriptors (0,1) to refer to the named file, by closing the necessary file (0 or 1) and opening the new file instead. Since both processes will share the file descriptor table, this should work.
However, this is what I don't understand: Once the son has closed a file and opened the new file (for example a command executed by the shell), how will the shell reopen the file descriptor for the input,output files that it previously had? It says here that "the shell need not know the actual names of the files (...) because it need never reopen them". However, why should it not need to reopen them if it closed them before executing the child and needs to keep accessing those files to continue running commands?
After fork() there are two processes, each with separate descriptor table. So if one copy closes some of the descriptors, the other one is unaffected. It doesn't need to reopen them, because they have never been closed from its perspective.
Note also the difference between file descriptors ie I/O channel nums (0=stdin, 1=sdtout, 2=stderr) and actual filenames.
0,1,2 are default setup for each shell as it starts, but you can use higher nums to open other streams/files at the same time, iirc at least as high as 9.
This is a good page: http://www.tldp.org/LDP/abs/html/io-redirection.html
My apologies if you already knew this stuff, but might be handy for others.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.