Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-24-2004, 06:54 PM
|
#1
|
Member
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140
Rep:
|
launching a program from a C++ application
Hello everybody.
I want to make a sort of task manager, but I don't know how to launch a program (for example: OpenOffice) from my own program.
In Windows you would use something like:
system("C:\folder\program.exe");
but I don't know how this works in Linux.
Could somebody help me?
Thanks in advance.
|
|
|
02-24-2004, 07:17 PM
|
#2
|
Member
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92
Rep:
|
Function exec replaces currently running process into application you want to run:
#include <unistd.h>
exec(const char *path, const char *arguments )
I dont remember whether its function which runs this application as a new process, but you can fork() current process.
|
|
|
02-24-2004, 07:28 PM
|
#3
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
exec() is what you're looking for like krajzega suggested. system() would be a poor choice because it would block your task manager process until the system() function returned.
try something like this in your task manager program:
Code:
// In the task manager process right now
if((pid = fork())
{ // pid is 0 if in the parent process, otherwise it's the new pid
exec(program, args);
// Child process is done when exec() returns.
exit(0); // Kill the child process. No longer needed.
}
// In the parent process here. Runs in parallel with the child
printf("PID %d started for program %s\n", pid, program);
|
|
|
02-24-2004, 09:44 PM
|
#4
|
Member
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176
Rep:
|
system() works for me in linux. i can get some code later if you'd like.
|
|
|
02-25-2004, 03:48 AM
|
#5
|
Member
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140
Original Poster
Rep:
|
Thanks for the answer guys, I haven't tried it yet, but I will do it as soon as possible.
|
|
|
02-25-2004, 03:51 AM
|
#6
|
Member
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551
Rep:
|
Library: stdlib.h
Prototype: int system(const char *cmd);
Syntax: system( "cat /etc/hosts");
Notes:
In Unix systems, the command is passed to "/bin/sh -c" for
execution. I do not know what handles the command in DOS systems.
use system in linux.
|
|
|
02-25-2004, 04:41 AM
|
#7
|
Member
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128
Rep:
|
Quote:
Originally posted by itsme86
Code:
[ ... ]
// Child process is done when exec() returns.
exit(0); // Kill the child process. No longer needed.
[ ... ]
|
It is worth noting that exec will only return on error. If it is a success then the old process does not exist for it to return to. Also, if you could still possibly have a forked process running, it is sometimes advisable to use _exit(2) to avoid the spring-cleaning performed by exit(3).
|
|
|
02-25-2004, 09:18 AM
|
#8
|
Member
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140
Original Poster
Rep:
|
Thanks for the answers. It works fine now.
|
|
|
All times are GMT -5. The time now is 05:25 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|