Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 8th, 2008, 12:44 PM   #1
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Cross-platform exec()

Hello everyone,

I am quite motivated to code a new programming language this summer. My idea is, given the source code of a program written in this language, to translate it to c/c++ and then compile it using gcc/g++. Now, before starting the project, I would like to know if there is a way to do this cross-platform. What I mean is :
1) On UNIX-like systems, you only have to do fork() and exec("g++ -o etc...")
2) On Wind@ws, I don't know of an exec()-like substitute SC, perhaps system(), but I'm not sure if I should use it.
What would be the correct way of doing this?

Thank you
Lakrids is offline   Reply With Quote
Old Jun 8th, 2008, 2:40 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Cross-platform exec()

I'm not sure, but popen might be what you're looking for.
titaniumdecoy is offline   Reply With Quote
Old Jun 8th, 2008, 3:16 PM   #3
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 21
Rep Power: 0 Sorrofix is on a distinguished road
Re: Cross-platform exec()

>perhaps system(), but I'm not sure if I should use it.
Depends on what you need. system() pauses your program, opens a new shell, and executes the command you give it in its argument. When the program finishes executing, it will exit, the shell will close, and your program will resume execution.

popen, like titaniumdecoy mentioned, is for opening a pipe to a program. That is, it'll execute a program, and then you can use the pipe it's opened to communicate with the other process. In other words, your program isn't halted until the process is done (like system()).

Last edited by Sorrofix; Jun 8th, 2008 at 3:28 PM.
Sorrofix is offline   Reply With Quote
Old Jun 8th, 2008, 10:16 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Cross-platform exec()

There is no direct fork() equivalent under windows [creating a duplicate of the current process with the same state, except being able to detect it is forked] but it is possible to create a new process to execute a new program.

Libraries with some compilers (Microsoft, Borland) targeting windows do support an execl() function within a header named something like <process.h>.

Also look up the CreateProcess() function in the win32 API. execl() functions and system() functions under windows will typically be implemented using CreateProcess() or related functions.
grumpy is offline   Reply With Quote
Old Jun 8th, 2008, 10:59 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Cross-platform exec()

You could use the pthread library, although I believe it requires a DLL on Windows.
titaniumdecoy is offline   Reply With Quote
Old Jun 9th, 2008, 7:55 AM   #6
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Cross-platform exec()

Thank you! I didn't know so much possibilities existed on Wind@ws! I will definately look up popen() and execl(). system() doesn't look like a very good choice, given that you can't communicate with your "child", e.g. you don't know when it has completed it's task or failed, and if so, for what reason.

Thanks for the help!
Lakrids is offline   Reply With Quote
Old Jun 9th, 2008, 10:47 AM   #7
Sorrofix
Expert Bug Developer
 
Sorrofix's Avatar
 
Join Date: Apr 2008
Posts: 21
Rep Power: 0 Sorrofix is on a distinguished road
Re: Cross-platform exec()

>you don't know when it has completed it's task or failed
system() waits until its child process has completed. Upon completion, system() will return the value returned by the child process. So yes, using the return value of system(), you can determine the success of the program you ran.
Sorrofix is offline   Reply With Quote
Old Jun 9th, 2008, 11:04 AM   #8
Lakrids
Newbie
 
Join Date: Dec 2007
Posts: 28
Rep Power: 0 Lakrids is on a distinguished road
Re: Cross-platform exec()

Quote:
system() waits until its child process has completed. Upon completion, system() will return the value returned by the child process. So yes, using the return value of system(), you can determine the success of the program you ran.
Wow, I didn't know that! To be honest, the only time I used system() was back when I started learning C++ and I used system("pause") (they should really change those online tutorials )...had no clue that it actually returned something!

So what does it return? An int?
Lakrids is offline   Reply With Quote
Old Jun 9th, 2008, 2:41 PM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Re: Cross-platform exec()

man 3 system:
Quote:
If command is a NULL pointer, system() will return non-zero if the command interpreter sh(1) is available, and zero if it is not.

The system() function returns the exit status of the shell as returned by waitpid(2), or -1 if an error occurred when invoking fork(2) or waitpid(2). A return value of 127 meansmthe execution of the shell failed.
titaniumdecoy is offline   Reply With Quote
Old Jun 9th, 2008, 3:39 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Re: Cross-platform exec()

Quote:
Originally Posted by Sorrofix View Post
system() waits until its child process has completed. Upon completion, system() will return the value returned by the child process. So yes, using the return value of system(), you can determine the success of the program you ran.
That is only true on some target platforms, and with some libraries. It is not a predictable result.

The C standard has this to say on system(), and is all that can relied upon in cross-platform development.
Quote:
Originally Posted by ISO/IEC 9899
7.20.4.6 The system function

Synopsis


1 #include <stdlib.h>
int system(const char *string);

Description

2 If string is a null pointer, the system function determines whether the host
environment has a command processor. If string is not a null pointer, the system function passes the string pointed to by string to that command processor to be executed in a manner which the implementation shall document; this might then cause the program calling system to behave in a non-conforming manner or to terminate.

Returns

3 If the argument is a null pointer, the system function returns nonzero only if a command processor is available. If the argument is not a null pointer, and the system function does return, it returns an implementation-defined value.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coder's Block Arena - The Game AI Platform Sane Existing Project Development 23 May 6th, 2008 9:12 PM
Which Flavor: CPython or IronPython and Exec Opts Kigneer Python 2 Apr 6th, 2008 10:36 PM
Php with Sun Java System Application Server Platform lucifer PHP 0 Jun 7th, 2007 4:03 AM
Need help from someone who has downloaded microsoft platform sdk rsnd Other Programming Languages 3 Feb 18th, 2006 5:04 PM
Platform Api jstephens C 3 Jan 2nd, 2006 5:17 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC