Back
+15 XP
Concept

File Descriptor Manipulation with os.pipe

`os.pipe()` creates a unidirectional anonymous pipe returning low-level read and write file descriptors `(r, w)` at the OS kernel layer 🚰.

python
import os

r, w = os.pipe()

os.write(w, b"Kernel message 🚰")
os.close(w)

data = os.read(r, 100)
os.close(r)

print(data.decode("utf-8"))

# 107. File Descriptor Manipulation with os.pipe `os.pipe()` creates a unidirectional anonymous pipe returning low-level read and write file descriptors `(r, w)` at the OS kernel layer 🚰. ```python import os r, w = os.pipe() os.write(w, b"Kernel message 🚰") os.close(w) data = os.read(r, 100) os.close(r) print(data.decode("utf-8")) ``` Low-level pipes operate with raw integers representing kernel file descriptors, which can be passed to child processes during `fork()`! 🚰 --- ### Question What does `os.pipe()` return? 🧠 - [ ] a. A pair of integer file descriptors `(read_fd, write_fd)` 🚰 - [ ] b. A high-level text stream object - [ ] c. A network socket IP address - [ ] d. A multiprocessing Queue instance --- ### Challenge Import `os`. Create `r, w = os.pipe()`. Write `b'PING'` to `w`. Close `w`. Read 10 bytes from `r` with `os.read(r, 10)`. Close `r`. Print decoded string. 🎯 <details> <summary>Solution</summary> ```python import os r, w = os.pipe() os.write(w, b"PING") os.close(w) data = os.read(r, 10) os.close(r) print(data.decode("utf-8")) ``` </details>

Checkpoint

What does `os.pipe()` return? 🧠

Practice

Import `os`. Create `r, w = os.pipe()`. Write `b'PING'` to `w`. Close `w`. Read 10 bytes from `r` with `os.read(r, 10)`. Close `r`. Print decoded string. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks