程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 一種簡單易用的C++進程類

一種簡單易用的C++進程類

編輯:C++入門知識

一種簡單易用的C++進程類


一 代碼結構

\

二 代碼

1. posix_process.h

/*************************************************************************
    > File Name: posix_process.h
    > Author: wangzhicheng
    > Mail: [email protected] 
    > Created Time: Thu 12 Feb 2015 07:35:34 PM WST
 ************************************************************************/
#ifndef POSIX_PROCESS_H
#define POSIX_PROCESS_H
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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: [email protected] 
    > 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: [email protected] 
    > 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

三 程序運行截圖


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved