将创建进程的API-posix_spawn封装成一个进程类

本文介绍了一种使用C++实现的进程创建类,通过该类可以简化进程创建过程,提供了创建进程所需的API封装,并展示了如何使用此类创建多个进程并等待它们完成。

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

将创建进程的API封装成一个进程类,用该类生成一个对象,则创建了一个进程;

主要API函数为:

 #include <spawn.h>
int posix_spawn(pid_t *restrict pid, const char *restrict path,
              const posix_spawn_file_actions_t *file_actions,
              const posix_spawnattr_t *restrict attrp,
              char *const argv[restrict], char *const envp[restrict]);
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);

       #include <spawn.h>

       int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t *
              file_actions);
       int posix_spawn_file_actions_init(posix_spawn_file_actions_t *
              file_actions);

       #include <spawn.h>

       int posix_spawnattr_destroy(posix_spawnattr_t *attr);
       int posix_spawnattr_init(posix_spawnattr_t *attr);

MyProcess.h

/*************************************************************************
	> File Name: MyProcess.h
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 20时13分03秒
 ************************************************************************/

#ifndef _MYPROCESS_H
#define _MYPROCESS_H
#include <iostream>
#include <spawn.h>
#include <string>
#include <sys/wait.h>

using std::string;

class MyProcess{
    public:
        MyProcess(string path, char **av, char **ep);
        MyProcess(string path, char **av, char **ep, posix_spawn_file_actions_t psfa, posix_spawnattr_t attr);
        void run();
        void pwait(int *x);
    private:
        pid_t pid;
        posix_spawn_file_actions_t file_actions;
        posix_spawnattr_t attrp;
        char **argv;
        char **envp;
        string ProgramPath;
};
#endif

MyProcess.cpp

/*************************************************************************
	> File Name: MyProcess.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 20时19分58秒
 ************************************************************************/

#include "MyProcess.h"
using namespace std;

MyProcess::MyProcess(string path, char **av, char **ep)
{
    ProgramPath = path;
    argv = av;
    envp = ep;
    posix_spawn_file_actions_init(&file_actions);
    posix_spawnattr_init(&attrp);
}

MyProcess::MyProcess(string path, char **av, char **ep, posix_spawn_file_actions_t psfa, posix_spawnattr_t attr)
{
    ProgramPath = path;
    argv = av;
    envp = ep;
    file_actions = psfa;
    attrp = attr;

    posix_spawn_file_actions_init(&file_actions);
    posix_spawnattr_init(&attrp);
}

void MyProcess::run()
{
    posix_spawn(&pid, ProgramPath.c_str(), &file_actions, &attrp, argv, envp);
}

void MyProcess::pwait(int *x)
{
    wait(x);
}

main.cpp

/*************************************************************************
	> File Name: main.cpp
	> Author: 
	> Mail: 
	> Created Time: 2015年12月14日 星期一 20时04分46秒
 ************************************************************************/

#include "MyProcess.h"
#include <cstdlib>
using namespace std;

#define PROCESSNUM 2

int main(int argc, char *argv[])
{
    int wait_val[2];

    if (argc < 2){
        cout << "the argc is less than 3" << endl;
        exit(-1);
    }

    MyProcess child1("ls", argv, NULL);
    MyProcess child2("/bin/ps", argv, NULL);
    
    child1.run();
    child2.run();
    
    child1.pwait(&wait_val[0]);
    child1.pwait(&wait_val[1]);
   
    cout << "child1.pwait is " << wait_val[0] << endl;
    cout << "child2.pwait is " << wait_val[1] << endl;

    exit(0);
}


下图才是程序的运行结果:(将下面的运行结果与上图比较,证明程序正确)


在使用 `posix_spawn` 函数创建进程时,可以通过设置 `posix_spawn_file_actions_t` 对象来设置环境变量。 具体步骤如下: 1. 创建 `posix_spawn_file_actions_t` 对象并初始化。 ```c posix_spawn_file_actions_t file_actions; posix_spawn_file_actions_init(&file_actions); ``` 2. 使用 `posix_spawn_file_actions_addattr` 函数设置环境变量。 ```c char* envp[] = {"ENV_VAR=value", NULL}; // 环境变量数组 posix_spawn_file_actions_addattr(&file_actions, POSIX_SPAWN_USEVFORK, NULL, 0); // 设置 vfork 标志 posix_spawn_file_actions_addattr(&file_actions, POSIX_SPAWN_SETENV, envp, 0); // 设置环境变量 ``` 3. 调用 `posix_spawn` 函数创建进程。 ```c pid_t pid; const char* path = "/bin/bash"; // 可执行文件路径 char* argv[] = {path, NULL}; // 命令行参数数组 posix_spawn(&pid, path, &file_actions, NULL, argv, envp); ``` 完整示例代码如下: ```c #include <stdio.h> #include <stdlib.h> #include <spawn.h> int main(int argc, char* argv[]) { pid_t pid; posix_spawn_file_actions_t file_actions; char* envp[] = {"ENV_VAR=value", NULL}; posix_spawn_file_actions_init(&file_actions); posix_spawn_file_actions_addattr(&file_actions, POSIX_SPAWN_USEVFORK, NULL, 0); posix_spawn_file_actions_addattr(&file_actions, POSIX_SPAWN_SETENV, envp, 0); const char* path = "/bin/bash"; char* args[] = {path, NULL}; if (posix_spawn(&pid, path, &file_actions, NULL, args, envp) != 0) { perror("posix_spawn"); return EXIT_FAILURE; } waitpid(pid, NULL, 0); // 等待子进程结束 posix_spawn_file_actions_destroy(&file_actions); return EXIT_SUCCESS; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值