我正在尝试解决经典的哲学家进餐问题。哲学家进餐问题指出 K 位哲学家围坐在圆桌旁,每对哲学家之间夹着一根筷子。每个哲学家之间有一根筷子。如果哲学家能拿起他身边的两根筷子,他就可以吃饭。一根筷子可以被任何一个相邻的追随者捡起,但不能同时被两个人捡起。我试图通过多重处理来解决这个问题,这意味着每根筷子都是一个互斥锁,而每个哲学家都是一个过程。
HANDLE forks[NUMBER_OF_FORKS];
int main()
{
STARTUPINFO si[NUMBER_OF_PHILOSOPHERS]; // NUMBER_OF_PHILOSOPHERS is 5
PROCESS_INFORMATION pi[NUMBER_OF_PHILOSOPHERS]; // NUMBER_OF_PHILOSOPHERS is 5
initForks(NUMBER_OF_PHILOSOPHERS); // The function initializing all the Mutexs
std::string param;
LPWSTR test;
for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i++)
{
ZeroMemory(&si[i], sizeof(si[i]));
si[i].cb = sizeof(si[i]);
ZeroMemory(&pi[i], sizeof(pi[i]));
// Converting the param to LPWSTR(The param represent the number of the philosopher).
param = std::to_string(i);
test = ConvertString(param);
if (!CreateProcess(L"..\\Debug\\Philosopher.exe", test, NULL, NULL, FALSE, 0, NULL, NULL, &si[i], &pi[i]))
{
std::cout << GetLastError() << std::endl;;
}
}
for (int i = 0; i < NUMBER_OF_PHILOSOPHERS; i++)
{
WaitForSingleObject(pi[i].hProcess, INFINITE);
}
}
在第 17 行,当我使用 CreateProcess 函数时,我收到此错误: showing the error
谁能帮我找出问题所在?谢谢大家的帮助!
答案 0 :(得分:0)
从图片中可以看出,
Expression : _p !- nullptr
您没有初始化 test
变量,试试这个,
LPWSTR test = new WCHAR[100];
memset(test, NULL, 200);
...
或者,
std::string param;
WCHAR test[100];
param = std::to_string(i);
mbstowcs(test, param.c_str(), param.size() + 1); //Converts a multibyte character string from the array whose first element is pointed to by src to its wide character representation.