網(wǎng)站開發(fā)日常工作廊坊seo推廣
Linux進(jìn)程編程(PS: exec族函數(shù)、system、popen函數(shù))_linux popen函數(shù)會新建進(jìn)程嗎-CSDN博客
execvp函數(shù)詳解_如何在C / C ++中使用execvp()函數(shù)-CSDN博客
C語言的多進(jìn)程fork()、函數(shù)exec*()、system()與popen()函數(shù)_c語言 多進(jìn)程-CSDN博客
Linux---fork函數(shù)和exec函數(shù)_fork exec-CSDN博客
#include <unistd.h>int main() {pid_t childPid = fork(); //創(chuàng)建子進(jìn)程if (childPid == -1) {perror("fork");return 1;} else if (childPid == 0) {// 子進(jìn)程的代碼char* args[] = {"/bin/sh", "-c", "logcat | grep", NULL};int result = execvp(args[0], args);if (result == -1) {perror("execvp");exit(EXIT_FAILURE);}} else {// 父進(jìn)程的代碼waitpid(childPid, NULL, 0); //等待子進(jìn)程結(jié)束}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(); // 創(chuàng)建子進(jìn)程if (pid > 0) {int status;// wait(&status); // 等待子進(jìn)程結(jié)束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";// 關(guān)閉標(biāo)準(zhǔn)輸入、輸出和錯誤流close(STDIN_FILENO);close(STDOUT_FILENO);close(STDERR_FILENO);// 打開要寫入的文件int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);// 設(shè)置新的標(biāo)準(zhǔn)輸出為指定文件dup2(fd, STDOUT_FILENO);// 執(zhí)行需要重定向的命令// 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后面的代碼都不會執(zhí)行了printf("chlid end\n");exit(0);//讓子進(jìn)程到這里就結(jié)束#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"命令為例,也可以根據(jù)需求修改成其他命令或腳本if (pipe == nullptr) {std::cout << "Failed to create child process." << std::endl;return -1;}// 子進(jìn)程部分char buffer[256];while (!feof(pipe)) {fgets(buffer, sizeof(buffer), pipe);printf("result: %s\n", buffer);}pclose(pipe); // 關(guān)閉管道printf("pclose(pipe); : %s\n", buffer);exit(EXIT_SUCCESS);
#endif} else {std::cerr << "Fork failed!" << std::endl;return -1;}return 0;
}