简体   繁体   中英

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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