1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Creating A PID as an elixir til

This commit is contained in:
jbranchaud
2017-03-18 16:29:23 -05:00
parent 0194520d78
commit 7b86428161
2 changed files with 42 additions and 1 deletions

View File

@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
warrant a full blog post. These are mostly things I learn by pairing with
smart people at [Hashrocket](http://hashrocket.com/).
_511 TILs and counting..._
_512 TILs and counting..._
---
@@ -90,6 +90,7 @@ _511 TILs and counting..._
- [Compute md5 Digest Of A String](elixir/compute-md5-digest-of-a-string.md)
- [Counting Records With Ecto](elixir/counting-records-with-ecto.md)
- [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md)
- [Creating A PID](elixir/creating-a-pid.md)
- [Creating Indexes With Ecto](elixir/creating-indexes-with-ecto.md)
- [Determine The Latest Release Of A Hex Package](elixir/determine-the-latest-release-of-a-hex-package.md)
- [Do You Have The Time?](elixir/do-you-have-the-time.md)

40
elixir/creating-a-pid.md Normal file
View File

@@ -0,0 +1,40 @@
# Creating A PID
Often times, when invoking a function that spawns a process, the PID of the
spawned process is returned and we bind to it. That PID is a reference to
some BEAM process in our system.
We can create our own references using the `pid/3` function.
Let's assume we have the following processes, among others, in our system at
the moment.
```elixir
> Process.list |> Enum.reverse |> Enum.take(3)
[#PID<0.284.0>, #PID<0.283.0>, #PID<0.282.0>]
```
We can create a reference to any of them using the three number parts that
they are made up of.
```elixir
> pid(0, 284, 0)
#PID<0.284.0>
```
See, it's alive.
```elixir
> pid(0, 284, 0) |> Process.alive?
true
```
What if we make up a PID that doesn't actually reference any process?
```elixir
> pid(0, 333, 0) |> Process.alive?
false
```
Note: there is also a `pid/1` version of the function. See `h pid` for more
details.