UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-09-16 02:25 AM

johndela1
Member
Registered: 2008-09-15
Posts: 4

Re: read and write to process from C

I need to be able to open a process (like a lua interpreter or bash shell) and pipe it
a script, then I need to read the output.

I know popen can only do r or w, not both.  I don't want to redirect to the fs if I can avoid it.

any ideas on how to do this?

Offline

#2 2008-09-16 04:50 AM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: read and write to process from C

Offline

#3 2008-09-16 12:45 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: read and write to process from C

Yes, the usual approach is to run the process in a pseudo-tty, so forkpty() or
exp_popen() (from libexpect) or doing all the low-level pty work yourself will do it...

OR, if you don't actually need a full-blown pseudo-tty, another approach I've used
in the past is to simply create a socketpair() of Unix domain sockets, swap in one
end as the stdin/stdout/stderr of the newly spawned child process, and have the
parent use the other end to communicate with it in both directions (sending it input,
and reading its output)...  This works just fine for many apps...  But, some may be
happier having a real pty underneath, so it does depend on what you're running...
If you want the most generally usable approach for running anything, probably best
to go with a pty...

Offline

Board footer

Powered by FluxBB