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 10-27-2005, 10:03 AM   #1
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Rep: Reputation: 30
why does this work?


hiho@ll

excuse for the generalized subject, but i was to uncreative to post it in another way

well my "problem":

i have this code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(){
 close(2);
 printf("TEST");
}
which gets executed this way:
Code:
g++ -o descr.o descr.test.c
./descr.o 2>err.out 1>&2
what does this do?

in the prog itself i only close stderr (2) and write the String TEST to stdout (1)
the commandline redirects
stderr to a file err.out
and
stdout to stderr

what i'm asking myself now is
why does this work?
if i do a
cat err.out

i get
TEST

this means that printf("TEST"); has been written to err.out through stderr, which i closed at the begining of the prog!?

thx@ll
 
Old 10-27-2005, 10:28 AM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

I believe it works because of the following:

1) Shell redirects stderr to err.out
2) Shell closes generic stdout file descriptor, dups current stderr file descriptor and assigns it to stdout

Now when your program begins you still have two different file descriptors, one for stdout and one for stderr and they are both pointing to err.out

3) Your program closes file descriptor for stderr, however stdout file descriptor is still fully functional
4) You program writes text to stdout, since it was going to file err.out the text is placed into the file.

The points to note are that the shell does not point stdout to stderr's file descriptor, it duplicates it thus creating a valid and autonomous new file descriptor for stdout. When you close stderr, you are only closing one instance of the access to your err.out file.

I hope that my explanation is clear enough.
 
Old 10-27-2005, 10:39 AM   #3
Thinking
Member
 
Registered: Oct 2003
Posts: 249

Original Poster
Rep: Reputation: 30
sounds very good!

thx
 
  


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
Does the mounting of a *BSD partition on linux work work well when rw ? kloss *BSD 3 10-08-2005 02:21 PM
Will Ubuntu Work work With my Wireless Card? PulsarSL Ubuntu 3 10-07-2005 10:33 PM
got ALSA to work in root, need help getting it to work in my user profile ic3 Slackware 2 11-28-2003 10:32 PM
FALCONS EYE (nethack) FAILS ON STARTUP, use to work, wont work even after reinstall roorings Linux - Software 0 10-08-2003 10:39 PM
UPS cable didn't work. A cool work-around jlangelier LinuxQuestions.org Member Success Stories 1 07-18-2003 05:04 PM

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

All times are GMT -5. The time now is 07:58 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