Hi

I am trying to figure out how to do something for a program I am writing I am fairly new to C++ I have only been using it for about 3 months.

Basically I want to launch a pthread which is easy enough but from within that thread I want to launch a perl script, I don't want the perl embedded, something like:

exec("/usr/local/bin/perl", "sqldaemon.pl", "/var/log/error");

kind of thing, the "sqldaemon.pl" and "/var/log/error" sections being arguements to be passed to the script.

I have gone through my books but I can't find any reference on how to launch an external application, this application will also be running in a daemon mode and wont terminate until the main program exits, which is why it is launched from a thread instead of from the main program.

Any help on the correct syntax for launching external applications with arguements would be appreciated.

Thanks

Ben

Recommended Answers

All 4 Replies

if you call an exec() function from a thread, the entire process will be replaced by the program u call, not just the single thread of execution. exec'ing replaces the entire address space of a process with the application being executed. what u want to do is fork() off a child and have the child exec(). using the exec functions can be a bit tricky, but if u read the man pages carefully it makes sense. 'man execve'. here is a short summary tho:

using execve() means u have to provide an array of pointers(NULL terminated) for the arguments to the prog and for the enviromental variables. usage would be:

char  *args[] = {"ls", "-al", NULL}, *env[] = {NULL};
  execve("/bin/ls", args, env);

the 'v' stands for Vector, as in a vector of pointers for args/env, and the 'e' stands for enviroment, since u include the enviromental variables.

using the execl() function, u can provide a list of arguments instead, usage:

execl("/bin/ls", "ls", "-al", NULL);

notice how u have to provide the fulll path to executable, if instead u wanted to use your $PATH variable, u can use execlp()

execlp("ls", "ls", "-al", NULL);

the 'l' stands for list, as in a list of arguments. the 'p' stands for path, as in use the $PATH enviromental variable to search for "ls". HTH

what u want to do is fork() off a child and have the child exec().

Hi this is something I need to investigate a little further, however, I am really trying to avoid any fork()'s in the program, the program is actually a searchengine for an ecommerce site, and some of the spec's for the project include keeping the program contained as much as possible, so if the exec* series of functions all cause forks is there any alternative functions that specifically contain functions in a thread, one of the other reasons is when the program terminates there is a possibility that it will not have time to tidy child processes, as such one of the reasons for using threads is that it should destroy the thread when the program exit()'s. I know the chances of there being a function like I am describing are fairly remote but you never know unless you ask right?

:o)

Thanks for you answer if there isn't anything more like what I am looking for then I will use one of the examples you gave.

Regards
Ben

let me clear that up. calling exec() does NOT create a new process. calling fork creates the new process. what i was trying to say was: if u have a thread call an exec() function, THE ENTIRE process that the thread was a part of will cease to run and be replaced by the process that is being exec'd. so if one thread is off serving a client, and another thread in that process calls execve(), the thread servering the client will be killed when the other thread calls execve(). what the exec family of functions does is replace the entire address space of the current process. if u dont want to have any 'zombied' children, then install a signal handler in your parent process that will catch SIGCHLD and cleanup any children that die. if one the other hand your main program will be exiting before the children, what you can do is instead install an atexit() handler in the parent process that will send a SIGKILL or SIGQUIT to the ENTIRE PROCESS GROUP of the parent. then, all the child process's will be sent a SIGKILL signal when the parent exits, and they will exit as well.

Excellent I think I understand what you mean now, I will play about with execl, and RTFM :)

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.