Sama na szczycie…

newsempire24.com 5 dni temu

Samotna Halinka…

Od kilku tygodni Halina obserwowała nową sąsiadkę, która wprowadziła się do klatki naprzeciwko, na parter. Młoda kobieta, około trzydziestki, sama wychowywała czteroletnią córeczkę. Nazywała się Kinga, a jej dziewczynka – Zosia. Po rozwodzie zamieszkały tu same, a Zosia chodziła do przedszkola tuż za blokiem.

Halinka gwałtownie się z Kingą zaprzyjaźniła. Ledwo zaczęły się witać i uśmiechać na korytarzu, a już tydzień później siedziała z Zosią w sobotnie popołudnie, gdy Kinga wybiegła z mieszkania.

— Ona jest spokojna, bawi się lalkami na podłodze, a ty rób swoje — tłumaczyła Kinga, ściskając torbę. — Dzięki, iż pomagasz. Mam dziś ważne spotkanie, wrócę wieczorem.

Halinka tylko wzruszyła ramionami, ale gdy drzwi się zamknęły, dotarło do niej, iż Kinga poszła na randkę.

— No proszę, „ważne spotkanie”… — szepnęła, spoglądając na Zosię, która, jak przepowiedziała matka, już układała zabawki w kącie pokoju.

Halince życie nie układało się po jej myśli. Miała dwadzieścia osiem lat, wiek, gdy inne kobiety rodziły dzieci, a ona nie miała ani męża, ani rodziny.

— To przez to, iż jesteś staroświecka — mówiły przyjaciółki. — Siedzisz w domu, drutujesz, zamiast iść na tańce, spotykać się z ludźmi. Tak można przesiedzieć całą młodość, czekając na księcia z bajki…

Halinka przytakiwała, ale nic nie zmieniała. Wstydziła się swojej lekkiej otyłości, nie uważała się za piękność. Teraz, gdy wieczorami często gościła u siebie Zosię, jeszcze mniej rozumiała, jak Kinga może zostawiać takie cudowne dziecko, by gonić za nowym związkiem.

Dla Haliny rodzina, a zwłaszcza dzieci, były darem od Boga. Pokochała Zosię całym sercem, czytając jej książki, bawiąc się, lepiąc z plasteliny.

— Halciu, nie wiem, jak ci się odwdzięczę — szeptała Kinga, odbierając półśpiącą córkę późnym wieczorem. — Jesteś moim wybawieniem.

— A tata Zosi? — spytała pewnego dnia Halinka. — Odw# Starvation-Free Readers-Writers Problem Solution

## Problem Description:
The Readers-Writers problem involves multiple threads (readers and writers) accessing a shared resource, where readers can read simultaneously, but writers must have exclusive access. Traditional solutions can lead to starvation, where certain threads are perpetually denied access. Our goal is to devise a starvation-free solution ensuring fairness for both readers and writers.

## Solution: Starvation-Free Readers-Writers
To prevent starvation, we use three semaphores:
1. `mutex` – Ensures mutual exclusion for updating the `read_count`.
2. `rw_mutex` – Controls access to the resource for writers and the first/last reader.
3. `queue` – Ensures fairness by forming a FIFO queue for both readers and writers.

### Algorithm:

“`python
# Initialization
read_count = 0
mutex = Semaphore(1) # Controls access to read_count
rw_mutex = Semaphore(1) # Controls access to the resource
queue = Semaphore(1) # Ensures fairness by queuing threads

# Reader Thread
def reader():
queue.wait() # Join the queue
mutex.wait() # Protect read_count
read_count += 1
if read_count == 1:
rw_mutex.wait() # First reader locks resource for writers
mutex.signal()
queue.signal() # Allow other threads to join the queue

# Reading is performed here

mutex.wait()
read_count -= 1
if read_count == 0:
rw_mutex.signal() # Last reader releases the resource
mutex.signal()

# Writer Thread
def writer():
queue.wait() # Join the queue
rw_mutex.wait() # Request exclusive access to the resource
queue.signal() # Allow other threads to join the queue

# Writing is performed here

rw_mutex.signal() # Release the resource
“`

## Key Concepts:
1. **Fairness (No Starvation)**:
– The `queue` semaphore ensures that both readers and writers are served in the order they arrive, preventing indefinite waiting.

2. **Concurrent Reading**:
– Multiple readers can access the resource simultaneously, as long as no writer is active.

3. **Exclusive Writing**:
– Writers gain exclusive access, ensuring no readers or other writers can access the resource during a write operation.

## Benefits:
– **Starvation-Free**: Both readers and writers get fair access.
– **Efficient**: Readers can still read concurrently when no writer is present.
– **Simple and Scalable**: Easy to implement and understand.

## Conclusion:
This solution effectively solves the Readers-Writers problem by ensuring no thread is left waiting indefinitely while maintaining the integrity of the shared resource. By introducing a queue semaphore, we balance access and prevent starvation, making it a robust and fair solution.

Idź do oryginalnego materiału