#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include "Process.hpp"
Process::Process(const std::vector<char*>& args, bool verbose) :
verbose(verbose),
m_name(args[0]),
m_pid((pid_t)NULL),
m_writepipe {-1,-1},
m_readpipe {-1,-1},
m_pwrite((FILE*)NULL),
m_pread((FILE*)NULL)
{
if ((m_pid = fork()) < 0)
{
perror("Process fork");
throw std::string("Process fork");
} else if ( m_pid == 0 ) {
/* child process */
execvp(args[0], const_cast<char**>(&args[0]));
perror("Process execvp");
throw std::string("Process execvp");
} else {
/* parent process */
if (verbose)
std::cerr << "Process " << m_name << ": forked PID " << m_pid << std::endl;
}
};
Process::~Process()
{
if (verbose)
std::cerr << "Process " << m_name << ": Entering ~Process()" << std::endl;
kill(m_pid, SIGTERM);
int status;
pid_t pid = waitpid(m_pid, &status, 0);
if (pid < 0)
perror("~Process waitpid");
if (verbose)
std::cerr << "Process " << m_name << ": Leaving ~Process()" << std::endl;
};
没有合适的资源?快使用搜索试试~ 我知道了~
封装Unix进程的基本进程类_C++_下载.zip
共6个文件
cpp:3个
readme:1个
makefile:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 36 浏览量
2023-04-26
11:03:53
上传
评论
收藏 3KB ZIP 举报
温馨提示
封装Unix进程的基本进程类_C++_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
封装Unix进程的基本进程类_C++_下载.zip (6个子文件)
process-master
Process.hpp 1KB
Process.cpp 1KB
README 322B
process_test.cpp 955B
Makefile 459B
child.cpp 436B
共 6 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功