|
|
Microsoft has
released a series of operating system starting from Windows 95 to
Windows Vista. It was the Microsoft who took the initiative to
launch a multi-tasking operating system on PCs. In older days, the
multi-tasking was non-preemptive. But now the latest Operating
Systems (OS) like Windows NT/2000/Xp/Vista support preemptive
multi-tasking. In multi-tasking environment, many processes can run
simultaneously on the same machine. Microsoft has added a new
concept of thread in OS features. It was introduced to create
parallelism with in a single process. In this article, we will
discuss the processes, threads and thread synchronization.
What is a Process?
A process is a program that is loaded in to the primary memory and
ready to execute. In other words we can say that a Process is a
program in execution. A process is allocated private virtual address
space which includes code segment, data segment, other OS resources
like file pointers, pipes etc. A process is allowed to run with in
its address space. A process can be seen as a single thread of
execution.
What is a Thread?
A thread is another path of execution with in a process. It adds the
parallelism with in a process. There exists a main execution path
with in a process. Creating a thread from a process, creates another
path of execution with in the process. A process can create many
different threads. All these threads are allotted CPU time to run in
parallel. All these threads share the parent process code,
resources, global data items and address space.
We can see how easily we can achieve desired degree of parallelism
using the concept of threads. In the next section, we will discuss
about the synchronization of threads.
Methods for Thread Synchronization
In the multi-threaded environment, it is very much required to
provide some sort of synchronization among the threads otherwise
these threads can create problems which can not be resolved easily.
The writing of multi-threaded code is not for novice programmers, it
is generally written by highly skilled programmers. As we know that
threads do have access to global data structure of the parent
process. If many threads are in execution without synchronization,
the global data structure may be in inconsistent state. To avoid
such problems, there has to be some methods for serialization of the
running thread. The MS Windows OS provides certain methods for
thread synchronization as discussed below.
Mutex
As its name suggest, it is Mutual Exclusion (MUTEX). It means it
provides mutual exclusive rights to update the global data by a
thread. It can be understood by the concept of lock and key. Suppose
all the critical global data is stored in a room and room is locked.
The lock has only one key. It means that room can only be accessed
by the person who has the key. This is how mutual exclusion is
achieved for thread synchronization. Only a single thread can access
the critical global data at a time. These mutex are also OS commands
which are atomic in nature.
Semaphore
Semaphores are similar to mutex, but a mutex allows only one thread
at a time to access the data; a semaphore allows multiple threads to
access its data at the same time. The difference can be understood
by comparing it with the real life situations. Suppose there is a
room with 5 chairs. It means only 5 persons can sit at a time inside
the room. The sixth person has to wait until anyone leaves the room.
In the same fashion, semaphore works. The operating system treats
the semaphores as resources and it keeps a count of it. A thread
makes a request for a semaphore before entering in to critical
section otherwise it has to wait.
Events
Sometimes the threads are designed to perform some task on some
event. These kinds of threads are in sleeping mode until the desired
event has not happened. Once the desired event has happened, the
operating system sends a message to the sleeping thread. The
sleeping thread wakes up and perform the desired task and again go
to sleep mode. This is also one way to handle thread
synchronization.
We have seen how multi-threaded environment generates parallelism
with in a process. There threads run with in the process address
space and share memory and resources. These threads can also have
their local storage to store the thread specific data. Windows OS
offers TLS (Thread Local Storage) API to create dynamic thread
specific data that is bound at run time.
|
|