0

I wanted to simulate preemptive by running two process simultaneously. Let's say that I have program A which runs

while(1){
   printf("A\n");
}

and program B which runs

while(1){
   printf("B\n");
}

What I wanted to do is for the program to display (or at least simulate) how preemptive works, so I want the .exe to look more or less like this

A
A
A
B
B
A
A
...

Here is how my code so far

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

void _tmain( int argc, TCHAR *argv[] ){
   STARTUPINFO si;
   PROCESS_INFORMATION pi;

   ZeroMemory( &si, sizeof(si) );
   si.cb = sizeof(si);
   ZeroMemory( &pi, sizeof(pi) );

   if(CreateProcess
   (TEXT("c:\\C\\Osenshuu\\create_process_a.exe"), 
   NULL, 
   NULL,
   NULL,
   FALSE,
   0,
   NULL,
   NULL,
   &si,
   &pi)){ 
      WaitForSingleObject(pi.hProcess,INFINITE);
      CloseHandle(pi.hThread);
      CloseHandle(pi.hProcess);
   } 

else{
    printf("The process could not be started...");
    }
}

It works great displaying the A program but now I'm wondering how could I add b.exe to that createprocess to make both program A and B run simultaneously? Is it even possible to do it?

Sae
  • 79
  • 7
  • Just call `CreateProcess()` again, know for the "b.exe"? – alk May 01 '17 at 09:15
  • @alk I did try adding new CreateProcess() after the first CreateProcess(), but since the first CreateProcess() was an if statement and it called an infinite loop, the program was just showing me an endless "AAAA..." with no sign of "B". Any hint on where to add the other CreateProcess() statement? – Sae May 01 '17 at 09:20
  • 1
    Did you make the second `CreateProcess` call before or after the call to `WaitForSingleObject`? It should be before, of course, otherwise you'll never get to run the second program if the first program never ends. – Eran May 01 '17 at 09:31
  • You might 1st of want to read the related documentation (https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx, https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx), then understand "your" code, and finally make the adjustments as per your requirements. :-) – alk May 01 '17 at 09:34
  • 1
    @eran well, that was... very simple. I wonder why I didn't think of that. Thanks a lot! worked like a magic – Sae May 01 '17 at 10:09
  • @alk That was one of the very first things I looked up, but there are many words that, while attempting to understand this function have sent me into a Wikipedia'ing frenzy, trying to understand each word while putting together the big picture didn't help me at all... Thanks for your answer though! – Sae May 01 '17 at 10:13

0 Answers0