将创建进程的API-posix_spawn封装成一个进程类
2015-12-14 20:36
381 查看
将创建进程的API封装成一个进程类,用该类生成一个对象,则创建了一个进程;
主要API函数为:
MyProcess.h
MyProcess.cpp
main.cpp
下图才是程序的运行结果:(将下面的运行结果与上图比较,证明程序正确)
主要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); }
下图才是程序的运行结果:(将下面的运行结果与上图比较,证明程序正确)
相关文章推荐
- 让 orangepi 用上Btsync(资源分享工具)好资源不怕和谐!
- JavaBean
- Angular2组件开发—模板的逻辑控制(二)
- 模拟退火求函数最值问题求解
- iOS_开发中遇到的那些问题_3
- 【Beta】Daily Scrum Meeting第七次
- undefined
- QTP中使用强制退出程序语句小记
- PHP读取TXT中文乱码的解决方式
- 【Beta】Daily Scrum Meeting第七次
- PCL—低层次视觉—关键点检测(rangeImage)
- firefox浏览器批处理插件imacros
- linux 搭建git 服务器
- 文本相似度算法
- Zero Copy 简介
- Spring中Bean的4种依赖检查模式
- 网格的学习—1
- Swift的74标准功能
- Python中strip()函数
- uva 10655 - Contemplation! Algebra 【矩阵快速幂】【最坑的输入】