Page 1 of 1

How to change default stdin to a process?

Posted: Fri May 06, 2011 6:12 am
by sudheer2510
Hi,

I have two processes (one written in tcl (wish process) and the other is a C Program (a.out))
I am opening the second process as a sub process from wish process by using pipe as follows

set pipe [open "| sh -c { $execCommand } " r+ ]

So, when i want to send a message to process 2, i can use "puts $pipe <message>" and the message can be received at process2 at fd 0 . i.e., "read(0,buff,size) in process2"

Now my query is "How to change that default fd 0 at receiving side? i.e., I want to receive the message when I give read((5,buff,size)) on process2???"

Thanks in Advance,
rAzoRbaCK

Re: How to change default stdin to a process?

Posted: Fri May 06, 2011 6:33 am
by Solar
Syntax for moving stdin to a different fd (in your case, 5) is:

Code: Select all

5<&-
Check out "man bash", section "REDIRECTION", subsection "Moving File Descriptors". I am assuming that generic "sh" can do similar things, but you better test yourself.

Note that this closes stdin, i.e. stuff like scanf() wouldn't work anymore. If you want to duplicate stdin, i.e. have it on fd 5 and fd 0, there's a way to do that too, which is documented quite close to the other one in the bash manpage.