Dynamically name processes

人走茶凉 提交于 2019-12-11 07:45:12

问题


Is it possible for a program to create dynamically name processes that it starts? Consider the famous fork bomb code:

 :(){ :|:& };:

or

 import os
 while os.fork() or True: os.fork()

Would it be possible to let it generate a new random process name every time that it executes? Which would make it a lot harder to get rid of. It doesn't necessarily have to be in perl or python, I'd love to see examples in other languages too.


回答1:


It is usually possible to set the process name, but this capability varies from OS to OS. In Perl, we can assign to the $0 variable, which changes the current process name:

for my $i (1 .. 5) {
  fork && next;
  $0 = "foobar: $i";
  sleep 5;
  exit;
}

print for grep /foobar/, `ps aux`;

Which produces the output:

1000      1231  0.0  0.0   7836   552 pts/5    S+   13:32   0:00 foobar: 1
1000      1232  0.0  0.0   7836   552 pts/5    S+   13:32   0:00 foobar: 2
1000      1233  0.0  0.0   7836   552 pts/5    S+   13:32   0:00 foobar: 3
1000      1235  0.0  0.0   7836   552 pts/5    S+   13:32   0:00 foobar: 4
1000      1236  0.0  0.0   7836   552 pts/5    S+   13:32   0:00 foobar: 5

or something similar. I do not know how this would be done, but shell and Python should have similar functionality.



来源:https://stackoverflow.com/questions/19220001/dynamically-name-processes

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