Setting process name

Arun Chandrasekaran aruncxy at gmail.com
Thu May 2 15:50:08 UTC 2019


On Wed, May 1, 2019 at 7:50 PM Anonymouse via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:
>
> Is there a way of setting the process/thread name that's neater
> than this?
>
> import core.sys.posix.pthread;
>
> extern(C) int pthread_setname_np(pthread_t, const char*);
>
> void main()
> {
>      import std.string : toStringz;
>      pthread_setname_np(pthread_self(), toStringz("thread_name"));
>      // ...
> }
>
> It seems to work, but it also seems peculiar that
> pthread_setname_np isn't in core.sys.posix.pthread with the rest.
> Why is this? Is there an easier way? Thread.getThis().name
> doesn't seem to be it.
>
>
> https://forum.dlang.org/thread/gudwwmmdcqrfhbduxruq@forum.dlang.org

prctl can set and get the process name and thread name on Linux. You
can do something like this:

import core.sys.linux.sys.prctl;
byte[16] name = cast(byte[]) "Blabla";
name[name.length-1] = 0;
prctl(PR_SET_NAME, cast(size_t) name.ptr, cast(size_t) null,
cast(size_t) null, cast(size_t) null);

// to confirm it works fine
byte[16] newname;
prctl(PR_GET_NAME, cast(size_t) newname.ptr, cast(size_t) null,
cast(size_t) null, cast(size_t) null);
int i;
foreach (b; newname)
{
        assert(b == name[i]);
            i++;
}
writeln(cast(string) newname);


More information about the Digitalmars-d-learn mailing list