Discussion:
[Mingw-users] Making system calls to the os (system, exec & fork?)
Peter Mcleod
2003-05-25 23:17:01 UTC
Permalink
I've recently learned that we cannot use fork() on MinGW?
Seeing as system() is actually built from exec & fork -
how do we go about making cross-platform compatible system
calls to the OS? Also, how do we fork() a process? (do we
have to use multi-threading?)

==
Download ringtones, logos and picture messages at Ananzi Mobile Fun.
http://www.ananzi.co.za/cgi-bin/goto.pl?mobile
Greg Chicares
2003-05-25 23:44:02 UTC
Permalink
Post by Peter Mcleod
I've recently learned that we cannot use fork() on MinGW?
The operating system doesn't provide it.
Post by Peter Mcleod
Seeing as system() is actually built from exec & fork -
how do we go about making cross-platform compatible system
calls to the OS? Also, how do we fork() a process? (do we
have to use multi-threading?)
system() is available--it's required by the C and
C++ standards. If you need something better on
ms windows, look at CreateProcess(). This may help:
http://www.byte.com/art/9410/sec14/art3.htm
Anders Lindén
2003-05-26 04:19:03 UTC
Permalink
----- Original Message -----
From: "Peter Mcleod" <***@ananzi.co.za>
To: <mingw-***@lists.sourceforge.net>
Sent: Monday, May 26, 2003 3:16 AM
Subject: [Mingw-users] Making system calls to the os (system, exec & fork?)
Post by Peter Mcleod
I've recently learned that we cannot use fork() on MinGW?
Seeing as system() is actually built from exec & fork -
how do we go about making cross-platform compatible system
calls to the OS? Also, how do we fork() a process? (do we
have to use multi-threading?)
fork+exec translates to:
CreateProcess or ShellExecute
(there is no API way to fork() in win32, I think)

multithreading is accomplished by using:
CreateThread
Post by Peter Mcleod
==
Download ringtones, logos and picture messages at Ananzi Mobile Fun.
http://www.ananzi.co.za/cgi-bin/goto.pl?mobile
-------------------------------------------------------
This SF.net email is sponsored by: ObjectStore.
If flattening out C++ or Java code to make your application fit in a
relational database is painful, don't do it! Check out ObjectStore.
Now part of Progress Software. http://www.objectstore.net/sourceforge
_______________________________________________
MinGW-users mailing list
https://lists.sourceforge.net/lists/listinfo/mingw-users
good luck
/Anders Lindén
Peter Mcleod
2003-05-26 04:25:03 UTC
Permalink
On Sun, 25 May 2003 21:43:21 -0400
Post by Greg Chicares
The operating system doesn't provide it.
Understood.
Post by Greg Chicares
system() is available--it's required by the C and
C++ standards. If you need something better on
http://www.byte.com/art/9410/sec14/art3.htm
system() is normally just a function which reuses fork()
and execl(). I take it that system was built in a
different way under MinGW? If it does the trick though I
don't think I will need anything from the windows API (that
is, I'm endavouring to keep the code the same for windows
and linux/unix variations). Out of curiousity though - I
would like to know what method it's using internally.

I'll be investigating/trialing it soon then - not looking
for further response unless you want to add something.

==
Download ringtones, logos and picture messages at Ananzi Mobile Fun.
http://www.ananzi.co.za/cgi-bin/goto.pl?mobile
Joerg Bruehe
2003-05-26 08:01:05 UTC
Permalink
Hi Peter!
Post by Peter Mcleod
On Sun, 25 May 2003 21:43:21 -0400
[...]
Post by Greg Chicares
system() is available--it's required by the C and
C++ standards. If you need something better on
http://www.byte.com/art/9410/sec14/art3.htm
system() is normally just a function which reuses fork()
^^^^^^^^
That is true for any Unix OS including Linux, because it has
"fork()" and the "exec()" family of calls. Windows simply does
not have them. I personally like your wording "normally",
but many people may contradict you.
Post by Peter Mcleod
and execl(). I take it that system was built in a
different way under MinGW? If it does the trick though I
don't think I will need anything from the windows API (that
AIUI, in MinGW, "system()" is taken from the MS C runtime lib
which in turn probably uses "CreateProcess()".
Post by Peter Mcleod
is, I'm endavouring to keep the code the same for windows
and linux/unix variations). Out of curiousity though - I
would like to know what method it's using internally.
I'll be investigating/trialing it soon then - not looking
for further response unless you want to add something.
IMNSHO, it depends on your requirements:
1) Using "fork()" only, you get a child process that executes
the same code as the parent (quite similar to multithreading,
but without sharing the global variables).
2) Using "fork()" + "exec()", you have parent and child run in
parallel, and they can communicate via pipes or other means.
3) If you need one-way communication via a pipe, use "popen()".
4) Using "system()", the parent process will wait for the child's
termination - there is no parallelism.

