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

发布于 2024-12-28 15:07:21 字数 278 浏览 8 评论 0原文

我有一个服务器应用程序,它分叉多个子进程。当使用 pstopprstat 显示进程时,它们的显示与父进程完全相同。我可以通过 pidppid 找出父子和子子,但这很快就会变得困难。我想稍微更改一下子进程的名称,以便我可以快速查看哪些进程执行什么操作。

我尝试了几种在 Linux 上都能完美运行的技巧,但在 Solaris 上却行不通。有谁知道如何做到这一点,最好是用纯 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. I can find out which is parent and child by their pid and ppid but it gets quickly difficult. 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. Does anyone know how it is possible to do that and preferably in plain C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2025-01-04 15:07:21

其中一种方法是为子进程创建一个真正的可执行程序,并从 fork 中调用 exec 方法之一。

这样,分叉的进程将被新的可执行文件“替换”。

沿着这些思路:

  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;
 }

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.

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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文