我正在尝试创建子进程,向子进程发送命令"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)