I do not know how to do 1) on Windows, others may tell you.
If you need 2), you have to use "CreateProcess()", so you will
have a Windows-only section of code.
If 3) or 4) is sufficient for you, "popen()" or "system()" is
the way to go, both are required by C and are in the MSVCRT lib.

HTH,
Joerg Bruehe
--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
(speaking only for himself)
mailto: ***@sql.de
Greg Chicares
2003-05-26 13:35:06 UTC
Permalink
Post by Peter Mcleod
system() is normally just a function which reuses fork()
and execl(). I take it that system was built in a
different way under MinGW? If it does the trick though I
don't think I will need anything from the windows API (that
is, I'm endavouring to keep the code the same for windows
and linux/unix variations). Out of curiousity though - I
would like to know what method it's using internally.
I have the source code to borland's 1997 runtime library.
Its system() invokes
CreateProcess()
along with
GetLastError()
WaitForSingleObject()
GetExitCodeProcess()
TROCHU Xavier
2003-05-26 14:15:06 UTC
Permalink
Post by Greg Chicares
I have the source code to borland's 1997 runtime
library.
Its system() invokes
CreateProcess()
along with
GetLastError()
WaitForSingleObject()
GetExitCodeProcess()
This is very interesting.

How does it handle system("DIR") ?

My personnal tests here show that with mingw,
system("DIR") works as expected, but the CRT doesn't
call ShellExecute(), but instead seems to call
CreateProcess(0, "%COMSPEC% /c dir");

as CreateProcess("dir", ...) or CreateProcess(0,
"dir", ...) does not work.

Greetings,

Xavier


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
Greg Chicares
2003-05-26 17:07:04 UTC
Permalink
Post by TROCHU Xavier
Post by Greg Chicares
I have the source code to borland's 1997 runtime
library.
Its system() invokes
CreateProcess()
[...]
Post by TROCHU Xavier
This is very interesting.
How does it handle system("DIR") ?
They inspect whether the argument starts with a
name that has an extension, and if so, whether
that extension matches .bat or .cmd, without
regard to upper or lower case. They also look
for a file with the given name; apparently, if
they can't find one, they use $comspec /c name,
same as for batch files.

Maybe the open watcom project has something
available that can be freely copied.
Post by TROCHU Xavier
My personnal tests here show that with mingw,
system("DIR") works as expected, but the CRT doesn't
call ShellExecute(), but instead seems to call
CreateProcess(0, "%COMSPEC% /c dir");
as CreateProcess("dir", ...) or CreateProcess(0,
"dir", ...) does not work.
That's what I see too, using this program:

C:/tmp/syscall_dir[0]$sed 999q foo.c
#include <stdlib.h>
int main()
{
system("DIR");
return 0;
}

With the borland compiler whose rtl source I have,
as well as with mingw, I get this:

C:/tmp/syscall_dir[0]$./a
/c: can't open input file: /c

in zsh when I've set
export COMSPEC=$0

However, if I set
export COMSPEC='C:\WINNT\system32\cmd.exe'
then I get the CMD.EXE DIR command.

TROCHU Xavier
2003-05-26 14:15:07 UTC
Permalink
Post by Greg Chicares
I have the source code to borland's 1997 runtime
library.
Its system() invokes
CreateProcess()
along with
GetLastError()
WaitForSingleObject()
GetExitCodeProcess()
This is very interesting.

How does it handle system("DIR") ?

My personnal tests here show that with mingw,
system("DIR") works as expected, but the CRT doesn't
call ShellExecute(), but instead seems to call
CreateProcess(0, "%COMSPEC% /c dir");

as CreateProcess("dir", ...) or CreateProcess(0,
"dir", ...) does not work.

Greetings,

Xavier


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
Loading...