开发者

How to track processed launch by a shell from outside the shell?

开发者 https://www.devze.com 2023-03-18 15:47 出处:网络
I launch a shell with exec(\"/bin/sh\") in a C program. How can I track the processed launched by the \"sh\" process ?

I launch a shell with exec("/bin/sh") in a C program. How can I track the processed launched by the "sh" process ? For example if I type 'ls' in this /bin/sh process I'd like to retrieved the pid of the 'ls' and also being notified when it ends.

EDIT开发者_开发技巧:

To make my question more general. How do you track processes launched by a process you launched yourself ?


If you want to be notified directly when a child of yours launches a child of its own (your grandchild) or when this grandchild exits, you have to trace your child. There is no portable way to do this.

On Linux you could use the ptrace() system call to do this. You basically become a debugger attached to the child process, like gdb or strace. Other operating systems have similar facilities. But there are some side effects and its use should preferably be limited to testing and troubleshooting.

The only portable thing you can do is repeatedly watch the output of ps and check it for processes that have your child as their parent, but you may miss short-lived processes that are spawned and quickly exit before you have a chance to notice them, and this approach is also work-intensive.

0

精彩评论

暂无评论...
验证码 换一张
取 消