Re: Redirecting STDOUT/STDERR of a process forked by a thread

[email protected] (Sreeji K Das)
Newsgroups perl.ithreads
Message-ID <[email protected]>
Okay, I just figured out how to do it.
I did this in the forked child: 
$fh = new FileHandle();
$fh->fdopen(1, 'w');
<now use this $fh for redirection>

(If anybody has similar problem and would like to know
the solution in detail, I'll post the details).

Sreeji

 --- PerlDiscuss - Perl Newsgroups and mailing lists
<[email protected]> wrote: > Hi,
> 
> I'm trying to use threads in one of our
> applications. The application has
> few long running jobs which can be done in parallel
> and I'm thinking
> perl-ithreads can be of use here. 
> My proto-type implementation does something like
> this:
> 
> 1. Open Log main.log
> 2. Redirect STDOUT and STDERR to main.log
> 3. <Some processing>
> 4. Spawn thread #1:
>    4a. Open thread.log and redirect fd of main.log
> to thread.log
>    4b. Redirect STDOUT and STDERR to thread.log
>    4c. Print few messages to STDOUT, STDERR, fd
>    4d. Fork & exec an external program. (Eg.
> system("ls -l ")
> 
> My problem is that all o/p in 4d goes to the
> main.log. (4c goes to
> thread.log) Once a thread is launched, I'd like to
> have all the o/p go to
> thread.log. Once the thread has exited, I'll append
> thread.log to main.log.
> 
> I guess the reason step 4d does not work is this:
> * Once a thread is launched, STDOUT/STDERR ..etc.
> are 'faked' - ie.
> fileno(STDOUT) != 1
> * When I exec() a program, it's STDOUT = 1, STDERR =
> 2. So anything that's
> printed by the exec'd program goes to main.log.
> 
> Has anyone faced similar issues ? Is there anyway I
> can point STDOUT to fd
> 1 inside a thread ?
> I tried
> open(STDOUT, "<&=1") in the thread but got error
> 'Invalid argument' from
> open.
> 
> I can post a complete script example if needed.
> Appreciate any help.
> 
> thanks
> Sreeji
>  

________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping" 
your friends today! Download Messenger Now 
http://uk.messenger.yahoo.com/download/index.html
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.