Experiment: Simulating Reader–Writer Problem using LPC2148
with Keil RTX
Objective:
To simulate the Reader–Writer synchronization problem using mutex and counters on
LPC2148 running Keil RTX, where:
• Multiple reader tasks can access data simultaneously.
• The writer task requires exclusive access.
Theory
The Reader–Writer problem is a classical synchronization challenge that involves:
• Multiple readers accessing shared data concurrently.
• Writers that need exclusive access to avoid data inconsistency.
Since LPC2148 with Keil RTX lacks condition variables, we simulate behavior using:
• A mutex for protecting the shared counter/data.
• A shared reader count to track active readers.
• A binary semaphore or mutex to block the writer while readers are active.
Requirements:
LPC2148 Development Board
Keil uVision IDE with RTX v4
Flash Magic
UART terminal (optional for I/O)
Breadboard, USB cable, power supply
Key Concepts Simulated
Role Method Used
Readers Multiple tasks, count readers
Writer One task, exclusive access
Shared Resource A variable (e.g., data_value)
Protection reader_count + mutex
Procedure
Create a new Keil RTX project for LPC2148.
Declare:
• reader_count (global)
• mutex to protect reader_count
• write_mutex to control write access
Create:
• 2+ Reader tasks (simulate reading)
• 1 Writer task (simulate writing)
Build and flash to LPC2148 using Flash Magic.
Observe behavior via LEDs or UART.
Code Snippet:
#include <LPC214x.h>
#include <RTL.h>
#include <stdio.h>
OS_MUT mutex;
OS_MUT write_mutex;
int reader_count = 0;
int shared_data = 0;
// Optional UART output
void uart_init() {
PINSEL0 |= 0x05; // Enable TxD0 and RxD0
U0LCR = 0x83; // 8-bit, 1 stop bit, enable DLAB
U0DLL = 97; // 9600 Baud Rate @15MHz
U0LCR = 0x03;
}
void uart_tx(char c) {
while (!(U0LSR & 0x20));
U0THR = c;
void uart_print(char *s) {
while (*s) uart_tx(*s++);
__task void Reader(void) {
char msg[50];
while (1) {
os_mut_wait(&mutex, 0xFFFF); // Protect reader_count
reader_count++;
if (reader_count == 1)
os_mut_wait(&write_mutex, 0xFFFF); // First reader blocks writer
os_mut_release(&mutex);
// --- Read section ---
sprintf(msg, "Reader: Data = %d\n", shared_data);
uart_print(msg);
os_dly_wait(100); // Simulate reading
os_mut_wait(&mutex, 0xFFFF);
reader_count--;
if (reader_count == 0)
os_mut_release(&write_mutex); // Last reader unblocks writer
os_mut_release(&mutex);
os_dly_wait(50);
__task void Writer(void) {
char msg[50];
while (1) {
os_mut_wait(&write_mutex, 0xFFFF); // Exclusive access
shared_data++;
sprintf(msg, "Writer: New Data = %d\n", shared_data);
uart_print(msg);
os_mut_release(&write_mutex);
os_dly_wait(200);
__task void init_task(void) {
uart_init();
os_mut_init(&mutex);
os_mut_init(&write_mutex);
os_tsk_create(Reader, 1);
os_tsk_create(Reader, 1);
os_tsk_create(Writer, 2);
os_tsk_delete_self();
}
int main(void) {
os_sys_init(init_task);
Expected Output (UART Terminal)
Writer: New Data = 1
Reader: Data = 1
Reader: Data = 1
Writer: New Data = 2
Reader: Data = 2
Reader: Data = 2
• Multiple readers read simultaneously.
• Writer writes only when no readers are active.
Result
Successfully simulated the Reader–Writer Problem on LPC2148 using mutexes and
shared state logic with Keil RTX, ensuring proper task synchronization.
Viva Questions:
What is the Reader–Writer Problem?
Why must readers be synchronized with writers?
What does reader_count help with?
What’s the difference between mutex and write_mutex in this context?
How would starvation or deadlock be prevented?
Can multiple writers access the shared data at the same time?