实现system函数超时退出的功能

 
 
#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");
 
//  实现system函数超时退出的功能
    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 27888
        std::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 1
        std::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 0
        std::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 0
 
        std::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;
}
 
 
 
 
 
 
 
 
 
 
 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值