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.

  • Index
  • » C
  • » how to call an executable with args passed in Thread function

#1 2008-01-03 07:37 AM

Raj Kumar Arora
Member
Registered: 2008-01-03
Posts: 7

Re: how to call an executable with args passed in Thread function

Hi,

For one of my modules, I am using  Multithreading. I am creating two threads.

I needed to call an executable with some arguments in the function body for second thread. For that I am using system() call.

The problem is that :-
1. system() function call is not allowing any further arguments to executable being called as execl() would if I was using a fork()-execl() pair in case of Multiprocessing.

2. I need alternative to system() because system is also creating a new process. And I dont want to create a new process in a thread.

Regards,
Raj Kumar Arora

Offline

#2 2008-01-03 10:35 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: how to call an executable with args passed in Thread function

i'm not sure i understand the problem
as i can see system(); accepts arguments like
system("somescript.sh arg1");

a simple test on my ubuntu box showed it works like that

Offline

#3 2008-01-03 01:41 PM

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

Re: how to call an executable with args passed in Thread function

I'm not sure I fully understand, but it sounds like maybe you want to basically only
exec*() a new program to take over just the current thread, rather than the whole
process?  If so, no, I don't think that's possible...  Executing a new program necessarily
requires replacing the whole process, not just a single thread of it...

However, one trick you might be able to do, if you have control over the external
program you want to execute: modify it so that the guts of its processing is done
in a dynamically loadable shared library...  Then, you can simply dlopen() that lib
from the thread, and call functions within it...  (I'm not sure, but I think some systems
even let you dlopen() an executable program, for that matter...  But, it's probably not
something you want to rely on, since I don't think it's widespread...)  Of course,
you'll want to make sure that everything done in this external code is thread-safe,
too...

Offline

#4 2008-01-03 04:15 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: how to call an executable with args passed in Thread function

You don't have any choice but to creater a new thread, if you want the existing
process to keep living after the exec call. What's wrong with fork+exec? Fork won't
duplicate all threads, only the current thread, which is replaced by the executed
program.

If you don't want to fork from a thread, for whatever reason (performance when
having many threads might be one), you can fork from the main thread and let that
child process do the future fork+exec calls (you'd need a way to communicate with
it, but that can be done with pipes or whatever).

Offline

  • Index
  • » C
  • » how to call an executable with args passed in Thread function

Board footer

Powered by FluxBB