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

TSL vs Semaphore: Key Differences

The document compares Test-and-Set Lock (TSL) and Semaphore, highlighting their definitions, purposes, and mechanisms for achieving mutual exclusion and synchronization. TSL is a low-level hardware primitive that uses busy-waiting, while Semaphore is a high-level abstract data type that can avoid busy waiting and is more efficient in managing process access to shared resources. Key differences include TSL's reliance on atomic operations and lack of fairness, compared to Semaphore's ability to implement fairness and handle complex synchronization problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views3 pages

TSL vs Semaphore: Key Differences

The document compares Test-and-Set Lock (TSL) and Semaphore, highlighting their definitions, purposes, and mechanisms for achieving mutual exclusion and synchronization. TSL is a low-level hardware primitive that uses busy-waiting, while Semaphore is a high-level abstract data type that can avoid busy waiting and is more efficient in managing process access to shared resources. Key differences include TSL's reliance on atomic operations and lack of fairness, compared to Semaphore's ability to implement fairness and handle complex synchronization problems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Comparison Table: TSL vs Semaphore

Basis TSL (Test-and-Set Lock) Semaphore

Definition A hardware-level atomic An abstract data type (ADT) used


instruction used to for signaling mechanisms to
implement mutual coordinate process access to
exclusion. shared resources.

Main Purpose Achieve mutual Synchronize access to critical


exclusion using low- sections and coordinate multiple
level locking. processes.

Type Busy-wait / spinlock Can be of two types: Counting or


mechanism. Binary.

Level Low-level hardware High-level OS or kernel


primitive. abstraction.

Atomicity? Yes, TSL is an atomic Achieved through internal atomic


machine instruction. operations (wait() / signal()).

How It Works Uses TSL instruction to Uses wait() (P) and signal() (V)
atomically check and set operations to decrease or
a flag to lock a resource. increase counter-based resource
availability.

Typical Code c<br>while (TSL(lock)) c<br>wait(S);<br>// critical


Snippet ;<br>// critical section<br>signal(S);
section<br>lock = 0;

Problem Solved Critical section problem Can solve mutual exclusion,


(mutual exclusion only). synchronization, deadlock
avoidance, etc.

Busy Waiting? Yes Not necessarily; blocking


semaphores do not busy wait

Efficiency Inefficient on More efficient since blocked


uniprocessor systems processes are put to sleep.
due to wasted CPU
cycles (busy wait).
Fairness No guaranteed fairness – Fairness can be implemented
can cause starvation. (e.g., FIFO queue).

Example Use Spinlock in multi-core Producer-consumer problem,


Case CPU environments. readers-writers, etc.

Advantages - Simple & fast on - No busy waiting- Handles


multiprocessor systems- complex synchronization- Used in
Atomic hardware all major OSs
support

Disadvantages - Busy wait wastes CPU- - Needs kernel support- Overhead


No scheduling- Not of managing sleep/wake and
suitable for general queues
synchronization

Prevents Race Yes Yes


Conditions?

Can Be Used for No Yes


Signaling?

Used In Low-level OS kernel General OS process/thread


locks or hardware synchronization
interrupt contexts

Note:
 TSL is mostly theoretical in university curricula or used in very low-level
kernel/hardware operations.
 Semaphores are more widely implemented and studied in context of classical
synchronization problems like:
o Producer-Consumer

o Reader-Writer

o Dining Philosophers
o Sleeping Barber
Key Difference:

Feature TSL Semaphore

Lock Mechanism Hardware-based spinlock Abstract data structure

Busy Wait? Yes No (in practical implementations)

Critical Section Protected using atomic test-and-set Protected using wait/signal

You might also like