I would like to create a new process of an exe from within the code itself, so that I can have two parallel processes.
But, I would like to them to be separate processes and not parent-child.
Is there a way to do this in C (Windows)?
I would like to create a new process of an exe from within the code itself, so that I can have two parallel processes.
But, I would like to them to be separate processes and not parent-child.
Is there a way to do this in C (Windows)?
In Windows, processes don't have parents. Some tools read the InheritedFromUniqueProcessId
value, but this does not tell you which process started your process. It only tells you where handles and other attributes were inherited from. In practice however, this value is usually set to the ID of the process that started the child process.
On Vista and above, you can change the InheritedFromUniqueProcessId
value by calling CreateProcess
with the STARTUPINFOEX
structure filled in appropriately: create an attribute list with InitializeProcThreadAttributeList
, and add a
PROC_THREAD_ATTRIBUTE_PARENT_PROCESS
attribute with UpdateProcThreadAttribute
.
On XP, there is no official way of doing this. You can try to use NtCreateProcess
or RtlCreateUserProcess
, but these don't set up the Win32 subsystem properly so your program might not run.
An ugly way I've done it in the past is to launch a child process, which then launches a second child process, and then the first child exits. This causes the second child to lose any association with the original parent.
I'm sure I later found a better way to do this, but I've gone looking around and can't find anything at the moment.
Most likely fork
ing a new process doesn't exist in windows rather you could use CreateProcess function to do that which is much easier and better option for windows.
The "easy" way is to use an intermediate command, see KB here:
http://support.microsoft.com/kb/315939
Another way to have independent processes is to ensure to do not inherit handles to ensure that the 2nd process, and creating a new process group. See Creating independent process!