简体   繁体   English

如何在C中的Solaris上更改分叉进程的名称?

[英]How to change the name of forked processes on Solaris in C?

I have a server application that forks several child processes. 我有一个分叉多个子进程的服务器应用程序。 When showing the processes with ps , top or prstat they display exactly like the parent process. 当使用pstopprstat显示进程时,它们的显示与父进程完全相同。 I can find out which is parent and child by their pid and ppid but it gets quickly difficult. 我可以通过它们的pidppid找出哪个是父母和孩子,但是很快就变得困难了。 I would like to change slightly the name of the child processes so that I can look quickly which does what. 我想稍微更改子进程的名称,以便我快速查看该执行的操作。

I tried several tricks that all work flawlesly on Linux, but they do not on Solaris. 我尝试了几种技巧,这些技巧都可以在Linux上完美地工作,但是在Solaris上却不能。 Does anyone know how it is possible to do that and preferably in plain C. 有谁知道这样做的可能性,最好是在纯C语言中。

One of the ways would be to create a real executable program for a child process and call one of the exec methods from the fork. 一种方法是为子进程创建一个真正的可执行程序,然后从fork调用exec方法之一。

This way the forked process will be "replaced" with the new executable file. 这样,分叉的进程将被新的可执行文件“替换”。

Something along these lines: 遵循以下原则:

  pid_t child_pid = fork( );

  switch ( child_pid )
  {
  case -1:
    die( );
    return;

  case 0:
    // setup argv ...
    static const char* argv[] =
    {
      "prog_name",
      NULL
    };

    execv( *argv, (char**) argv );
    // No code should be executed beyond this point

    fprintf(
      stderr,
      "%s fork: execv failed: %d (%s)\n",
      argv[ 0 ],
      errno,
      strerror( errno )
    );

    die( );
    return;
  default:
    break;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM