一种简单易用的C++进程类

一 代码结构

二 代码

1. posix_process.h

/*************************************************************************
    > File Name: posix_process.h
    > Author: wangzhicheng
    > Mail: 2363702560@163.com 
    > Created Time: Thu 12 Feb 2015 07:35:34 PM WST
 ************************************************************************/
#ifndef POSIX_PROCESS_H
#define POSIX_PROCESS_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>
#include <iostream>
#include <string>
using namespace std;
/*
 * posix process class
 * */
class POSIX_PROCESS {
	protected:
		pid_t mPid;
		posix_spawnattr_t mSpawnattr;
		posix_spawn_file_actions_t mFileactions;
		char **mArgv;
		char **mEnvp;
		string mProgram_path;
	public:
		POSIX_PROCESS(const string &program_path, char **argv, char **envp);
		POSIX_PROCESS(const string &program_path, char **argv, char **envp, posix_spawnattr_t &X, \
				posix_spawn_file_actions_t &Y);
		void run(void);
		void join(int &value);
};
#endif

2. posix_process.cpp

/*************************************************************************
    > File Name: posix_process.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@163.com 
    > Created Time: Thu 12 Feb 2015 07:50:36 PM WST
 ************************************************************************/

#include "posix_process.h"
POSIX_PROCESS::POSIX_PROCESS(const string &program_path, char **argv, char **envp) {
	this->mArgv = argv;
	this->mEnvp = envp;
	this->mProgram_path = program_path;
	posix_spawnattr_init(&this->mSpawnattr);
	posix_spawn_file_actions_init(&this->mFileactions);
}
POSIX_PROCESS::POSIX_PROCESS(const string &program_path, char **argv, char **envp, posix_spawnattr_t &X, posix_spawn_file_actions_t &Y) {
	this->mArgv = argv;
	this->mEnvp = envp;
	this->mProgram_path = program_path;
	this->mSpawnattr = X;
	this->mFileactions = Y;
	posix_spawnattr_init(&this->mSpawnattr);
	posix_spawn_file_actions_init(&this->mFileactions);
}
void POSIX_PROCESS::run(void) {
	posix_spawn(&this->mPid, this->mProgram_path.c_str(), &this->mFileactions, &this->mSpawnattr, \
			this->mArgv, this->mEnvp);
}
void POSIX_PROCESS::join(int &value) {
	wait(&value);
}

3. schedule.cpp

/*************************************************************************
    > File Name: schedule.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Thu 12 Feb 2015 08:01:44 PM WST
 ************************************************************************/
#include "posix_process.h"
int main(int argc, char **argv, char **envp) {
	string program_path;
	cin >> program_path;
	POSIX_PROCESS process(program_path, argv, envp);
	int value;
	process.run();
	process.join(value);

	return 0;
}

4. makefile

CC=g++
all:
	$(CC) -g -o main schedule.cpp posix_process.cpp posix_process.h

三 程序运行截图


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值