#include #include #include #include void draw(const char *txt); int main(int argc, char* argv[]) { STARTUPINFO si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); memset(&pi, 0, sizeof(pi)); int curtime = (int) GetTickCount(); printf("Starting a process, curtime=%d\n", curtime); if (argc <= 1) { printf("Parent\n"); if (!CreateProcess( "winproc.exe", // Module name "winproc Tails", // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Do not inherit handles 0, // CREATE_NEW_CONSOLE, // Creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi // Pointer to PROCESS_INFORMATION structure )) { printf( "CreateProcess failed (%d)\n", GetLastError() ); exit(-1); } srand(curtime); draw("Heads"); // Wait until child process exits. WaitForSingleObject(pi.hProcess, INFINITE); // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } else { printf("Child\n"); srand(curtime + 1234567); draw(argv[1]); } return 0; } void draw(const char *txt) { for (int i = 0; i < 10; ++i) { printf("%s\n", txt); int r = rand() % 2000 + 1; Sleep(r); } }