LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2008, 03:42 PM   #1
joseamuniz
Member
 
Registered: May 2003
Distribution: Mandrake Linux (9.1) , Red Hat Linux (8)
Posts: 32

Rep: Reputation: 15
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?

Thank you very much!
 
Old 02-27-2008, 04:02 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 02-27-2008, 04:54 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing File Descriptors Geoffrey_Firmin Slackware 5 04-21-2008 09:48 AM
File Descriptors and Ulimits ALInux Linux - Software 1 07-25-2007 06:46 AM
file descriptors node047 Linux - Newbie 1 03-29-2005 05:47 AM
unix file descriptors versus c FILE pointers nodger Programming 9 11-25-2004 07:02 AM
BASH scripting: confused about redirection & file descriptors funkymunky Programming 1 06-07-2004 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration