小编ILi*_*arn的帖子

C++ Fork子,请求子进程列表,在Linux中终止进程

我正在尝试创建子进程,向子进程发送命令"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]; …
Run Code Online (Sandbox Code Playgroud)

c++ linux fork pipe

5
推荐指数
1
解决办法
662
查看次数

标签 统计

c++ ×1

fork ×1

linux ×1

pipe ×1