Skip to content

Commit 7b86428

Browse files
committed
Add Creating A PID as an elixir til
1 parent 0194520 commit 7b86428

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
77
warrant a full blog post. These are mostly things I learn by pairing with
88
smart people at [Hashrocket](http://hashrocket.com/).
99

10-
_511 TILs and counting..._
10+
_512 TILs and counting..._
1111

1212
---
1313

@@ -90,6 +90,7 @@ _511 TILs and counting..._
9090
- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md)
9191
- [Counting Records With Ecto](elixir/counting-records-with-ecto.md)
9292
- [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md)
93+
- [Creating A PID](elixir/creating-a-pid.md)
9394
- [Creating Indexes With Ecto](elixir/creating-indexes-with-ecto.md)
9495
- [Determine The Latest Release Of A Hex Package](elixir/determine-the-latest-release-of-a-hex-package.md)
9596
- [Do You Have The Time?](elixir/do-you-have-the-time.md)

elixir/creating-a-pid.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Creating A PID
2+
3+
Often times, when invoking a function that spawns a process, the PID of the
4+
spawned process is returned and we bind to it. That PID is a reference to
5+
some BEAM process in our system.
6+
7+
We can create our own references using the `pid/3` function.
8+
9+
Let's assume we have the following processes, among others, in our system at
10+
the moment.
11+
12+
```elixir
13+
> Process.list |> Enum.reverse |> Enum.take(3)
14+
[#PID<0.284.0>, #PID<0.283.0>, #PID<0.282.0>]
15+
```
16+
17+
We can create a reference to any of them using the three number parts that
18+
they are made up of.
19+
20+
```elixir
21+
> pid(0, 284, 0)
22+
#PID<0.284.0>
23+
```
24+
25+
See, it's alive.
26+
27+
```elixir
28+
> pid(0, 284, 0) |> Process.alive?
29+
true
30+
```
31+
32+
What if we make up a PID that doesn't actually reference any process?
33+
34+
```elixir
35+
> pid(0, 333, 0) |> Process.alive?
36+
false
37+
```
38+
39+
Note: there is also a `pid/1` version of the function. See `h pid` for more
40+
details.

0 commit comments

Comments
 (0)