Concurrency
A process is a running program – code, data (variables, data structures, etc.) with at least one thread of execution.
A thread is a flow of control through the process, plus a private stack for local data.
The basic principle is that making threads is much cheaper than making new processes. Threads are also known as “lightweight processes” and are similar to forking a process. Multiple threads of a process use the same address space, so they share all static and external data, open files, and can make parallel programming easier as they require less memory and use less time for context switching. Threads are used when real-time priority scheduling is needed. RPC server stub routines use threads internally to enable RPC-based servers to handle multiple client requests simultaneously. The threads can also be used by client applications.
Threads are used when you need to synchronise or to protect shared resources (especially in non-reentrant code). Threads are used in modern operating systems software. In UNIX thread processes are used to provide jacket routines for a number of non-reentrant UNIX system calls. In Microsoft Office products like word processor threads are used to check spelling and grammar, format paragraphs, as well as inputting your typing. So threads have helped me write this Blog!
Threaded programming in Java or Python
What do you know about locking, deadlock and semaphores?
Since the textbook uses Java example, I will use Python to look at the same topic.
Semaphores, Locks and Monitors are pieces of software. Locks make sure that two processes do not get into each other’s way when accessing a common resource; they sequence the processes when there are dependencies among them. It is easy to mix up the concepts as a lock is a synchronisation primitive similar to semaphores, by providing mutual exclusion. A lock can either be free or busy initially; a lock must be acquired and be free. The lock is “acquired” before access to a shared variable process is released by the process that acquired it (after it is done accessing that variable). A lock is different from a semaphore where it is not necessary that the process decrementing the semaphore value to say, x, will increment it to x+1 upon finishing with its critical section.
One concept to note when using threads is the boss-worker thread relationship. Think of a timetable clash for my lecture in this subject and a tutorial in another subject. As the boss thread, I spawn a worker thread (casual tutor) to synchronise with the same time slot and give the tutorial, while I give the lecture. The worker thread will halt and it used only during the need for synchronisation of tutorial with lecture. So the poor casual tutor is unemployed again until another worker thread is required.
Threaded programming creates multiple threads of execution in the one running Python program (process). Python – since is an interpreter environment, can also use multiple interpreters to make multiple processes, where communication is through message-passing and synchronisation happens using signals. The steps to follow in threaded programming include:
1. Start off with a main thread.
2. Create other threads as Thread objects.
3. Here is some sample Python code by Horatio Davis:
from threading import *
x = Thread(target = add, args=(3,2))
x.run()
4. This creates a separate thread of control, which calls add(3,2).
5. The original thread keeps going after the run().
6. The target can be any function, with any arguments.
7. The thread dies when the function returns or throws an exception.
If you want to find out more, then start at section 7.4 to 7.7 of the Python Reference Library at http://docs.python.org/library/thread.html.
Transactions
Client/Server Transaction processing such as large and complex B2B e-commerce systems and distributed applications (according to the textbook), faces the ACID test and occurs when a server executes a sequence of operations to be known as transactions. The section on serial equivalence will help to explain why concurrency and transactions goes together for this topic. The whole area of concurrency control for distributed systems also brings with it the chance that a distributed deadlock will occur between several servers. I like the way that Enterprise JavaBeans and Bean code development are introduced as an example of TP monitor technology.
The study you make into concurrency and transactions will ensure that you appreciate the role and functions of the operating system in client/server computing.
0 comments:
Post a Comment