异想天开

What's the true meaning of light, Could you tell me why

改变进程名字

日期:2015-07-27 16:50:36
  
最后更新日期:2015-07-27 16:53:30
nginx进程,ps的结果一般为: nginx: master process /usr/local/nginx/sbin/nginx。这是通过更改argv[0]的值做到的。在linux系统上面argv和environ是连续存放的,进程内部将argv和environ保存到另外的地方,然后设置该值。
[code lang="cpp"]
* To change the process title in Linux and Solaris we have to set argv[1]
* to NULL and to copy the title to the same place where the argv[0] points to.
* However, argv[0] may be too small to hold a new title. Fortunately, Linux
* and Solaris store argv[] and environ[] one after another. So we should
* ensure that is the continuous memory and then we allocate the new memory
* for environ[] and copy it. After this we could use the memory starting
* from argv[0] for our process title.
*
* The Solaris's standard /bin/ps does not show the changed process title.
* You have to use "/usr/ucb/ps -w" instead. Besides, the UCB ps does not
* show a new title if its length less than the origin command line length.
* To avoid it we append to a new title the origin command line in the
* parenthesis.
[/code]
该过程通过两个函数调用,ngx_setproctitle和ngx_init_setproctitle。ngx_os_init调用第一个初始化函数ngx_init_setproctitle。