0% found this document useful (0 votes)
5 views3 pages

Forks and Mutex

The document provides Python code examples demonstrating the use of a mutex to prevent race conditions in a counter incrementing scenario, ensuring accurate results when multiple threads modify a shared variable. It also includes an example of using os.fork() to create a child process, detailing the behavior and output of both parent and child processes. The explanations clarify how locks and process forking work in Python to manage concurrency and process creation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Forks and Mutex

The document provides Python code examples demonstrating the use of a mutex to prevent race conditions in a counter incrementing scenario, ensuring accurate results when multiple threads modify a shared variable. It also includes an example of using os.fork() to create a child process, detailing the behavior and output of both parent and child processes. The explanations clarify how locks and process forking work in Python to manage concurrency and process creation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Code Using Mutex to Prevent Race Condition

import threading

counter = 0

lock = threading.Lock() # This acts like a binary semaphore (mutex)

def increment():

global counter

for _ in range(100000):

with lock: # Only one thread can enter this block at a time

counter += 1

# Create threads

t1 = threading.Thread(target=increment)

t2 = threading.Thread(target=increment)

# Start threads

t1.start()

t2.start()

# Wait for both to finish

t1.join()

t2.join()

print("Final counter value:", counter)

Why this works:


 Without lock, threads might both read the same value before either writes, causing
missed updates.
 with lock: ensures one thread at a time modifies counter.

This prevents the race condition and gives the correct final result: 200000.

Python Code Using os.fork()

import os

pid = os.fork()

if pid == 0:

# This block runs only in the child process

print("Child Process")

print("Child PID:", os.getpid()) # Get child's own PID

print("Parent PID:", os.getppid()) # Get parent’s PID

else:

# This block runs only in the parent process

print("Parent Process")

print("Parent PID:", os.getpid()) # Get parent’s own PID

print("Child PID:", pid) # PID returned by fork()

Step-by-Step Execution Explanation:

1. os.fork() creates a new child process.


2. Two processes now exist: parent and child.
3. In the child process:
o os.fork() returns 0.
o os.getpid() prints child’s own PID.
o os.getppid() prints the parent’s PID.
4. In the parent process:
o os.fork() returns the child’s PID.
o os.getpid() prints parent’s own PID.
o pid holds the child’s PID.

You might also like