Notes: Concurrency | Operating System - Computer Science Engineering (CSE) PDF Download

A sequential program has a single thread of control. Its execution is called a process. A concurrent program has multiple threads of control. They may be executed as parallel processes. This lesson presents main principles of concurrent programming. A concurrent program can be executed by

  • Multiprogramming: processes share one or more processors
  • Multiprocessing: each process runs on its own processor but with shared memory
  • Distributed processing: each process runs on its own processor connected by a network to others

Properties of Concurrent Processes

Concurrent programs are governed by two key principles. These are the principles of "safety" and "liveness".

  • The "safety" principle states "nothing bad can happen".
  • The "liveness" principle states "eventually, something good happens".

 Safety

  • Safety in general means that only one thread can access the data at a time, and ensures that the data remain in a consistent state during and after the operation. Supopose, functions "A" and "B" below run concurrently. What is a resultant value of "x"? var x = 0; function A() {x = x + 1;} function B() {x = x + 2;}
  • x = 3 if operations x = x + 1 and x = x + 2 are atomic, i.e. cannot be interrupted.
  • x = 1,2 or 3 if operations x = x + 1 and x = x + 2 can interrupt one another.

If we read/modify/write a file and allow operations to interrupt one another, the file might be easily corrupted. Safety is ensured by implementing

  1. "mutual exclusion" and
  2. "condition synchronization"
  • When operating on shared data. "Mutual exclusion" means that that only one thread can access the data at a time, and ensures that the data remain in a consistent state during and after the operation.
  •  (atomic update). "Condition synchronization" means that operations may be delayed if shared resources are in the wrong state (e.g., read from empty buffer).

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

 Liveness

  • Mutual exclusion solves many safety issues, but gives rise to other problems, in particular deadlock and starvation. The problem of deadlock arises when a thread holds a lock on one object and blocks attempting to gain a lock on another object, because the second object is already locked by a different thread, which is blocked by the lock the original thread currently holds.

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

  • Both threads settle down to wait for the other to release the necessary lock, but neither thread will ever release their own lock because they are both blocked waiting for the other lock to be released first. Stated like this, it may seem an unlikely occurrence, but in fact, deadlock is one of the most common concurrent programming bugs. 
  • The trouble is that deadlock spreads out through more than two threads and can involve complex interdependencies. Deadlock is an extreme form of starvation. Starvation occurs when a thread cannot proceed because it cannot gain access to a resource it requires.

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

The problems of deadlock and starvation bring us to the next big topic in concurrent programming − liveness. Concurrent programs are also described as having a ‘liveness’ property if there are:

  • No Deadlock: some process can always access a shared resource
  • No Starvation: all processes can eventually access shared resources
  • The liveness property states that eventually something good happens. Deadlocked programs don’t meet this requirement. Liveness is gradational. Programs can be ‘nearly’ dead or ‘not very’ live. Every time you use a synchronized method, you force sequential access to an object.
  •  If you have a lot of threads calling a lot of synchronized methods on the same object, your program will slow down a lot. A programming language must provide mechanisms for Expressing Concurrency:
  • Process creation how do you specify concurrent processes?
  • Communication: how do processes exchange information?
  • Synchronization: how do processes maintain consistency?

Process creation

Most concurrent languages offer some variant of the following Process Creation mechanisms:

  • Co-routines how do you specify concurrent processes?
  • Fork and Join: how do processes exchange information?
  • Cobegin/coend: how do processes maintain consistency?

Co-routines are only pseudo-concurrent and require explicit transfers of control:

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

Co-routines can be used to implement most higher-level concurrent mechanisms. Fork can be used to create any number of processes:
Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

Join waits for another process to terminate. Fork and join are unstructured, so require care and discipline. Cobegin/coend blocks are better structured, but they can only create a fixed number of processes.
Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

The caller continues when all of the co-blocks have terminated.

The document Notes: Concurrency | Operating System - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Operating System.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
10 videos|99 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Notes: Concurrency - Operating System - Computer Science Engineering (CSE)

1. What is concurrency in computer science engineering?
Ans. Concurrency in computer science engineering refers to the ability of a system to execute multiple tasks or processes simultaneously, allowing for efficient utilization of resources and improved performance. It involves managing the execution of multiple threads or processes concurrently, enabling them to run independently and potentially interact with each other.
2. How is concurrency achieved in computer science engineering?
Ans. Concurrency in computer science engineering can be achieved through various techniques such as multi-threading, multiprocessing, and distributed computing. Multi-threading involves dividing a program into multiple threads that can be executed concurrently within a single process. Multiprocessing, on the other hand, involves executing multiple processes simultaneously on multiple processors. Distributed computing involves executing tasks across multiple interconnected computers.
3. What are the benefits of concurrency in computer science engineering?
Ans. Concurrency in computer science engineering offers several benefits, including improved performance, better resource utilization, enhanced responsiveness, and increased scalability. By executing multiple tasks simultaneously, it reduces idle time and maximizes the utilization of system resources, resulting in faster execution times. It also enables systems to handle multiple user requests concurrently, leading to better responsiveness and user experience.
4. What are the challenges or issues associated with concurrency in computer science engineering?
Ans. Concurrency in computer science engineering comes with its own set of challenges and issues. One major challenge is managing shared resources, as concurrent processes or threads may compete for the same resources, leading to conflicts or race conditions. Synchronization and mutual exclusion mechanisms are required to ensure proper access to shared resources. Another challenge is coordinating the execution of concurrent tasks, as incorrect synchronization can cause deadlocks or livelocks, resulting in system failures or performance degradation.
5. How can concurrency be implemented safely in computer science engineering?
Ans. To implement concurrency safely in computer science engineering, several best practices and techniques can be followed. The use of synchronization primitives such as locks, semaphores, and atomic operations can help ensure proper access to shared resources and avoid race conditions. Employing thread-safe data structures and algorithms can also minimize the risk of data corruption or inconsistencies. Additionally, thorough testing and debugging of concurrent code, as well as employing concurrency control mechanisms like deadlock detection and prevention, can contribute to safer implementation of concurrency.
10 videos|99 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Sample Paper

,

mock tests for examination

,

Objective type Questions

,

shortcuts and tricks

,

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

,

Summary

,

Previous Year Questions with Solutions

,

Free

,

Viva Questions

,

ppt

,

Semester Notes

,

Exam

,

past year papers

,

pdf

,

Extra Questions

,

Important questions

,

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

,

Notes: Concurrency | Operating System - Computer Science Engineering (CSE)

,

video lectures

,

MCQs

,

study material

,

practice quizzes

;