c - Linux - 如何在 C 中更改 fork 进程的信息

标签 c linux gcc fork

标题可能听起来有点奇怪,用ps aux我看到了这个:

root     20953  0.0  0.0   9528  1280 ?        Ss   Apr28   0:07 sendmail: accepting connections

其中“接受连接”类似于发送邮件进程的标题。这不是参数,因为 cat/proc/20953/cmdline 返回 sendmail: accepting connections(空格而不是 0x00):

# cat /proc/20953/cmdline |hexdump -C
00000000  73 65 6e 64 6d 61 69 6c  3a 20 61 63 63 65 70 74  |sendmail: accept|
00000010  69 6e 67 20 63 6f 6e 6e  65 63 74 69 6f 6e 73     |ing connections|
0000001f

/proc fs 中的参数用空字节分隔:

# cat /proc/26511/cmdline |hexdump -C
00000000  2f 62 69 6e 2f 62 61 73  68 00 2f 77 65 62 72 6f  |/bin/bash./webro|
00000010  6f 74 2f 70 72 6f 72 61  69 6c 2f 73 63 72 69 70  |ot/prorail/scrip|
00000020  74 73 2f 73 79 6e 63 6c  6f 6f 70 2e 73 68 00     |ts/syncloop.sh.|
0000002f

那么,当我在 C 中执行 fork() 时,我如何设置此进程信息,以便我可以识别哪个是哪个进程?

最佳答案

sendmail 有多种方式,具体取决于系统。请参阅源代码中 sendmail/conf.c 中的 setproctitle:

#define SPT_NONE        0       /* don't use it at all */
#define SPT_REUSEARGV   1       /* cover argv with title information */
#define SPT_BUILTIN     2       /* use libc builtin */
#define SPT_PSTAT       3       /* use pstat(PSTAT_SETCMD, ...) */
#define SPT_PSSTRINGS   4       /* use PS_STRINGS->... */
#define SPT_SYSMIPS     5       /* use sysmips() supported by NEWS-OS 6 */
#define SPT_SCO         6       /* write kernel u. area */
#define SPT_CHANGEARGV  7       /* write our own strings into argv[] */

有关详细信息,请参阅 conf.c 中的 setproctitle 例程。

关于c - Linux - 如何在 C 中更改 fork 进程的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5881556/

相关文章:

android - 使用独立工具链编译的 Libogg(由 NDK 脚本生成)尝试从不正确的文件加载自身 - libogg.so.0 而不是 libogg.so

c - while 循环不会执行

linux - Bash - 意外 token `fi'

gcc - red hat devtoolset-3 中的 g++ 找不到 -lelf

c - 具有 "Independent"目标文件的多个定义

c - C中空格的符号是什么?

c - 为什么读取不终止我的 USB 设备?

linux - 变量返回两个值,如何将它们存储在不同的变量中?

c - 用户级程序应该捕获哪些 "fatal"信号?

gcc - ld 链接时找不到入口符号 main