Skip to content

Instantly share code, notes, and snippets.

@figengungor
Created October 7, 2013 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save figengungor/6874976 to your computer and use it in GitHub Desktop.
Save figengungor/6874976 to your computer and use it in GitHub Desktop.
Parent Process in Windows Lab1
#include<stdio.h>
#include<Windows.h>
#define NO_OF_PROCESS 4
int main(int argc, char* argv[])
{
STARTUPINFO si[NO_OF_PROCESS]; //needed info to create process, cb(size needed for info), new window, title...
PROCESS_INFORMATION pi[NO_OF_PROCESS]; //to store info of newly created process, ids, handles of process and thread...
int i=0;
//Whatcha planning after creating processes, putting them on a leash, so they won't run away. You are reading my mind.
//Joke besides, in order to manage process after creation, you need to create a handle for it.
HANDLE handle[NO_OF_PROCESS];
//need to clear memory for startup info struct,
//because if there is any value left, it may mess process that's gonna be created.
for(i=0; i<NO_OF_PROCESS; i++)
{
//ZeroMemory sometimes can be ignored while compiling so we use SecureZeroMemory.
//Cleaning memory starting from the beginning of the startup info struct
SecureZeroMemory(&si[i], sizeof(STARTUPINFO));
si[i].cb = sizeof(STARTUPINFO);
//Cleaning memory starting from the beginning of the process info struct
SecureZeroMemory(&pi[i], sizeof(PROCESS_INFORMATION));
}
//This array keeps arguments that we are going to use while creating our child processes.
//Our parent process will execute those items as a command line in terminal
//First one is the process(program) that is gonna be executed and second is the first argument to this process.
char *lpCommandLine[] = {"child.exe 1", "child.exe 2", "child.exe 3", "child.exe 4"};
//Are we readyyyyyy? to create our child processes? Yessssssssss!
//There are lots of parameters that CreateProces func. takes.
//Let's focus on lpCommandLine, si, pi and flag for a new window(new window for each process) for now.
if(!CreateProcess(NULL, lpCommandLine[i], NULL, NULL, FALSE, CREATE_NEW_WINDOW, NULL, NULL, &si[i], &pi[i]))
{
printf("No process and cookies for you!");
system("pause");
exit(0);
}
else
{
printf("parent is working just fine.");
handles[i] = pi[i].hProcess; //let's get handle of newly created process from process info
}
//After parent finishes creating child processes, it wants to call it a day.
//But not that easy parent, be a responsible parent and wait for your all children.
WaitForMultipleObjects(NO_OF_PROCESS, handles, INFINITE); //how many?, where is the leash?, how much to wait?
//Okey, we are done. done is done. time to let go of those leashes so others who need can use later.
//Let's close our handles. We have two of them for each process.
//Each process is created with at least one thread which is called primary thread.
//This thread also has a handle. Don't believe me? Examine the process info struct;)
for(i=0; i<NO_OF_PROCESS; i++)
{
CloseHandle(pi[i].hProcess); //or CloseHandle(handle[i])
CloseHandle(pi[i].hThread);
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment