Question

How can I change the Erlang VM to use a random 128 bit value for one of it's pid values?

It seems the largest value at this time that I can set is:

32> pid(1, 32767, 8191).
** exception error: bad argument
     in function  list_to_pid/1
        called as list_to_pid("<1.32767.8191>")
     in call from c:pid/3 (c.erl, line 419)
33> pid(0, 32767, 8191).
<0.32767.8191>

It looks like the generation of pid comes down to something like this in erts/emulator/beam/erl_ptab.h:283:

ERTS_GLB_INLINE Eterm
erts_ptab_make_id(ErtsPTab *ptab, Eterm data, Eterm tag)
{
    HUint huint;
    Uint32 low_data = (Uint32) data;
    low_data &= (1 << ERTS_PTAB_ID_DATA_SIZE) - 1;
    low_data <<= ERTS_PTAB_ID_DATA_SHIFT;
    huint.hval[ERTS_HUINT_HVAL_HIGH] = erts_ptab_data2pix(ptab, data);
    huint.hval[ERTS_HUINT_HVAL_LOW] = low_data | ((Uint32) tag);
    return (Eterm) huint.val;
}
Was it helpful?

Solution

Why do you want to do this? Creating a pid does not guarantee that there is a process with that pid, or that will ever be a process with that pid, only a return from spawn ensures that. Read the answers to Can someone explain the structure of a Pid in Erlang? to get an eplanation what the various fields mean. It will help explain why you can't just set it to any value.

You can set the size of the process table when you start erlang with the '+P Number' option. This gives the maximum value of the second field.

EDIT: Just some more comments about the question and the comments below.

Note that a pid, Process Identifier, is just a reference to a process, it is not the process itself. When you spawn a process you get both a new process and a new pid referring to it. When you create a pid with either pid/3 in the shell or using list_to_pid/1 you get just a pid which may or may not refer to a process.

There is today no way in the BEAM to control which pid you get when you create a process. If you really need this functionality you would have to go in and modify the BEAM internally to do that. Considering the BEAM is structured internally (with a process table) and how a pid is structured that could be very difficult to do. For example one field in a pid is the index of the process in the process table so it is illegal to have to different pids with the same table index.

Wouldn't a better solution instead be to create an indentifier/pid table?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top