Concurrency
We introduce a new abstraction for a single running process: that of a thread. Instead of our classic view of a single point of execution within a program (i.e., a single PC where instructions are being fetched from and executed), a multi-threaded program has more than one point of execution (i.e., multiple PCs, each of which is being fetched and executed from).
Perhaps another way to think of this is that each thread is very much like a separate process, except for one difference: they share the same address space and thus can access the same data.
There are at least two major reasons you should use threads. The first is : parallelism.
The task of transforming your standard single-threaded program into a program that does this sort of work on multiple CPUs is called parallelization
The second reason is a bit more subtle: to avoid blocking program progress due to slow I/O.
Assume this instruction adds a value to a memory location, and the hardware guarantees that it executes atomically.
Issues in Concurrency:
1. Non-atomic
2. Deadlock
3. Blocking
4. Race Conditions
5. Starvation.
Advantages of Concurrency :
- Running of multiple applications –
It enable to run multiple applications at the same time. - Better resource utilization –
It enables that the resources that are unused by one application can be used for other applications. - Better average response time –
Without concurrency, each application has to be run to completion before the next one can be run.
Disadvantages of Concurrency :
- It is required to protect multiple applications from one another.
- It is required to coordinate multiple applications through additional mechanisms.
- Additional performance overheads and complexities in operating systems are required for switching among applications.