C++ 分叉子,向子进程询问进程列表,在 Linux 中杀死一个进程

C++ Fork child, ask child for process list, kill a process in Linux

本文关键字:进程 一个 Linux 列表 分叉 子进程 C++      更新时间:2023-10-16

>我正在尝试创建一个子进程,向子进程发送命令"LISTALL"。然后,子进程应向系统发出命令 ps,并将该列表返回到父进程。然后,父进程应选择一个进程并终止它。这就是我到目前为止所拥有的,但我在让它运行时遇到了麻烦。

#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
char* getlistOfProcesses(const char* cmd) 
{
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return (char*)"ERROR";
    char buffer[128];
    char *result = new char[1024];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            strcat(result, buffer);
    }
    pclose(pipe);
    return result;
}
int spawnGEdit()
{
    pid_t gPid = fork();
    if(gPid == 0)
    {
        execl("gedit", "gedit", NULL);
        exit(-1);
    }
    else
    {
    }
    return 0;
}
int main(int argc, char **argv)
{   
    int P2C[2];
    int C2P[2];
    pipe(P2C);
    pipe(C2P);
    pid_t cPid = fork();
    char cmd[50];
    char* listOfProcesses = new char[1024];
    spawnGEdit();
    if (cPid == 0)
    {
        close(P2C[1]); 
        close(C2P[0]); 
        read(P2C[0], cmd, 10);
        if(strcmp(cmd,"LISTALL") == 0)
        {
            write(C2P[1], getlistOfProcesses("ps"), 1024);
            close(P2C[0]);
            close(C2P[1]);
        }
    }
    else if (cPid > 0)
    {
        close(C2P[1]); 
        close(P2C[0]); 
        write(P2C[1], "LISTALL", 10);
        wait(NULL);
        read(C2P[0], listOfProcesses,1024);
        printf("%s",listOfProcesses); 
        //TODO
        //get user input of a PID
        //kill the PID
        close(C2P[0]);
        close(P2C[1]);
    }
    else
    {
        // fork failed
        printf("Forking failed!n");
        exit(1);
    }
    return 0;
}

这些是我尝试编译它时遇到的错误:

/tmp/cciTPIOZ.o: In function `getlistOfProcesses(char const*)':
test.cpp:(.text+0x53): undefined reference to `operator new[](unsigned long)'
/tmp/cciTPIOZ.o: In function `main':
test.cpp:(.text+0x166): undefined reference to `operator new[](unsigned long)'
/tmp/cciTPIOZ.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x2c0): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x2cf): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

我正在编译:

cc test.cpp -o test

行号:9,53,64 上发生的编译错误可以通过使用以下方法解决:

第 9 行:FILE* pipe = popen(cmd.data(), "r");

第 53 行:write(C2P[1], getlistOfProcesses("ps").data(), 1024);

第 64 行:printf("%s",listOfProcesses.data());

原因:这些 popen,write,printf 需要 char* 作为它们的参数,但您正在传递它们 std::string。你必须改用 std::string.data() 函数,因为它返回指向由对象表示std::string字符数组的指针。

对于您在第 63 行的错误,请参阅此内容。

PS:- 对于您有问题的编辑:

第 10 行:if (!pipe) return (char*)"ERROR";

第 12 行:char *result = new char[1024];

53 行:(第 7 行更改)char* getlistOfProcesses(const char* cmd)

一些建议:在读取listOfProcesses之前在父进程中使用wait(NULL);,在子进程结束时使用exit(0);

工作代码:

#include <stdio.h>
#include <unistd.h>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <sys/wait.h>
char* getlistOfProcesses(const char* cmd) 
{
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return (char*)"ERROR";
    char buffer[128];
    char *result = new char[1024];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            strcat(result, buffer);
    }
    pclose(pipe);
    return result;
}
int spawnGEdit()
{
    pid_t gPid = fork();
    if(gPid == 0)
    {
        execl("gedit", "gedit", NULL);
        exit(-1);
    }
    else
    {
    }
    return 0;
}
int main(int argc, char **argv)
{   
    int P2C[2];
    int C2P[2];
    pipe(P2C);
    pipe(C2P);
    pid_t cPid = fork();
    char cmd[50];
    char* listOfProcesses = new char[1024];
    spawnGEdit();
    if (cPid == 0)
    {
        close(P2C[1]); 
        close(C2P[0]); 
        read(P2C[0], cmd, 10);
        if(strcmp(cmd,"LISTALL") == 0)
        {
            write(C2P[1], getlistOfProcesses("ps"), 1024);
            close(P2C[0]);
            close(C2P[1]);
        }
        exit(0);
    }
    else if (cPid > 0)
    {
        close(C2P[1]); 
        close(P2C[0]); 
        write(P2C[1], "LISTALL", 10);
        wait(NULL);
        read(C2P[0], listOfProcesses,1024);
        printf("%s",listOfProcesses); 
        //TODO
        //get user input of a PID
        //kill the PID
        close(C2P[0]);
        close(P2C[1]);
    }
    else
    {
        // fork failed
        printf("Forking failed!n");
        exit(1);
    }
    return 0;
}