当前位置: 首页 > news >正文

c++ fork, execl 参数 logcat | grep

Linux进程编程(PS: exec族函数、system、popen函数)_linux popen函数会新建进程吗-CSDN博客

execvp函数详解_如何在C / C ++中使用execvp()函数-CSDN博客

C语言的多进程fork()、函数exec*()、system()与popen()函数_c语言 多进程-CSDN博客

Linux---fork函数和exec函数_fork exec-CSDN博客

#include <unistd.h>int main() {pid_t childPid = fork(); //创建子进程if (childPid == -1) {perror("fork");return 1;} else if (childPid == 0) {// 子进程的代码char* args[] = {"/bin/sh", "-c", "logcat | grep", NULL};int result = execvp(args[0], args);if (result == -1) {perror("execvp");exit(EXIT_FAILURE);}} else {// 父进程的代码waitpid(childPid, NULL, 0); //等待子进程结束}return 0;
}

#include <iostream>
#include <stdio.h>
#include <unistd.h> // for getpid() function
#include <sys/wait.h>
#include <string.h>
#include <chrono>
#include <thread>
#include <unistd.h>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int main() {char *p = (char*)malloc(5000);strcpy(p, "ifsddsffdsdsdfsfdssdffds");pid_t pid = fork(); // 创建子进程if (pid > 0) {int status;// wait(&status); // 等待子进程结束std::this_thread::sleep_for(std::chrono::milliseconds(3000));// std::string cmd_str = "kill -9  " + std::to_string(pid);//pkill -TERM -P 27888std::string cmd_str = "pkill -TERM -P  " + std::to_string(pid);auto ret = system(cmd_str.c_str());std::cout << "Child process ID is: " << pid << std::endl;} else if (pid == 0) {char *a = (char*)malloc(100);// std::this_thread::sleep_for(std::chrono::milliseconds(3000));#if 1std::string cmd_log = "/home/zeekr/code/test/test";// 关闭标准输入、输出和错误流close(STDIN_FILENO);close(STDOUT_FILENO);close(STDERR_FILENO);// 打开要写入的文件int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);// 设置新的标准输出为指定文件dup2(fd, STDOUT_FILENO);// 执行需要重定向的命令// execlp("/bin/ls", "ls", "-la", NULL);// execlp(cmd_log.c_str(), "test", "3", NULL);// execl(cmd_log.c_str(),"test", "3", ">a.txt 2>&1",NULL);char* args[] = {"/bin/sh", "-c", "logcat | grep xxx", NULL};int result = execvp(args[0], args);if (result == -1) {perror("execvp");exit(EXIT_FAILURE);}// execl("/bin/ls", "ls", "-l", NULL);perror("Child failed to exec ls");//execl后面的代码都不会执行了printf("chlid end\n");exit(0);//让子进程到这里就结束#elif 0std::string cmd_log = "top > a.txt  2>&1 ";auto status = system(cmd_log.c_str());printf("system: %d\n", status);std::this_thread::sleep_for(std::chrono::milliseconds(30000));printf("exit(EXIT_SUCCESS);\n");exit(EXIT_SUCCESS);#elif 0std::string cmd_log = "top > a.txt  2>&1 ";FILE* pipe = popen(cmd_log.c_str(), "r"); // 这里以"ls"命令为例,也可以根据需求修改成其他命令或脚本if (pipe == nullptr) {std::cout << "Failed to create child process." << std::endl;return -1;}// 子进程部分char buffer[256];while (!feof(pipe)) {fgets(buffer, sizeof(buffer), pipe);printf("result: %s\n", buffer);}pclose(pipe); // 关闭管道printf("pclose(pipe); : %s\n", buffer);exit(EXIT_SUCCESS);
#endif} else {std::cerr << "Fork failed!" << std::endl;return -1;}return 0;
}

http://www.lryc.cn/news/277556.html

相关文章:

  • QT:单例
  • IPv6路由协议---IPv6动态路由(OSPFv3-4)
  • 移动通信原理与关键技术学习(4)
  • 第二百五十八回
  • freesurfer-reconall后批量提取TIV(颅内总体积)
  • 【GO】如何用 Golang 的 os/exec 执行 pipe 替换文件
  • 基于Spring-boot-websocket的聊天应用开发总结
  • 2023年度总结 - 职业生涯第一个十年
  • setup 语法糖
  • Javaweb之Mybatis的基础操作的详细解析
  • 知名开发者社区Stack Overflow发布《2023 年开发者调查报告》
  • vue element plus Form 表单
  • zmq_connect和zmq_poll
  • TinyLog iOS v3.0接入文档
  • react-native 配置@符号绝对路径配置和绝对路径没有提示的问题
  • element的Table表格组件树形数据与懒加载简单使用
  • 游戏、设计选什么内存条?光威龙武系列DDR5量大管饱
  • linux磁盘清理_docker/overlay2爆满
  • Redis过期清理策略和内存淘汰机制
  • 2_并发编程同步锁(synchronized)
  • Python 常用模块pickle
  • CentOS 6 制作openssh 9.6 p1 rpm包(含ssh-copy-id、openssl) —— 筑梦之路
  • Tomcat Notes: Deployment File
  • 某邦通信股份有限公司IP网络对讲广播系统挖矿检测脚本
  • uniapp点击跳转传对象
  • 简单用PHP实现微信小程序的游戏功能
  • 某查查请求头参数加密分析(含JS加密算法与Python爬虫源码)
  • 免费用chatGPT
  • 还不会python 实现常用的数据编码和对称加密?看这篇文章就够啦~
  • 简易实现 MyBatis 底层机制