C - 子进程显示在错误的位置
C - child processes are displayed in wrong place
我用 C 语言编写了一个代码,它接受输入参数,使用 CreateProcessA(...) 创建了两个子进程,但它并不总是按预期工作。在控制台中,它应该显示为
:
大多数时候都是这样。但有时...
发生这种情况(一些 PID 没有进入正确的“组”)。我尝试以多种不同的方式解决这个问题(我的 Linux 的代码使用 forks(),它的作用是一样的,顺便说一下,它就像一个魅力),但任何改变都没有解决问题。
这是代码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<windows.h>
#include<tchar.h>
#include<process.h>
#include<processthreadsapi.h>
int main(int argc, char* argv[], char** envp)
{
if (argc != 2)
{
fprintf(stderr, "Wrong number of input arguments!\n");
return 1;
}
char* endptr;
int secs = strtol(argv[1], &endptr, 10);
if (secs <= 0 || *endptr != '[=10=]')
{
fprintf(stderr, "Input argument is not a natural number!\n");
return 2;
}
if (atoi(argv[1]) < 1 || atoi(argv[1]) > 13)
{
fprintf(stderr, "Input argument must be between 1 and 13\n");
return 3;
}
if (atoi(argv[1]) == 1 || atoi(argv[1]) == 2)
{
return 1;
}
//CreateProcessA() etc part is here
STARTUPINFO si;
PROCESS_INFORMATION pi[2];
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
int argument = atoi(argv[1]);
int argument1 = argument - 1;
int argument2 = argument - 2;
char a1[50];
sprintf(a1, "thisprogram.exe %d", argument1);
BOOL v1 = CreateProcessA("thisprogram.exe", a1, NULL, NULL, 0, 0, NULL, NULL, &si, pi+0);
char a2[50];
sprintf(a2, "thisprogram.exe %d", argument2);
BOOL v2 = CreateProcessA("thisprogram.exe", a2, NULL, NULL, 0, 0, NULL, NULL, &si, pi+1);
WaitForSingleObject(pi[0].hProcess, INFINITE);
WaitForSingleObject(pi[1].hProcess, INFINITE);
DWORD exit1;
GetExitCodeProcess(pi[0].hProcess, &exit1);
DWORD exit2;
GetExitCodeProcess(pi[1].hProcess, &exit2);
DWORD pid = GetCurrentProcessId();
printf("%d \t %d \t %d \t %d \t\n", pid, pi[0].dwProcessId, argument1, exit1);
printf("%d \t %d \t %d \t %d \t\n", pid, pi[1].dwProcessId, argument2, exit2);
printf("%d \t \t \t %d\n\n", GetCurrentProcessId(), exit1 + exit2);
for (int i = 0; i >= 0; i--)
{
CloseHandle(pi[i].hProcess);
CloseHandle(pi[i].hThread);
}
return exit1+exit2;
}
所示示例的输入参数为 5。对于 4,它始终可以正常工作。对于 6 岁及以上 - 问题越来越大。我不认为这是一件“正常”的事情。有解决问题的想法吗?
感谢您的帮助。
我用 C 语言编写了一个代码,它接受输入参数,使用 CreateProcessA(...) 创建了两个子进程,但它并不总是按预期工作。在控制台中,它应该显示为
:
大多数时候都是这样。但有时...
发生这种情况(一些 PID 没有进入正确的“组”)。我尝试以多种不同的方式解决这个问题(我的 Linux 的代码使用 forks(),它的作用是一样的,顺便说一下,它就像一个魅力),但任何改变都没有解决问题。
这是代码:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<windows.h>
#include<tchar.h>
#include<process.h>
#include<processthreadsapi.h>
int main(int argc, char* argv[], char** envp)
{
if (argc != 2)
{
fprintf(stderr, "Wrong number of input arguments!\n");
return 1;
}
char* endptr;
int secs = strtol(argv[1], &endptr, 10);
if (secs <= 0 || *endptr != '[=10=]')
{
fprintf(stderr, "Input argument is not a natural number!\n");
return 2;
}
if (atoi(argv[1]) < 1 || atoi(argv[1]) > 13)
{
fprintf(stderr, "Input argument must be between 1 and 13\n");
return 3;
}
if (atoi(argv[1]) == 1 || atoi(argv[1]) == 2)
{
return 1;
}
//CreateProcessA() etc part is here
STARTUPINFO si;
PROCESS_INFORMATION pi[2];
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
int argument = atoi(argv[1]);
int argument1 = argument - 1;
int argument2 = argument - 2;
char a1[50];
sprintf(a1, "thisprogram.exe %d", argument1);
BOOL v1 = CreateProcessA("thisprogram.exe", a1, NULL, NULL, 0, 0, NULL, NULL, &si, pi+0);
char a2[50];
sprintf(a2, "thisprogram.exe %d", argument2);
BOOL v2 = CreateProcessA("thisprogram.exe", a2, NULL, NULL, 0, 0, NULL, NULL, &si, pi+1);
WaitForSingleObject(pi[0].hProcess, INFINITE);
WaitForSingleObject(pi[1].hProcess, INFINITE);
DWORD exit1;
GetExitCodeProcess(pi[0].hProcess, &exit1);
DWORD exit2;
GetExitCodeProcess(pi[1].hProcess, &exit2);
DWORD pid = GetCurrentProcessId();
printf("%d \t %d \t %d \t %d \t\n", pid, pi[0].dwProcessId, argument1, exit1);
printf("%d \t %d \t %d \t %d \t\n", pid, pi[1].dwProcessId, argument2, exit2);
printf("%d \t \t \t %d\n\n", GetCurrentProcessId(), exit1 + exit2);
for (int i = 0; i >= 0; i--)
{
CloseHandle(pi[i].hProcess);
CloseHandle(pi[i].hThread);
}
return exit1+exit2;
}
所示示例的输入参数为 5。对于 4,它始终可以正常工作。对于 6 岁及以上 - 问题越来越大。我不认为这是一件“正常”的事情。有解决问题的想法吗?
感谢您的帮助。