LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 02-24-2004, 06:54 PM   #1
Error1312
Member
 
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140

Rep: Reputation: 15
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.
 
Old 02-24-2004, 07:17 PM   #2
krajzega
Member
 
Registered: Jan 2004
Location: Poland
Distribution: FreeBSD 5.1
Posts: 92

Rep: Reputation: 15
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.
 
Old 02-24-2004, 07:28 PM   #3
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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);
 
Old 02-24-2004, 09:44 PM   #4
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
system() works for me in linux. i can get some code later if you'd like.
 
Old 02-25-2004, 03:48 AM   #5
Error1312
Member
 
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140

Original Poster
Rep: Reputation: 15
Post

Thanks for the answer guys, I haven't tried it yet, but I will do it as soon as possible.
 
Old 02-25-2004, 03:51 AM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 547

Rep: Reputation: 38
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.
 
Old 02-25-2004, 04:41 AM   #7
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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).
 
Old 02-25-2004, 09:18 AM   #8
Error1312
Member
 
Registered: Feb 2004
Location: Belgium
Distribution: Ubuntu 10.04 Lucid Lynx
Posts: 140

Original Poster
Rep: Reputation: 15
Thanks for the answers. It works fine now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Launching application + variables in FVWM Peff Linux - Software 2 06-28-2005 09:36 AM
using console after launching a program off it Lechium Linux - Newbie 3 08-07-2004 03:37 AM
Launching application when gnome startups appel Linux - Software 3 08-02-2003 11:06 PM
launching an application when X starts... DKnight Linux - Newbie 13 06-08-2003 08:43 AM
Launching X Program from SSH deckmoney Linux - General 4 04-16-2003 05:34 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration