Skip to content
BIASED
Back to topic

Optional

fork/COW vs threads

For 4 tasks choose process or thread; mark address space and COW after fork.

Version
v2
Minutes
~20
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

CA · fork/COW против потоков / fork/COW vs threads

Activity version: 2 · Seed: 7 · Данные: four toy tasks plus a page-array model of COW vs shared-thread memory.

Choose process or thread for each task; after fork+write, COW diverges pages while threads share the write.

Ожидаемые числа
Task Choice Shared AS?
run another program (exec) process no
parallel sum over a shared array thread yes
isolate an untrusted plugin process no
many short workers, shared heap thread yes
after fork, parent writes page 0 child stays [10,11][10,11]; parent [99,99][99,99] COW
two threads write page 0 both see [99,99][99,99] shared
Самопроверка в ноутбуке

Активность не входит в процент освоения.

import numpy as np
 
rng = np.random.default_rng(7)
 
tasks = [
    ("exec_other_program", "process", False),
    ("parallel_sum_shared_array", "thread", True),
    ("isolate_untrusted_plugin", "process", False),
    ("many_short_workers_shared_heap", "thread", True),
]
assert [kind for _, kind, _ in tasks] == ["process", "thread", "process", "thread"]
assert [shared for _, _, shared in tasks] == [False, True, False, True]
 
# Shared physical pages until a write; then the writer gets a private copy (COW).
phys = {
    0: np.array([10, 11], dtype=int),
    1: np.array([20, 21], dtype=int),
    2: np.array([30, 31], dtype=int),
    3: np.array([40, 41], dtype=int),
}
next_frame = 4
parent_map = {i: i for i in range(4)}
child_map = {i: i for i in range(4)}
cow = {i: True for i in range(4)}
 
 
def write(maps, pid, vpn, new_row):
    global next_frame
    frame = maps[pid][vpn]
    if cow[frame]:
        phys[next_frame] = phys[frame].copy()
        maps[pid][vpn] = next_frame
        cow[frame] = False
        cow[next_frame] = False
        next_frame += 1
        frame = maps[pid][vpn]
    phys[frame] = np.array(new_row, dtype=int)
 
 
maps = {"parent": parent_map, "child": child_map}
write(maps, "parent", 0, [99, 99])
parent_page = phys[parent_map[0]]
child_page = phys[child_map[0]]
print("parent", parent_page, "child", child_page)
assert list(parent_page) == [99, 99]
assert list(child_page) == [10, 11]
assert parent_map[0] != child_map[0]
 
# Threads share one address space: one write is visible to both views.
shared = np.array([[10, 11], [20, 21]], dtype=int)
t1 = t2 = shared
t1[0] = np.array([99, 99])
assert list(t2[0]) == [99, 99]
assert int(rng.integers(0, 100)) == 94
print("OK: fork/COW vs threads")
 
Самопроверка в ноутбуке
  • 4 задачи: process / thread / process / thread
  • после fork+write ребёнок сохраняет [10,11][10,11]
  • потоки оба видят [99,99][99,99]