views:

1182

answers:

5

Do we have any sort of relationship between fork() and CreateThread? Is there anything that CreateThread internally calls fork()?

+2  A: 

fork() only exists on Unix systems and it creates a new process with the same state as the caller. CreateThread() creates a new thread in the same process.

sharptooth
Windows NT, 2000 etc do support posix and therefor do support fork http://www.robelle.com/smugbook/process.html
David Waters
Wow. Why is it not mentioned in MSDN?
sharptooth
mentioned here http://support.microsoft.com/kb/149902. Though it looks like it was taken out by default in winXP (it has been a while since I have c'ed on windows) http://support.microsoft.com/kb/308259
David Waters
Yeap, now it's clear why it's not in MSDN...
sharptooth
+1  A: 

CreateThread - is for threads, fork - is for creating duplicate process. And there is no native way to have fork functionality for windows (at least through Win32 ).

inazaruk
+6  A: 

In NT, the fundamental working unit is called a thread (ie NT schedules threads, not processes.). User threads run in the context of a process. When you call CreateThread, you request the NT kernel to allocate a working unit within the context of your process (you also have fibres that are basically threads you can schedule yourself but that's beyond the topic of your question).

When you call CreateThread you provide the function with an entry point that is going to be run after the function is called. The code must be within the virtual space of the process and the page must have execution rights. Put simply, you give a function pointer. ;)

fork() is an UNIX function that requests the kernel to create copy of the running process. The parent process gets the pid of the child process and the child process gets 0 (this way you know who you are).

If you wish to create a process in Windows, you call the CreateProcess function, but that doesn't behave like fork(). The reason being that most of the time you will create threads, not processes.

As you can see, there is no relation between CreateThread and fork.

Edouard A.
+1  A: 

The Windows and Unix process model is fundamentally very different, so there is no way of directly mapping the API from one on top of the other.

fork() clones the current process into two. In the parent process, fork() returns the pid, and in the child it returns 0. This is typically used like this:

int pid;
if (pid = fork()) {
    // this code is executed in the parent
} else {
    // this code is executed in the child
}

Cygwin is an emulation layer for building and running Unix applications on Windows which emulates the behavior of fork() using CreateProcess().

JesperE
I believe you labelled the blocks wrong, as the *child* process gets 0, and the parent gets the PID of the child.
Jeremybub
Yes, thanks. Fixed now. (Impressive that this went unnoticed for over a year.)
JesperE
+1  A: 

Found this link which i believe could be helpful in clearing few facts regarding forking/threading. Sharing over here: http://www.geekride.com/index.php/2010/01/fork-forking-vs-threading-thread-linux-kernel/

Manish