[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

_spawn() vs. CreateProcess()

Hi,
I'm a little confused. When exactly _spawn() is used and when CreateProcess() ?
Aren't both of them Win32 functions ?
Isn't the CRT a part of Win32 ?
Are there any compatability problems ?

Avi.

0
kavas
Asked:
kavas
  • 4
  • 3
  • 2
1 Solution
 
NickRepinCommented:
_spawn is a wrap around CreateProcess().
CreateProcess is a "native" Win32 API.
spawn is a part of CRT. Some functions of CRT (like spawn) use Win32 API.

I use CreateProcess() only, to get full control over it.
But it depends on the taste.
0
 
NickRepinCommented:
Also if you plan to port your program to other platform like UNIX, use spawn(). It's ANSI standart, if I'm not wrong.
In this case your program will be compatible with UNIX on the source code level.
0
 
nietodCommented:
_spawn() is not ANSI standard, no function that begins with a "_" is standard.

spawn() is a UNIX operating system call.  That is any program that runs on UNIXcan call spawn() to execture a new process.  (It doesn't even have to be a C/C++ program, a Pascal, Cobol etc program can call it as it is part of the OS, not the language).

Many non-UNIX C/C++ compilers added a non-standard function called spawn() to their libraries to help programmers that need to port UNIX code their OS.  This non-standard spawn() function just calls a function provided by this OS to perform the spawning, like on windows spawn()will call CreateProcess(), as Nick said.

In the past few years it was decided that any non-standard function added to the library by a manufacturer should begin with an "_".  This is to indicate to the programmer that they are using a non-standard function and that the function might not be available if they port the code to a different compiler or OS.   So the non-standard spawn()  function became _spawn().

Now C/C++ does provide a standard function that does the equivilent, it is system().  This is a standard funciton (No "_") and will be available on all C/C++ compilers and OSs.

As Nick said, which one to use depends on you taste, really yoru needs.  If you need power and control, you probably need CreateProcess(), but the code can't be proted to other OSs.  If you need to port to other OSs, you might want system() which is very portable, but has bery little power.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kavasAuthor Commented:
I checked both in Linux and BSDI and found no spawn entry in the man nor in the H files.
 
Is there something wrong with me ? :-)
another thing,
what is the relationship between the fork family and spawn ?



0
 
nietodCommented:
Mhhh, I thought that spawn was UNIX.....

Fork is a UNIX os call, rigth, you cna find that in the man right?  Fork is, well, weird.   The way it works, that is the way it is uysed by the programmer is unusual and awkward to say the least.  spawn() is much easier to use and may be used to spawn any process, while fok only "spawns" a new instance of the current process.  (However two forked processes can "communicate" a little more easily than two spawned processes.)
0
 
kavasAuthor Commented:
I checked with a few experienced people, and there is no spawn system all on Unix.

Any other ideas ????
0
 
nietodCommented:
opps...

Well it is a non-standard and therefore non-portable function.  It may appear as spawn() on older compilers and _spawn() on newer ones (an not at all on many).

Is it perhaps exec() that appears on UNIX?
0
 
kavasAuthor Commented:
Don't think so.
Unix exec changes the context of the calling process to the new created process. The usual scenario is calling fork and than exec.

0
 
nietodCommented:
Right.  I forgot about that.   Weird stuff.  
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now