以上就是给各位分享一个subprocess是否可以等待父进程在C中的Linux编程中终止?,其中也会对subprocess等待执行结束进行解释,同时本文还将给你拓展C++forksubprocess,
以上就是给各位分享一个subprocess是否可以等待父进程在C中的Linux编程中终止?,其中也会对subprocess等待执行结束进行解释,同时本文还将给你拓展C ++ forksubprocess,请求subprocess列表,在Linux中杀死进程、c – CreateProcess,使得子进程在父进程被杀时被杀死?、linux python subprocess.popen 引起的僵尸进程 defunct 解决方法、linux – 如果子进程在后台启动并且父进程已经退出,如何知道父进程等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 一个subprocess是否可以等待父进程在C中的Linux编程中终止?(subprocess等待执行结束)
- C ++ forksubprocess,请求subprocess列表,在Linux中杀死进程
- c – CreateProcess,使得子进程在父进程被杀时被杀死?
- linux python subprocess.popen 引起的僵尸进程 defunct 解决方法
- linux – 如果子进程在后台启动并且父进程已经退出,如何知道父进程
一个subprocess是否可以等待父进程在C中的Linux编程中终止?(subprocess等待执行结束)
在Linux的C编程中,我知道wait()函数是用来等待subprocess终止的,但有没有一些方法(或函数)让subprocess等待父进程终止呢?
如何用X11复制到剪贴板?
无法使用for循环创build多个线程
我可以产生附加到正在运行的debugging器的进程吗?
为什么gdb甚至会步入memcpy和其他系统函数?
如果一个孩子的过程在阅读的时候不会从写作中结束,会发生什么?
简短的回答:不。
父进程可以控制其子进程的终端或进程组,这就是为什么我们有wait()和waitpid()函数的原因。 一个孩子对父母没有这种控制,所以没有什么内在的东西。
如果你真的需要一个孩子知道它的父母什么时候退出,你可以让父母发送一个信号给atexit()处理程序中的孩子,让孩子抓住这个信号。
Linux有一个扩展(如在,非POSIX功能)。 查找prctl (“过程相关控制”)。
通过prctl ,您可以安排孩子在父母死亡时获得信号。 查找与prctl使用的PR_SET_PDEATHSIG操作代码。
例如,如果将它设置为SIGKILL信号,它实际上给了我们一个让父母死亡的孩子死亡的方法。 但是,当然,这个信号可以是孩子能够抓住的东西。
prctl可以做各种其他的事情。 这就像是一个以过程本身为目标的ioctl :一个“过程ioctl”。
在Linux中,您可以使用值为PR_SET_PDEATHSIG的prctl建立一个信号,当创建它的线程死亡时,它将被发送到您的进程。 也许你觉得它很有用。
总结
以上是小编为你收集整理的一个subprocess是否可以等待父进程在C中的Linux编程中终止?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
C ++ forksubprocess,请求subprocess列表,在Linux中杀死进程
我试图创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的问题
clone()系统调用是否最终依赖于fork函数?
具体来说,fork()如何在Linux中处理从malloc()dynamic分配的内存?
C多个进程写入1个pipe道
使用pipe道发送多个string到subprocess
从pipe道读取()保证在EOF之前提供所有primefaces写入的数据?
subprocess在使用共享内存时挂起?
如何复制当前进程?
父母怎样才能用一根pipe子给n个孩子发消息?
PHP:pcntl_fork()真的做什么?
编译错误行号:9,53,64可以通过使用这些解决:
第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; }
c – CreateProcess,使得子进程在父进程被杀时被杀死?
也许使用Create Process Flags?
编辑
解决方案是创建作业对象,将父项和子节点都放在作业对象中.没有父母被杀害,孩子被杀.我从这里得到了代码:
Kill child process when parent process is killed
记下@ wilx关于继承句柄的评论.
解决方法
SetInformationJobObject()
在作业对象上设置
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
,使作业拥有进程死机,使得子进程死机.当您的父进程退出/终止时,作业对象句柄将被关闭.为了使其工作,工作句柄不被子进程继承至关重要.如果您还要跟踪大孩子进程,那么您将必须创建暂停的子进程,将它们添加到您的作业对象中,然后让它们运行.
linux python subprocess.popen 引起的僵尸进程 defunct 解决方法
使用 popen 函数的时候,如果不注意的话,可能会引起僵尸进程 defunct 的存在,虽然该进程不占用内存和 cpu,但是会在进程任务管理树上占用一个宝贵的节点。这样就造成了进程名额的资源浪费,所以一定得处理僵尸进程!
下面以 python 为例来说明:
python 脚本如下(zombie.py):
#!/usr/bin/env python
#-*-encoding:UTF-8-*-
import os
import time
import subprocess
if __name__ == '__main__':
p = subprocess.Popen('ls',shell=True,close_fds=True,bufsize=-1,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
file = p.stdout.readlines()
for i in range(0,len(file)):
print file[i]
#end for
while True:
time.sleep(1)
#end while
#end if
运行结果如下:
我们用 top 命令查看此时有没有僵尸进程,结果如下:
用 ps axf 命令查看具体的僵尸进程,结果如下:
查看相关资料后发现,在使用 popen 函数后,需要调用 wait 函数(等待子进程中断或结束),否则就会产生僵尸进程,于是上面的代码做了简单修改
#!/usr/bin/env python
#-*-encoding:UTF-8-*-
import os
import time
import subprocess
if __name__ == '__main__':
p = subprocess.Popen('ls',stderr=subprocess.STDOUT)
file = p.stdout.readlines()
p.wait() # 添加 wait 函数
for i in range(0,len(file)):
print file[i]
#end for
while True:
time.sleep(1)
#end while
#end if
执行结果不变,但是使用 top 名令查看僵尸进程的个数,结果如下:
使用 ps axf 命令查看结果如下:
并无僵尸进程。
wait() 函数的功能:
wait() 会暂时停止目前进程的执行,直到有信号来到或子进程结束。如果在调用 wait() 时子进程已经结束,则 wait() 会立即返回子进程结束状态值。子进程的结束状态值会由参数 status 返回,而子进程的进程识别码也会一快返回。
linux – 如果子进程在后台启动并且父进程已经退出,如何知道父进程
proc / $pid / stat文件不再包含父pid如果已经退出父pid并且它显示1而不是原始父pid
linux$cat /proc/6267/stat 6267 (test3.sh) S 1 6265 ...... # ^ # | # I expected to get the origin parent pid but I get 1
为了快速重现此行为,我们可以使用以下脚本
test2.sh
#!/bin/sh echo "test2=$$" ./test3.sh &
test3.sh
#!/bin/sh echo "test3=$$" sleep 1000
执行:
linux$./test2.sh test2=6318 test3=6319 linux$ps aux | grep test 6319 root 1484 S {test3.sh} /bin/sh ./test3.sh linux$cat /proc/6319/stat 6319 (test3.sh) S 1 6318 2138 34816 6.......
解决方法
今天关于一个subprocess是否可以等待父进程在C中的Linux编程中终止?和subprocess等待执行结束的讲解已经结束,谢谢您的阅读,如果想了解更多关于C ++ forksubprocess,请求subprocess列表,在Linux中杀死进程、c – CreateProcess,使得子进程在父进程被杀时被杀死?、linux python subprocess.popen 引起的僵尸进程 defunct 解决方法、linux – 如果子进程在后台启动并且父进程已经退出,如何知道父进程的相关知识,请在本站搜索。
本文标签: