0% found this document useful (0 votes)
6 views27 pages

Con Currency

The document discusses concurrency in software execution, outlining four levels: instruction, statement, unit, and program level. It covers concepts such as tasks, synchronization, and various methods for managing concurrency, including semaphores, monitors, and message passing. Additionally, it highlights the importance of managing shared resources and the challenges of competition and cooperation synchronization.

Uploaded by

aniketkkr217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

Con Currency

The document discusses concurrency in software execution, outlining four levels: instruction, statement, unit, and program level. It covers concepts such as tasks, synchronization, and various methods for managing concurrency, including semaphores, monitors, and message passing. Additionally, it highlights the importance of managing shared resources and the challenges of competition and cooperation synchronization.

Uploaded by

aniketkkr217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Concurrency

S.Venkatesan

Reference: Robert W. Sebesta, Concepts of Programming Languages, 10th Edition, Pearson


Introduction
Concurrency in software execution can occur at four different
levels:
• Instruction level (executing two or more machine instructions
simultaneously),
• Statement level (executing two or more high-level language
statements simultaneously),
• Unit level (executing two or more subprogram units simultaneously),
• Program level (executing two or more programs simultaneously).

C Language
•OpenMP is simpler and provides automatic handling of parallelism.
•pthreads gives you low-level control, but requires more effort to manage threads manually.
Example
• One of the most commonly used programs is
Web browsers, whose design is based heavily on
concurrency.

• Browsers must perform many different functions


at the same time, among them
– sending and receiving data from Web servers,
– rendering text and images on the screen, and
– reacting to user actions with the mouse and the
keyboard.
Category
• Physical Concurrency – More than one processor available.

• Logical Concurrency - Interleaved fashion on a single


processor

From the programmer’s and language designer’s points of


view, logical concurrency is the same as physical concurrency.

It is the language implementor’s task, using the capabilities of


the underlying operating system, to map the logical
concurrency to the host hardware.
Terminologies
• Thread of Control – in a program is the
sequence of program points reached as
control flows through the program.

• Quassi Concurrent - Programs that have


coroutines but no concurrent sub-program.

• Multithread - Have more than one thread of


control
Statement-level concurrency
• It is a relatively simple concept.

• In a common use of statement-level concurrency, loops


that include statements that operate on array elements
are unwound so that the processing can be distributed
over multiple processors.

• A loop that executes 500 repetitions and includes a


statement that operates on one of 500 array elements
may be unwound so that each of 10 different
processors can simultaneously process 50 of the array
elements.
Fundamental Concepts
• A task is a unit of a program, similar to a subprogram,
that can be in concurrent execution with other units of
the same program.

• Each task in a program can support one thread of


control.

• Tasks are sometimes called processes.

• In some languages, for example Java and C#, certain


methods serve as tasks. Such methods are executed in
objects called threads.
Task vs Subprogram
• First, a task may be implicitly started, whereas a
subprogram must be explicitly called.

• Second, when a program unit invokes a task, in


some cases it need not wait for the task to
complete its execution before continuing its own.

• Third, when the execution of a task is completed,


control may or may not return to the unit that
started that execution.
Task Categories
• Heavyweight task executes in its own address
space.

• Lightweight tasks all run in the same address


space
Variables
• A task can communicate with other tasks through shared
nonlocal variables, through message passing, or through
parameters.

• If a task does not communicate with or affect the execution


of any other task in the program in any way, it is said to be
disjoint.

• Because tasks often work together to create simulations or


solve problems and therefore are not disjoint, they must
use some form of communication to either synchronize
their executions or share data or both
Synchronization
• Cooperation synchronization (producer-consumer
problem) is required between task A and task B when task
A must wait for task B to complete some specific activity
before task A can begin or continue its execution.

– Problem I: producer cannot put the value if buffer has data.


– Problem II: Consumer cannot fetch it the buffer empty

• Competition synchronization is required between two


tasks when both require the use of some resource that
cannot be simultaneously used.
Race condition

Task A: TOTAL += 1
Task B: TOTAL *= 2.
Possession
• One general method for providing mutually exclusive access (to
support competition synchronization) to a shared resource - a task
can possess and allow only a single task to possess it at a time.

• To gain possession of a shared resource, a task must request it.

• Possession will be granted only when no other task has possession.

• While a task possesses a resource, all other tasks are prevented


from having access to that resource.

• When a task is finished with a shared resource that it possesses, it


must relinquish that resource so it can be made available to other
tasks.
Possession Types
• Semaphores
• Monitor
• Message passing
Scheduler
• If there were never any interruptions and
tasks all had the same priority, the scheduler
could simply give each task a time slice,
– such as 0.1 second, and when a task’s turn came,
the scheduler could let it execute on a processor
for that amount of time.
States of Task
• New
• Ready
• Running
• Blocked
• Dead
Deadlock
• Loss of Liveness.

• In the environment of sequential programs, a


program has the liveness characteristic if it
continues to execute, eventually leading to
completion.
Design Issues
• Competition and cooperation synchronization.

• Task scheduling

• How and when tasks start and end their


executions, and how and when they are
created.
Semaphores
• A guard is a linguistic device that allows the guarded code to be
executed only when a specified condition is true.

• So, a guard can be used to allow only one task to access a particular
shared data structure at a time.

• A semaphore is an implementation of a guard.

• Specifically, a semaphore is a data structure that consists of an


integer and a queue that stores task descriptors.

• A task descriptor is a data structure that stores all of the relevant


information about the execution state of a task.
Cooperation Synchronization
• emptyspots—can use its counter to maintain the number of empty
locations in a shared buffer used by producers and consumers.

• fullspots—can use its counter to maintain the number of filled


locations in the buffer.

• The queues of these semaphores can store the descriptors of tasks


that have been forced to wait for access to the buffer.

• The queue of emptyspots can store producer tasks that are waiting
for available positions in the buffer;

• the queue of fullspots can store consumer tasks waiting for values
to be placed in the buffer.
Cooperation
semaphore fullspots, emptyspots;
fullspots.count = 0;
emptyspots.count = BUFLEN;
task producer;
loop
-- produce VALUE –
wait(emptyspots); { wait for a space }
DEPOSIT(VALUE);
release(fullspots); { increase filled spaces }
end loop;
end producer;

task consumer;
loop
wait(fullspots); { make sure it is not empty }
FETCH(VALUE);
release(emptyspots); { increase empty spaces }
-- consume VALUE –
end loop
end consumer;
Competition
• The wait statement allows the access only if the
semaphore’s counter has the value 1, which
indicates that the shared buffer is not currently
being accessed.

• If the semaphore’s counter has a value of 0,


there is a current access taking place, and the
task is placed in the queue of the semaphore.

• Notice that the semaphore’s counter must be


initialized to 1.
Monitor
• Competition Synchronization –
a) shared data is resident in the monitor rather than in
any of the client units.
b) Implementation of a monitor can be made to
guarantee synchronized access by allowing only one
access at a time.
c) Calls to monitor procedures are implicitly blocked and
stored in a queue if the monitor is busy at the time of
the call.
Monitor
Message Passing
• Message passing can be either synchronous or asynchronous.

• The basic concept of synchronous message passing is that tasks are


often busy, and when busy, they cannot be interrupted by other
units.

• The alternative is to provide a linguistic mechanism that allows a


task to specify to other tasks when it is ready to receive messages.
– This approach is somewhat like an executive who instructs his or her
secretary to hold all incoming calls until another activity, perhaps an
important conversation, is completed.
– Later, the executive tells the secretary that he or she is now willing to
talk to one of the callers who has been placed on hold.
Rendezvous
• A task can be designed so that it can suspend its execution at some point, either
because it is idle or because it needs information from another unit before it can
continue.

• This is like a person who is waiting for an important call.

• In some cases, there is nothing else to do but sit and wait.

• However, if task A is waiting for a message at the time task B sends that message,
the message can be transmitted. This actual transmission of the message is called
a rendezvous.

• Note that a rendezvous can occur only if both the sender and receiver want it to
happen.

• During a rendezvous, the information of the message can be transmitted in either


or both directions.
Thank you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy