Skip to content

Commit 9357f63

Browse files
authored
Merge pull request #7 from acceleratescience/6-add-example-on-process-visibility
split hello world and add visibility
2 parents c35dbe2 + de72d9d commit 9357f63

File tree

5 files changed

+334
-237
lines changed

5 files changed

+334
-237
lines changed

continuous.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import time
2+
from datetime import datetime
3+
4+
def main():
5+
print("Script started. Running continuously...")
6+
print(f"PID: {os.getpid()}")
7+
8+
counter = 0
9+
while True:
10+
counter += 1
11+
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
12+
13+
# Write to a log file so we can monitor it
14+
with open("./script.log", "a") as f:
15+
f.write(f"[{timestamp}] Iteration {counter}\n")
16+
17+
# Sleep for 10 seconds between iterations
18+
time.sleep(10)
19+
20+
if __name__ == "__main__":
21+
import os
22+
main()

docs/Basics/deep-dive.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# A Deeper Dive
2+
In the `hello-world` example we were told to play around with something more ambitious. So let's do that...
3+
4+
## Ubuntu in a Container
5+
6+
```bash
7+
docker run -it ubuntu bash
8+
```
9+
10+
You should now see a new prompt that looks something like this:
11+
12+
```text
13+
root@f4b5c7e4b6b4:/#
14+
```
15+
16+
This prompt indicates that you are now inside a Docker container running a Ubuntu image. The `root@f4b5c7e4b6b4` part is the hostname of the container (yours may be different), and the `/#` part is the command prompt.
17+
18+
Here is an overview of the commands we used:
19+
20+
```bash
21+
docker run # Base command to create and start a new container
22+
-i # Interactive - keep STDIN open (allows you to type into container)
23+
-t # Allocate a pseudo-Terminal (gives you the shell prompt)
24+
ubuntu # The image to use (in this case, official Ubuntu image)
25+
bash # The command to run inside container (start a bash shell)
26+
```
27+
28+
We can combine tags to make the command shorter: `-it` is the same as `-i -t`. Without `-it`:
29+
30+
- `-i` only: You can send input but display will be weird
31+
- `-t` only: You get nice formatting but can't type input
32+
- neither: Container runs the command and exits unless it has a foreground process
33+
34+
A "bash shell" is the command line interface (CLI). It so happens that if we run:
35+
36+
```bash
37+
docker run -it ubuntu
38+
```
39+
we'll also get a bash shell anyway, because this is the default command for the Ubuntu image. However, you can also do:
40+
41+
```bash
42+
docker run -it ubuntu sh
43+
```
44+
45+
to get a simple shell instead of bash. You can also do
46+
47+
```bash
48+
docker run -it ubuntu zsh
49+
```
50+
to get the interface that macOS uses. It is not available for this image, but you can install it in your own images.
51+
52+
When we are inside the container, if we run:
53+
54+
```bash
55+
cat /etc/os-release
56+
```
57+
58+
You should see the following output:
59+
60+
```text
61+
PRETTY_NAME="Ubuntu 24.04.1 LTS"
62+
NAME="Ubuntu"
63+
VERSION_ID="24.04"
64+
VERSION="24.04.1 LTS (Noble Numbat)"
65+
VERSION_CODENAME=noble
66+
ID=ubuntu
67+
ID_LIKE=debian
68+
HOME_URL="https://www.ubuntu.com/"
69+
SUPPORT_URL="https://help.ubuntu.com/"
70+
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
71+
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
72+
UBUNTU_CODENAME=noble
73+
LOGO=ubuntu-logo
74+
```
75+
76+
confirming that you are indeed running a Ubuntu container. This container does not have any additional software installed, so you have a clean Ubuntu environment to work with. To exit the container, you can type `exit` and press `Enter`.
77+
78+
## What can we see?
79+
Currently, in VS Code, we have a single terminal open. If we run
80+
```bash
81+
ps
82+
```
83+
We can see all processes inside our current terminal. It will probably read something like:
84+
```bash
85+
PID TTY TIME CMD
86+
1030 pts/0 00:00:00 bash
87+
34776 pts/0 00:00:00 ps
88+
```
89+
90+
To see all processes, we can either run `htop` for an interactive view, or run
91+
```bash
92+
ps aux
93+
```
94+
95+
The number in the left column `PID` is the Process ID, and everything that is running has a unique ID. To demonstrate, check out the script `continuous.py`. This script simply writes the date and iteration number to a log file. It will keep running until we terminate it.
96+
97+
To run it:
98+
```bash
99+
python continuous.py &
100+
```
101+
102+
The `&` makes sure we are spat back out into the terminal. Now run
103+
```bash
104+
ps
105+
```
106+
and you should see something like
107+
```bash
108+
PID TTY TIME CMD
109+
1035 pts/0 00:00:00 bash
110+
1815 pts/0 00:00:00 python
111+
2065 pts/0 00:00:00 ps
112+
```
113+
If we run `ps aux` we can also find our process:
114+
```bash
115+
codespa+ 1815 0.0 0.1 17172 9984 pts/0 S 15:47 0:00 python continuous.py
116+
```
117+
You should also see the log file getting written too. Notice that if we open up another terminal, we can no longer see this process running when we use `ps`, but we can see it when we run `ps aux`.
118+
119+
OK, now let's go back into our ubuntu container:
120+
```bash
121+
docker run -it ubuntu
122+
```
123+
Once again, we are back at our bash terminal. Now run the `ps` command.
124+
```bash
125+
root@b752bb229154:/# ps
126+
PID TTY TIME CMD
127+
1 pts/0 00:00:00 bash
128+
9 pts/0 00:00:00 ps
129+
```
130+
131+
Well OK, fine, but we're not in the same terminal, so of course we wouldn't see it. But now try running `ps aux`...
132+
```bash
133+
root@b752bb229154:/# ps aux
134+
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
135+
root 1 0.0 0.0 4588 3840 pts/0 Ss 15:49 0:00 /bin/bash
136+
root 10 0.0 0.0 7888 3968 pts/0 R+ 15:52 0:00 ps aux
137+
```
138+
139+
You find that you have access to no information about any processes running outside of your container. If you install and run `htop` you will find the same level of visibility.
140+
141+
Notice that if you run `lscpu` you can still see the system level resources, but it is possible to limit this aswell.
142+
143+
## Further reading
144+
If you want a **real** deep dive into namespaces and cgroups, I strongly recommend you check out the brilliant video from Liz Rice, [Containers From Scratch](https://www.youtube.com/watch?v=8fi7uSYlOdc). There are three versions of this lecture, but they are all good.

0 commit comments

Comments
 (0)