Skip to content

Latest commit

 

History

History
176 lines (112 loc) · 8.44 KB

process_drills.livemd

File metadata and controls

176 lines (112 loc) · 8.44 KB

Process Drills

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Process Drills

Drills help you develop familiarity and muscle memory with syntax through repeated exercises. Unlike usual problems, Drills are not intended to develop problem solving skills, they are purely for developing comfort and speed.

This set of drills is for the Process module. Follow the instructions for each drill and complete them as quickly as you can.

Process.send/3

Use Process.send/3 and self() to send the process for the Elixir cell below a :message message. Use receive to receive the message in the same cell.

Use Process.send/3 and self() to send the process for the Elixir cell below a message with a value i.e. {:message, "value"}. Use receive to receive the message in the same cell and return the value.

Process.spawn/2

Use Process.spawn/2 to spawn a new process which adds two integers together.

Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds, then prints "Finished!".

Use Process.spawn/2 and receive to spawn a process that receives a :message message. Use Process.send/3 to send the spawned process a :message message. The spawned process should print "received a message!".

Use Process.spawn/2 and receive to spawn a process that receives a message with a value i.e. {:message, "value"}. Use Process.send/3 to send the spawned process a message with a value. The spawned process should print the received value.

Use Process.spawn/2 to spawn a process that raises an error. Notice it does not crash the Livebook, because it is an unlinked process.

Use Process.spawn/3 and Process.sleep/1 to spawn a process which raises an error after one second. Use Process.link/1 to link the process.

Livebook should crash. Comment out your solution so that you can move on.

Process.alive?/1

Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Use Process.alive?/1 and Process.sleep/1 to check if the process is alive after two seconds. Process.alive?/1 should return true.

Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Use Process.alive?/1 and Process.sleep/1 to check if the process is alive after six seconds. Process.alive?/1 should return false.

Process.send_after/4

Use Process.send_after/4 and self() to send the process for the Elixir cell below a message after two seconds. Use receive in the same cell to receive the message.

Use Process.spawn/3 and receive to spawn a process that waits to receive a message. Use Process.send_after/4 to send the spawned process a message after two seconds.

Process.exit/2

Use Process.spawn/2 and Process.sleep/1 to spawn a process that sleeps for five seconds. Print "I started" before sleeping, and "I finished" after sleeping to prove if the process finishes sleeping or not. Use Process.exit/2 with the :normal exit reason to kill the spawned process.

Once finished, switch the exit reason to :kill to demonstrate that the process is unlinked, because it does not crash the calling process.

Use Kernel.spawn_link/1 and Process.sleep/1 to spawn a linked process that sleeps for five seconds. Use Process.exit/2 with the :normal exit reason to kill the spawned process.

Notice that the Livebook does not crash. Switch the exit reason to :kill and notice that the Livebook does crash. Comment out your solution to avoid crashing Livebook.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Process Drills exercise"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation