Windows使用fork函数的c++例子
时间: 2023-12-31 15:09:31 浏览: 91
很抱歉,Windows系统并不支持fork函数,因为它是基于Unix系统的概念。在Windows系统中,可以使用CreateProcess函数来创建进程。下面是一个简单的C++程序,演示如何使用CreateProcess函数创建一个新进程:
```c++
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcess(
NULL, // Application name
"notepad.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No 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
))
{
cout << "CreateProcess failed: " << GetLastError() << endl;
return 1;
}
cout << "New process created with ID: " << pi.dwProcessId << endl;
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
```
该程序创建一个新的进程,启动记事本(notepad.exe),然后输出新进程的ID。
阅读全文