Lock Variable Synchronization Mechanism
Last Updated :
09 Jan, 2025
A lock variable provides the simplest synchronization mechanism for processes. Some noteworthy points regarding Lock Variables are-
- It's a software mechanism implemented in user mode, i.e. no support required from the Operating System.
- It's a busy waiting solution (process continuously checks for a condition and hence wastes CPU cycles).
- It can be used for more than two processes.
When Lock = 0 implies critical section is vacant (initial value ) and Lock = 1 implies critical section occupied.
The pseudocode looks something like this -
// Entry section
while(lock != 0); // Note the semicolon
Lock = 1;
// critical section
...............................
// Exit section
Lock = 0;
A more formal approach to the Lock Variable method for process synchronization can be seen in the following code snippet :
C++
#include <mutex>
#include <condition_variable>
char buffer[SIZE];
int count = 0,
start = 0,
end = 0;
std::mutex mtx;
std::condition_variable cv;
void put(char c)
{
std::unique_lock<std::mutex> lock(mtx);
while (count == SIZE) {
cv.wait(lock);
}
count++;
buffer[start] = c;
start++;
if (start == SIZE) {
start = 0;
}
cv.notify_all();
}
char get()
{
std::unique_lock<std::mutex> lock(mtx);
while (count == 0) {
cv.wait(lock);
}
count--;
char c = buffer[end];
end++;
if (end == SIZE) {
end = 0;
}
cv.notify_all();
return c;
}
C
char buffer[SIZE];
int count = 0,
start = 0,
end = 0;
struct lock l;
// initialize lock variable
lock_init(&l);
void put(char c)
{
// entry section
lock_acquire(&l);
// critical section begins
while (count == SIZE) {
lock_release(&l);
lock_acquire(&l);
}
count++;
buffer[start] = c;
start++;
if (start == SIZE) {
start = 0;
}
// critical section ends
// exit section
lock_release(&l);
}
char get()
{
char c;
// entry section
lock_acquire(&l);
// critical section begins
while (count == 0) {
lock_release(&l);
lock_acquire(&l);
}
count--;
c = buffer[end];
end++;
if (end == SIZE) {
end = 0;
}
// critical section ends
// exit section
lock_release(&l);
return c;
}
Here we can see a classic implementation of the reader-writer's problem. The buffer here is the shared memory and many processes are either trying to read or write a character to it. To prevent any ambiguity of data we restrict concurrent access by using a lock variable. We have also applied a constraint on the number of readers/writers that can have access.
Now every Synchronization mechanism is judged on the basis of three primary parameters :
- Mutual Exclusion.
- Progress.
- Bounded Waiting.
Of which mutual exclusion is the most important of all parameters. The Lock Variable doesn't provide mutual exclusion in some cases. This fact can be best verified by writing its pseudo-code in the form of an assembly language code as given below.
1. Load Lock, R0 ; (Store the value of Lock in Register R0.)
2. CMP R0, #0 ; (Compare the value of register R0 with 0.)
3. JNZ Step 1 ; (Jump to step 1 if value of R0 is not 0.)
4. Store #1, Lock ; (Set new value of Lock as 1.)
Enter critical section
5. Store #0, Lock ; (Set the value of lock as 0 again.)
Now let's suppose that processes P1 and P2 are competing for Critical Section and their sequence of execution be as follows (initial value of Lock = 0) -
- P1 executes statement 1 and gets pre-empted.
- P2 executes statement 1, 2, 3, 4 and enters Critical Section and gets pre-empted.
- P1 executes statement 2, 3, 4 and also enters Critical Section.
Here initially the R0 of process P1 stores lock value as 0 but fails to update the lock value as 1. So when P2 executes it also finds the LOCK value as 0 and enters Critical Section by setting LOCK value as 1. But the real problem arises when P1 executes again it doesn't check the updated value of Lock. It only checks the previous value stored in R0 which was 0 and it enters critical section.
This is only one possible sequence of execution among many others. Some may even provide mutual exclusion but we cannot dwell on that. According to murphy's law "Anything that can go wrong will go wrong". So like all easy things the Lock Variable Synchronization method comes with its fair share of Demerits but its a good starting point for us to develop better Synchronization Algorithms to take care of the problems that we face here.
Similar Reads
What is OSI Model? - Layers of OSI Model The OSI (Open Systems Interconnection) Model is a set of rules that explains how different computer systems communicate over a network. OSI Model was developed by the International Organization for Standardization (ISO). The OSI Model consists of 7 layers and each layer has specific functions and re
13 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Introduction of ER Model The Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
DBMS Tutorial â Learn Database Management System Database Management System (DBMS) is a software used to manage data from a database. A database is a structured collection of data that is stored in an electronic device. The data can be text, video, image or any other format.A relational database stores data in the form of tables and a NoSQL databa
7 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Types of Network Topology Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. Common types of network topology include bus, star, ring, mesh, and tree topologies, each with its advantages and disadvantages. In this article, we will discuss different types of n
12 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Operating System Tutorial An Operating System(OS) is a software that manages and handles hardware and software resources of a computing device. Responsible for managing and controlling all the activities and sharing of computer resources among different running applications.A low-level Software that includes all the basic fu
4 min read
Computer Network Tutorial A Computer Network is a system where two or more devices are linked together to share data, resources and information. These networks can range from simple setups, like connecting two devices in your home, to massive global systems, like the Internet. Below are the main components of a computer netw
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read