Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-22-2003, 02:47 PM   #1 (permalink)
paul
Registered User
 
Join Date: Feb 2003
Location: NS, Canada
Posts: 15
paul is on a distinguished road
Running Programs From In C++

Whats the best way to run a program (and forget about it) from C++ (GNOME2 if it matters)?

Say I wanted to run mpg123 whenever somthing was clicked, but I didn't (need) to be able to stop it at any point.

I think its like exec() but I also need to know what to #include <if anything>
__________________
paul is offline   Reply With Quote
Old 03-22-2003, 03:33 PM   #2 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
To run something within your program as if it was clicked on,
Do this:

Code:
#include <windows.h> int main(void) { system("c:\\programs\\test.mp3"); // Any file will work, Not just .EXE or .MP3 in this case return 0; }
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-22-2003, 03:57 PM   #3 (permalink)
paul
Registered User
 
Join Date: Feb 2003
Location: NS, Canada
Posts: 15
paul is on a distinguished road
Im using linux, so that won't work
__________________
paul is offline   Reply With Quote
Old 03-22-2003, 04:09 PM   #4 (permalink)
Travis Dane
Code Monkey
 
Travis Dane's Avatar
 
Join Date: Feb 2003
Location: Netherlands
Posts: 89
Travis Dane is on a distinguished road
Send a message via ICQ to Travis Dane
Oh, Sorry then, What you probably want is running a child
process in the program, A tutorial for doing this can be found
here (Program 4).
__________________
OpenGL, DirectX
Travis Dane is offline   Reply With Quote
Old 03-22-2003, 06:44 PM   #5 (permalink)
paul
Registered User
 
Join Date: Feb 2003
Location: NS, Canada
Posts: 15
paul is on a distinguished road
Thanks, this should help.

I just need some clerification though:

Code:
#include #include #include #include main(int argc, char **argv, char **envp) { pid_t me, a; me = fork(); if (!me) { printf("Child process about to execve() a shell...\n"); printf("Type 'exit' to leave\n"); if (execve("/bin/bash", argv, envp)) { printf("Error condition %d.\n", errno); } else { a = waitpid(me, NULL, 0); printf("Parent process done.\n"); } }
Why doesnt it show the includes?
Can I do other things until the fork ends?
__________________
paul is offline   Reply With Quote
Old 03-23-2003, 10:22 AM   #6 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
Quote:
Originally posted by paul
Can I do other things until the fork ends?
fork() creates an entire new process (which is identical to the old process). the only difference between them is the value they get from the fork() function call.
now that you've forked, you want to exec() a new program. exec throws out the currently running process (in this case your child process) and replaces it with a new one (mpg123, in your case).
you seem to have gotten this far.

...but wait, there's more.
the forked processes are independant of each other. they can both run at the same time. however, there is a special relationship between a parent and child process in unix. when the child exits, it sends to the parent a signal (SIGCHLD) that the child has quit, and the child becomes a zombie process until reaped by the parent. you will need to implement a signal(2) handler to notify the parent of when the child exited and then use something like waitpid(2) to clean up the child.
...and more. when the parent process exits or dies, unix automatically kills all children processes. uncoupling them is somewhat of a mess, and i'm not going to cover it.

btw, using system() would not have worked, as it would have stalled your process until the program you are running exited. calling system() on a filetype (and not an executable, as suggested above), is a bad idea! you do not know which program will actually be called to handle that file type (or even if they are associated with anything on the current system).
__________________
joe_bruin is offline   Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
c simple question problem with switch case if13121 Standard C, C++ 1 10-24-2004 09:43 PM
operate overloading member function in C# sureshkumar_kc MS Technologies ( ASP, VB, C#, .NET ) 2 10-15-2004 02:36 AM
For those who have Learned C from "The C Programming Lanuage" DemosthenesB Standard C, C++ 5 07-13-2003 12:22 AM
Starting out with C anon C 0 02-24-2003 01:06 PM
edit? anon Lounge 10 11-21-2002 03:02 PM


All times are GMT -8. The time now is 09:05 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle