C - Get PID of process opened with popen

Deadly 提交于 2019-12-01 21:19:39

Just write your own implementation of popen that returns the PID. It's much less ugly than some crazy hackery around the existing popen. You can find source code to popen implementations all over the net. Here's one.

You might also be able to use ulimits and other tricks to achieve the desired functionality without using ptrace. You can do this by writing your command to be something akin to "sh -c 'ulimit [ulimit strings]; whatever-command'". ulimit -v takes care of RAM limit. CPU time limit (ulimit -t) might be able to approximate time limit; if not, and if you're willing to rewrite popen, then you can stick a call to setitimer in there. stdout is less straightforward; but if you are logging stdout with a shell redirect then I think ulimit -f will do it. If you're reading child's stdout in the parent you can track it yourself, and kill with a signal if they go too long.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!