C++ Fork child, ask child for process list, kill a process in Linux -
i'm trying create child process, send child process command "listall". child process should issue system command ps , return list parent process. parent process should choose process , kill it. have far i'm having trouble getting run.
#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 pid //kill pid close(c2p[0]); close(p2c[1]); } else { // fork failed printf("forking failed!\n"); exit(1); } return 0; }
these errors i'm getting when try compile it:
/tmp/ccitpioz.o: in function `getlistofprocesses(char const*)': test.cpp:(.text+0x53): undefined reference `operator new[](unsigned long)' /tmp/ccitpioz.o: in function `main': test.cpp:(.text+0x166): undefined reference `operator new[](unsigned long)' /tmp/ccitpioz.o: in function `__static_initialization_and_destruction_0(int, int)': test.cpp:(.text+0x2c0): undefined reference `std::ios_base::init::init()' test.cpp:(.text+0x2cf): undefined reference `std::ios_base::init::~init()' collect2: error: ld returned 1 exit status
i'm compiling with:
cc test.cpp -o test
compilation errors occurred on line number:9,53,64 can solved using these:
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 requires char* arguments passing them std::string. have use std::string.data() function instead returns pointer character array represented std::string
object.
and error on line 63, refer this.
ps:- edits in question:
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)
a bit of advice: use wait(null);
in parent process before reading listofprocesses
, exit(0);
@ end of child process.
working 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 pid //kill pid close(c2p[0]); close(p2c[1]); } else { // fork failed printf("forking failed!\n"); exit(1); } return 0; }
Comments
Post a Comment