Synchronization (Computer Science)
Synchronization (Computer Science)
1
2 3 MINIMIZING SYNCHRONIZATION
In addition to mutual exclusion and memory consistency, The barrier synchronization wait function for ith thread
Java synchronized blocks enable signaling, sending events can be represented as:
from those threads, which have acquired the lock and exe- (Wbarrier)i = f ((Tbarrier)i, (Rthread)i)
cute the code block to those which are waiting for the lock
within the block. This means that Java synchronized sec- Where Wbarrier is the wait time for a thread, Tbarrier
tions combine functionality of mutexes and events. Such is the number of threads has arrived, and Rthread is the
primitive is known as synchronization monitor. arrival rate of threads.[10]
Any object is ne to be used as a lock/monitor in Java. Experiments show that 34% of the total execution time is
[9]
The declaring object is implicitly implied as lock object spent in waiting for other slower threads.
when the whole method is marked with synchronized.
The .NET framework has synchronization primitives.
Synchronization is designed to be cooperative, demand-
ing that every thread or process follow the synchro- 3.4.3 Semaphores
nization mechanism before accessing protected resources
(critical section) for consistent results. In .NET, locking,
Main article: Semaphore_(programming)
signaling, lightweight synchronization types, spinwait and
interlocked operations are some of mechanisms related to
synchronization.[7] Semaphores are signalling mechanisms which can allow
one or more threads/processors to access a section. A
Semaphore has a ag which has a certain xed value as-
3.4 Implementation of Synchronization sociated with it and each time a thread wishes to access
the section, it decrements the ag. Similarly, when the
3.4.1 Spinlock thread leaves the section, the ag is incremented. If the
ag is zero, the thread cannot access the section and gets
Main article: Spinlock blocked if it chooses to wait.
Some semaphores would allow only one thread or process
Another eective way of implementing synchronization is in the code section. Such Semaphores are called binary
by using spinlocks. Before accessing any shared resource semaphore and are very similar to Mutex. Here, if the
or piece of code, every processor checks a ag. If the value of semaphore is 1, the thread is allowed to access
ag is reset, then the processor sets the ag and continues and if the value is 0, the access is denied.[11]
executing the thread. But, if the ag is set (locked), the
threads would keep spinning in a loop and keep checking
if the ag is set or not. But, spinlocks are eective only
if the ag is reset for lower cycles otherwise it can lead
to performance issues as it wastes many processor cycles 3.5 Mathematical foundations
waiting.[8]
Synchronization was originally a process-based concept
whereby a lock could be obtained on an object. Its pri-
3.4.2 Barriers
mary usage was in databases. There are two types of (le)
Main article: Barrier_(computer_science) lock; read-only and readwrite. Read-only locks may be
obtained by many processes or threads. Readerswriter
locks are exclusive, as they may only be used by a single
Barriers are simple to implement and provide good re- process/thread at a time.
sponsiveness. They are based on the concept of imple-
menting wait cycles to provide synchronization.Consider Although locks were derived for le databases, data is also
three threads running simultaneously, starting from bar- shared in memory between processes and threads. Some-
rier 1. After time t, thread1 reaches barrier 2 but it still times more than one object (or le) is locked at a time.
has to wait for thread 2 and 3 to reach barrier2 as it does If they are not locked simultaneously they can overlap,
not have the correct data. Once, all the threads reach bar- causing a deadlock exception.
rier 2 they all start again. After time t, thread 1 reaches Java and Ada only have exclusive locks because they are
barrier3 but it will have to wait for threads 2 and 3 and thread based and rely on the compare-and-swap processor
the correct data again. instruction.
Thus, in barrier synchronization of multiple threads there An abstract mathematical foundation for synchronization
will always be a few threads that will end up waiting for primitives is given by the history monoid. There are also
other threads as in the above example thread 1 keeps wait- many higher-level theoretical devices, such as process
ing for thread 2 and 3. This results in severe degradation calculi and Petri nets, which can be built on top of the
of the process performance.[9] history monoid.
4 4 DATA SYNCHRONIZATION
Windows provides:
4 Data synchronization
interrupt masks, which protect access to global re-
sources (critical section) on uniprocessor systems; Main article: Data synchronization
A distinctly dierent (but related) concept is that of data
spinlocks, which prevent, in multiprocessor systems,
spinlocking-thread from being preempted;
Linux provides:
semaphores;
spinlocks;
turnstiles, queue of threads which are waiting on ac- File synchronization, such as syncing a hand-held
quired lock. MP3 player to a desktop computer;
RAID, where data is written in a redundant fashion in the source system which captures the data, the security
across multiple disks, so that the loss of any one disk and information access privileges must be enforced on the
does not lead to a loss of data; target systems as well to prevent any potential misuse of
the information. This is a serious issue and particularly
Database replication, where copies of data on a when it comes for handling secret, condential and per-
database are kept in sync, despite possible large ge- sonal information. So because of the sensitivity and con-
ographical separation; dentiality, data transfer and all in-between information
must be encrypted.
Journaling, a technique used by many modern le
systems to make sure that le metadata are updated
on a disk in a coherent, consistent manner.
4.1.4 Data quality
4.1 Challenges in data synchronization Data quality is another serious constraint. For better
management and to maintain good quality of data, the
Some of the challenges which user may face in data syn- common practice is to store the data at one location and
chronization: share with dierent people and dierent systems and/or
applications from dierent locations. It helps in prevent-
ing inconsistencies in the data.
data formats complexity;
real-timeliness;
4.1.5 Performance
data security;
There are ve dierent phases involved in the data syn-
data quality; chronization process:
performance.
data extraction from the source (or master, or main)
system;
4.1.1 Data formats complexity
data transfer;
When we start doing something, the data we have usually
is in a very simple format. It varies with time as the orga- data transformation;
nization grows and evolves and results not only in building
a simple interface between the two applications (source data load to the target system.
and target), but also in a need to transform the data while
passing them to the target application. ETL (extraction Each of these steps is very critical. In case of large
transformation loading) tools can be very helpful at this amounts of data, the synchronization process needs to be
stage for managing data format complexities.. carefully planned and executed to avoid any negative im-
pact on performance.
4.1.2 Real-timeliness
[4] { { | url=http://hpcg-benchmark.org/ } }
7 External links
Anatomy of Linux synchronization methods at IBM
developerWorks
8.2 Images
File:Commons-logo.svg Source: https://upload.wikimedia.org/wikipedia/en/4/4a/Commons-logo.svg License: PD Contributors: ? Origi-
nal artist: ?
File:Data_Synchronization.png Source: https://upload.wikimedia.org/wikipedia/commons/d/dc/Data_Synchronization.png License:
CC BY-SA 4.0 Contributors: Own work Original artist: M.usman.14
File:Folder_Hexagonal_Icon.svg Source: https://upload.wikimedia.org/wikipedia/en/4/48/Folder_Hexagonal_Icon.svg License: Cc-by-
sa-3.0 Contributors: ? Original artist: ?
File:Multiple_Processes_Accessing_the_shared_resource.png Source: https://upload.wikimedia.org/wikipedia/commons/a/a3/
Multiple_Processes_Accessing_the_shared_resource.png License: CC BY-SA 4.0 Contributors: Own work Original artist: M.usman.14
File:Question_book-new.svg Source: https://upload.wikimedia.org/wikipedia/en/9/99/Question_book-new.svg License: Cc-by-sa-3.0
Contributors:
Created from scratch in Adobe Illustrator. Based on Image:Question book.png created by User:Equazcion Original artist:
Tkgd2007
File:Shared_Resource_access_in_synchronization_environment.png Source: https://upload.wikimedia.org/wikipedia/commons/5/
50/Shared_Resource_access_in_synchronization_environment.png License: CC BY-SA 4.0 Contributors: Own work Original artist:
M.usman.14