possibilities in "C"

Programming, for all ages and all languages.
Post Reply
rm

possibilities in "C"

Post by rm »

is it possible for a program in C to execute an exe file. ??? ???
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

Code: Select all

#include <stdlib.h>

int rc = system( "path/to/app.exe" );
Every good solution is obvious once you've found it.
erikgreenwald

Re:possibilities in "C"

Post by erikgreenwald »

system() can be an awfully dangerous call to make, as shell variables can be induced to alter the call... fork() and something from the exec() family (execl() or execv()) are easy and tend to be a wee bit safer :)

-Erik
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

Yes, but that's POSIX API, not C. How do you know he's running on a machine with POSIX API, and that he's not interested in portability across platforms? ;-)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:possibilities in "C"

Post by Candy »

How is system("notepad.exe"); more portable?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

Grrrr.... >:( 8)

For all we know, he might be doing system("MyOtherApp").

Starting a new process can be done with Process.Start(), OpenExec(), StartExecutable(), or execl(), depending on your operating system / support libraries.

But it can be done with system() on any compliant C compiler in any environment.
Every good solution is obvious once you've found it.
DevL

Re:possibilities in "C"

Post by DevL »

Candy wrote: How is system("notepad.exe"); more portable?
It isn't. Replacing the string with a header-defined constant (think EDITOR in this case) on the other hand would greatly aid portability...

Being the cross-platform and portability geek, I'll have to side with Solar on this one (surprise, surprise). :)
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:possibilities in "C"

Post by distantvoices »

since when you two don't agree, eh? *rofl* sometimes it occurs to me to think you are kinda twins or so.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

Thanks for the compliment. ;D
Every good solution is obvious once you've found it.
rich_m

Re:possibilities in "C"

Post by rich_m »

I was just thinkin that i could create somthing like multi-threading programs in 'C' usin this system(). but then it would'nt work in DOS if not for the windows start.exe

Code: Select all

int n=system("start music.exe");
btw in my college they teach us to program using 16-bit compilers..TURBO C.. :( . How do I use vc++ ?(i've got .NET2003)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

rich_m wrote: I was just thinkin that i could create somthing like multi-threading programs in 'C' usin this system(). but then it would'nt work in DOS if not for the windows start.exe
That isn't multithreading, that is batch parallelism.

Multithreading is when several threads cooperate. Your example can't give music.exe any input data beyond command line parameters, and the only output data is the return code. Anything beyond that would have to work with temporary files, and that defeats the whole purpose of multithreading - increased efficiency.

The POSIX function you want to look at is fork()... dunno what it's equivalent is in WinAPI.
Every good solution is obvious once you've found it.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:possibilities in "C"

Post by Colonel Kernel »

Using fork() doesn't qualify as multithreading either. IMO, "multithreading" implies multiple threads in the same process, sharing the same address space.

The Win32 equivalent of fork() is CreateProcess(), more or less. To create threads, the Win32 call is CreateThread(), but if you're using C or C++ you should use _beginthreadex() instead. It's a wrapper for CreateThread() that cleans up C/C++ run-time library bookkeeping structures when the thread terminates.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
rich_m

Re:possibilities in "C"

Post by rich_m »

Solar wrote: That isn't multithreading, that is batch parallelism.
Guess i had the wrong impression of what multithreading meant(i thought it was just running a couple of programs simultaneously), thanks . btw i would like to know of 'a' good tutorial for c,c++ using 32bit compilers, i'm not sure if my request sounds weird, if it does my apologies. :)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:possibilities in "C"

Post by Solar »

Colonel Kernel is correct, of course - fork() does create a new process, not a new thread - however, that process starts with all the data of the forking process, so you're a big step further than with system().

And I have to admit that I haven't written anything multithreading yet. :-D

Running several programs in parallel, BTW, is called "multitasking" (as opposed to what DOS did). ;)
Every good solution is obvious once you've found it.
Post Reply