进程创建、进程等待、进程休眠、结束子进程;macOS查看头文件路径;

本文通过三个实验演示了在Windows环境下如何使用C++进行进程管理,包括启动子进程、等待子进程结束及终止进程等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件路径

linux 下引用C、C++标准库、其他库的头文件一般都在:

  • /usr/include
  • /usr/local/include
  • /usr/lib/gcc-lib/xxx/xxx/include

macOS 上的头文件、库文件都被 XCode 接管了,也就是说不安装 XCode 很多开发都是做不了的,安装了 XCode 后一切都妥妥的。

开发通用的头文件都在这里:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include

很多库在这里:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks

Frameworks 里面基本都有 Headers 文件夹,也是些头文件,和 include里的基本相同

实验一

在这里插入图片描述

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;

int main() {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        "C:\\Users\\xxx\\Desktop\\childProcess.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
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return -1;
    }

    // Wait until child process exits.
    WaitForSingleObject( pi.hProcess, INFINITE );

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return 0;
}

#include <iostream>
#include <thread>
#include <windows.h>
using namespace std;

int main() {
    printf("child process begins\n");
    Sleep(5000);
    printf("child process ends\n");
    return 0;
}

实验二

在这里插入图片描述

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;

int main() {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        "C:\\Users\\DJN\\Desktop\\childProcess.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
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return -1;
    }

    // Wait until child process exits.
    // WaitForSingleObject( pi.hProcess, INFINITE );

    Sleep(3000);

    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return 0;
}

实验三

在这里插入图片描述

#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <synchapi.h>
using namespace std;

BOOL KillProcess(DWORD ProcessId) {
    HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, ProcessId);
    if (hProcess == NULL) {
        return FALSE;
    }
    if (!TerminateProcess(hProcess, 0)) {
        return FALSE;
    }
    return TRUE;
}

int main() {
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

    // Start the child process.
    if( !CreateProcess( NULL,   // No module name (use command line)
        "C:\\Users\\DJN\\Desktop\\childProcess.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
    )
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return -1;
    }

    // Wait until child process exits.
    // WaitForSingleObject( pi.hProcess, INFINITE );

    Sleep(3000);


    DWORD dwPid = GetProcessId(pi.hProcess);
    if (KillProcess(dwPid)) {
        puts("Success");
    } else {
        puts("Error");
    }


    // Close process and thread handles.
    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值