Results 1 to 2 of 2

Thread: [RESOLVED] [POSIX] fork() and setting process name of new process

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Resolved [RESOLVED] [POSIX] fork() and setting process name of new process

    Does anyone known how to specify the name of a child process that is created using the fork() function?

    I want to be able to fork() 4 times and have each process have a different name to make debugging easier and also so that users can kill the processes by name if a problem occurs.

    I have just found this page (http://www.uofr.net/~greg/processname.html) which would work but is a bit nasty as it depends on the length of the original parent's name. And also it only changes the commandline string not the actual process name.

    My aim is to have something nice in "top" output like...

    parent00
    fork00_01
    fork00_02
    fork00_03
    fork00_04


    Currently all 5 of my processes have the same name and its impossible to tell them apart except by their process IDs, which isn't ideal.

    Any ideas?
    Last edited by wossname; Jun 8th, 2008 at 08:54 AM.
    I don't live here any more.

  2. #2

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [POSIX] fork() and setting process name of new process

    [RESOLVED]

    A colleague of mine found a solution to this. I'm not sure how valid a solution it is though, the linux documentation doesn't appear to acknowledge it until kernel 2.6. This does allegedly work if your compilation box has 2.4 headers... I tested it and it worked beautifully on Centos 4.4.

    Code:
    #include <sys/prctl.h>
    char* myProcessName = "HelloWorld";
    ...
    prctl(PR_SET_NAME, (unsigned long)myProcessName, 0, 0, 0);
    The nasty thing about it is that PR_SET_NAME isn't actually #defined anywhere in 2.4 headers so we had to dig around to find that it's value is 15. Passing 15 into this function does indeed do what we thought it should do. There's a sister value called PR_GET_NAME with a value of 16 but I have not yet had a chance to use that.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width