我试图创build一个subprocess,发送subprocess命令“LISTALL”。 subprocess然后应该向系统发出命令ps并将该列表返回给父进程。 父进程应该select一个进程并杀死它。 这是我迄今为止,但我只是得到它运行麻烦。
#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
有pipe道,叉子,dup2的问题
具体来说,fork()如何在Linux中处理从malloc()dynamic分配的内存?
C多个进程写入1个pipe道
使用pipe道发送多个string到subprocess
从pipe道读取()保证在EOF之前提供所有primefaces写入的数据?
subprocess在使用共享内存时挂起?
如何复制当前进程?
父母怎样才能用一根pipe子给n个孩子发消息?
PHP:pcntl_fork()真的做什么?
第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)
有点建议:使用wait(NULL); 在读取listofProcesses和exit(0);之前的父进程中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,1024); close(P2C[0]); close(C2P[1]); } exit(0); } else if (cPid > 0) { close(C2P[1]); close(P2C[0]); write(P2C[1],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; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。