<p><code>os.getpid()</code> in Python is getting the <a href="https://docs.python.org/2/library/os.html" rel="nofollow noreferrer">current process id</a>. The processor id is 'random-ish', in that running the entire test several times will result in a different pid, but within the same test the pid will remain constant. </p>

<p>The <code>%</code> operator is the <a href="https://stackoverflow.com/questions/4432208/how-does-work-in-python">modulus operator</a>, which is essentially chopping off all but the last 3 digits of the processor id. </p>

<p>Together <code>os.getpid() % 999</code> is getting a constant offset and ignoring all but the last 3 digits. This new 3 digit number of added to the constant 11000 to return the port number to use. </p>

CC BY-SA 3.0
2015-04-15T15:57:48.150Z
Ok, but what I don't understand is how a pid can be set inside a configuration file for a process that is non-existent yet? Can you follow my thought? :/
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T16:01:42.490Z
The pid that `os.getpid()` returns is the pid of the Python script at the time that it is being run. Whenever `p2p_port(n)` is called, the id of the process that is currently running is returned. Is that more clear?
JohnDvorak
CC BY-SA 3.0
2015-04-15T16:05:50.673Z
Yes a little bit, since I'm a newbie in this field. So you say everytime this python script is run, it starts many process instances (nodes) of the python programm (not bitcoind!) that have individual process ids?
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T16:14:43.473Z
Looking at the code now, it seems like `util.py` just includes functions that are used by other portions of the test code. So every time other portions of the code call `initialize_datadir()` and `initialize_chain()` (which then call `p2p_port()`), the pids will be the same if they are called within the same process.
JohnDvorak
CC BY-SA 3.0
2015-04-15T21:40:03.853Z
Exactly initialize_datadir() is the point of confusion. My understanding/pattern of creating a network of peers is the following:
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T21:40:37.053Z
Step 1) create config file with free port and rpc port
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T21:41:43.073Z
Step 2) run bitcoind instance separately by specifying independent config files - running bitcoin instantly gets a process id
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T21:43:23.827Z
BUT in the util.py code it seems to me that step1 and 2 are twisted since for writing a config file a pid for the port no. Structure is needed
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T22:36:01.847Z
I think the source of confusion is that os.getpid() is not the process id of bitcoind, but of the Python script. It has nothing to do with the bitcoind pid, and is only a way to make the port *different* between tests.
JohnDvorak
CC BY-SA 3.0
2015-04-16T19:47:51.787Z
Ok. Thanks a lot I did a bit of research basing on your hints so it's much clearer now!
Aliakbar Ahmadi
CC BY-SA 3.0
2015-04-15T13:43:23.287Z
2015-04-15T13:43:23.287Z
2017-05-23T12:34:59.703Z
JohnDvorak
<p>I was looking at <code>../qa/rpc-tests/util.py</code> in order to figure out how arbitrary ports are assigned to each created node.
In the first lines of the code it says:</p>

<blockquote>
  <p>...<br/>def p2p_port(n):<br/>return 11000 + n + <strong>os.getpid()%999</strong><br/>...</p>
</blockquote>

<p>So I'm wondering what exactly os.getpid()%999 does?
Is it instantly looking for a free port?</p>

<p>Thanks in advance!</p>

<json-rpc><python><regtest>
os.getpid() in Bitcoin's python test scripts - What does it actually do?
Aliakbar Ahmadi
# os.getpid() in Bitcoin's python test scripts - What does it actually do?

(Asked by: username_0 on 2015-04-15)

I was looking at `../qa/rpc-tests/util.py` in order to figure out how arbitrary ports are assigned to each created node.
In the first lines of the code it says:

> ...  
> def p2p\_port(n):  
> return 11000 + n + **os.getpid()%999**  
> ...

So I'm wondering what exactly os.getpid()%999 does?
Is it instantly looking for a free port?

Thanks in advance!

**Tags:** json-rpc, python, regtest


---

## 1 answers

### Answer by username_1 on 2015-04-15. Score: 3 | Accepted answer

`os.getpid()` in Python is getting the [current process id](https://docs.python.org/2/library/os.html). The processor id is 'random-ish', in that running the entire test several times will result in a different pid, but within the same test the pid will remain constant.

The `%` operator is the [modulus operator](https://stackoverflow.com/questions/4432208/how-does-work-in-python), which is essentially chopping off all but the last 3 digits of the processor id.

Together `os.getpid() % 999` is getting a constant offset and ignoring all but the last 3 digits. This new 3 digit number of added to the constant 11000 to return the port number to use.

> **Comments:**
> **username_0**: Ok, but what I don't understand is how a pid can be set inside a configuration file for a process that is non-existent yet? Can you follow my thought? :/  
> **username_1**: The pid that `os.getpid()` returns is the pid of the Python script at the time that it is being run. Whenever `p2p_port(n)` is called, the id of the process that is currently running is returned. Is that more clear?  
> **username_0**: Yes a little bit, since I'm a newbie in this field. So you say everytime this python script is run, it starts many process instances (nodes) of the python programm (not bitcoind!) that have individual process ids?  
> **username_1**: Looking at the code now, it seems like `util.py` just includes functions that are used by other portions of the test code. So every time other portions of the code call `initialize_datadir()` and `initialize_chain()` (which then call `p2p_port()`), the pids will be the same if they are called within the same process.  
> **username_0**: Exactly initialize_datadir() is the point of confusion. My understanding/pattern of creating a network of peers is the following:  
> **username_0**: Step 1) create config file with free port and rpc port  
> **username_0**: Step 2) run bitcoind instance separately by specifying independent config files - running bitcoin instantly gets a process id  
> **username_0**: BUT in the util.py code it seems to me that step1 and 2 are twisted since for writing a config file a pid for the port no. Structure is needed  
> **username_1**: I think the source of confusion is that os.getpid() is not the process id of bitcoind, but of the Python script. It has nothing to do with the bitcoind pid, and is only a way to make the port *different* between tests.  
> **username_0**: Ok. Thanks a lot I did a bit of research basing on your hints so it's much clearer now!  

---

