5

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

sharptooth
  • 167,383
  • 100
  • 513
  • 979

6 Answers6

10

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.
  • 6,100
  • 26
  • 31
  • 2
    *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.* Which one is used is the consequence of the difference between the two not the reason :) – Piotr Dobrogost Mar 20 '11 at 11:46
5

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
  • 167,383
  • 100
  • 513
  • 979
  • 2
    Windows NT, 2000 etc do support posix and therefor do support fork http://www.robelle.com/smugbook/process.html – David Waters Mar 06 '09 at 14:39
  • Wow. Why is it not mentioned in MSDN? – sharptooth Mar 06 '09 at 14:41
  • 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 Mar 06 '09 at 14:47
  • 1
    @David Waters *Though it looks like it was taken out by default in winXP* What does make you think so? In the link you gave (http://support.microsoft.com/kb/308259) there's this statement *The POSIX subsystem has been replaced with a **more UNIX-like** environment that is named Windows Services for UNIX.* I wouldn't take it to mean there's no fork() implementation any more. In fact in the [Summary of Interfaces](http://technet.microsoft.com/en-us/library/bb463206.aspx#EBAA) section of Porting Applications in C (Services for UNIX 3.0 Technical Note) fork() is present as supported call... – Piotr Dobrogost Mar 20 '11 at 13:23
2

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
  • 63,317
  • 21
  • 138
  • 197
1

You might want to know Microsoft provides fork() in high-end versions of Windows with component called Subsystem for UNIX-based Applications (SUA). You can find details in my answer here.

Community
  • 1
  • 1
Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
1

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

Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • 1
    No native way is an awkward way of putting it since it is the NT Native API that allows for exactly that. Guess how the former POSIX subsystem and now SFU/SUA are implemented ;) ... check out the book "Windows NT/2000 Native API" by Nebbett, it includes an example implementation of `fork()`. – 0xC0000022L Mar 15 '12 at 13:22
0

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
  • 1
  • 1