async system()?

6 views
Skip to first unread message

AJ ONeal

unread,
Dec 19, 2010, 4:53:05 PM12/19/10
to uta...@googlegroups.com
How do I make a system call without waiting for it to finish?

I'm looking for something like

int pid = async_system(char*);

which I could then use with, say, libev's ev_child.


AJ ONeal

Shawn

unread,
Dec 19, 2010, 5:22:04 PM12/19/10
to Utah C Users Group
On Dec 19, 2:53 pm, AJ ONeal <coola...@gmail.com> wrote:
> How do I make a system call without waiting for it to finish?

The normal approach is to use fork() to create the child process and
then the appropriate member of the exec*() family of functions to load
the appropriate program for the child.

If you need to do I/O with the child process, use popen(). It does
the fork and exec for you (actually it invokes a shell and has it
execute the string you provide), and returns a file handle that
represents stdin/stdout of the child process.

There are libraries that provide higher-level APIs for starting and
managing child processes, but fork(), exec*() and popen() are all
POSIX and therefore portable.

AJ ONeal

unread,
Dec 19, 2010, 5:30:10 PM12/19/10
to uta...@googlegroups.com
I haven't used exec before, but I'll try it out tomorrow and post back with a simple example if no-one else does first.

That was enough information to get me started down the right path.

AJ ONeal

http://en.wikipedia.org/wiki/Fork-exec
http://en.wikipedia.org/wiki/Exec_(operating_system)



On Sun, Dec 19, 2010 at 3:14 PM, Shawn Willden <shawnw...@gmail.com> wrote:
(I tried to reply to the list, but my messages keep bouncing.  I have to look into that.)

On Sun, Dec 19, 2010 at 2:53 PM, AJ ONeal <cool...@gmail.com> wrote:
How do I make a system call without waiting for it to finish?

The normal approach is to use fork() to create the child process and then the appropriate member of the exec*() family of functions to load the appropriate program for the child.

If you need to do I/O with the child process, use popen().  It does the fork and exec for you (actually it invokes a shell and has it execute the string you provide), and returns a file handle that represents stdin/stdout of the child process.

There are libraries that provide higher-level APIs for starting and managing child processes, but fork(), exec*() and popen() are all POSIX and therefore portable.

-- 
Shawn


Shawn

unread,
Dec 19, 2010, 5:49:51 PM12/19/10
to Utah C Users Group
On Dec 19, 3:30 pm, AJ ONeal <coola...@gmail.com> wrote:
> I haven't used exec before, but I'll try it out tomorrow and post back with
> a simple example if no-one else does first.

There are some good examples at http://www.osix.net/modules/article/?id=641

It's pretty straightforward once you get your head around the idea of
the "duplicate myself, then replace one of me" approach to creating
child processes.

Alex Esplin

unread,
Dec 20, 2010, 2:11:31 PM12/20/10
to uta...@googlegroups.com
On Sun, Dec 19, 2010 at 15:22, Shawn <shawnw...@gmail.com> wrote:
> If you need to do I/O with the child process, use popen().  It does
> the fork and exec for you (actually it invokes a shell and has it
> execute the string you provide), and returns a file handle that
> represents stdin/stdout of the child process.

+1 for popen(). I've used it a lot and it's _incredibly_ handy.

--
Alex Esplin

Reply all
Reply to author
Forward
0 new messages