How to change default stdin to a process?

Programming, for all ages and all languages.
Post Reply
sudheer2510
Posts: 3
Joined: Mon May 02, 2011 11:57 pm

How to change default stdin to a process?

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: How to change default stdin to a process?

Post 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.
Every good solution is obvious once you've found it.
Post Reply