C ++ Fork child, ask the child to look at the list of processes, kill the process in Linux

I am trying to create a child process, send the command "LISTALL" to the child process. Then the child process should issue the ps command to the system and return this list to the parent process. Then the parent process must select the process and kill it. This is what I still have, but I have problems getting it started.

#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; } 

These are the errors I get when I try to compile them:

 /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 

I am compiling with:

 cc test.cpp -o test 
+5
source share
1 answer

Compilation errors occurred on the line number: 9.53.64 can be solved using the following:

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

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

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

Reason: these popen, write, printf require char * as arguments, but you pass them to std :: string. Instead, you should use the std :: string.data () function, since it returns a pointer to the character array represented by the std::string object.

And for your error on line 63, refer to this .

PS: - For your corrections:

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

line 12: char *result = new char[1024];

line 53: (change in line 7) char* getlistOfProcesses(const char* cmd)

Some tips: use wait(NULL); in the parent process before reading listOfProcesses and exit(0); at the end of the child process.

Work code:

 #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; } 
+2
source

All Articles