_spawn() vs. CreateProcess()

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 ?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
_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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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 ?

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.)
kavasAuthor Commented:
I checked with a few experienced people, and there is no spawn system all on Unix.

Any other ideas ????

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?
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.

Right.  I forgot about that.   Weird stuff.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.