dt265-2 object oriented software development 2 lecture 5 : concurrency
DESCRIPTION
DT265-2 Object Oriented Software Development 2 Lecture 5 : Concurrency . Lecturer Pat Browne [email protected]. Syllabus. Concurrency Threading and concurrency Thread synchronization Parallel extensions and platforms - PowerPoint PPT PresentationTRANSCRIPT
DT265-2 Object Oriented Software Development 2
Lecture 5 : Concurrency
Lecturer Pat [email protected]
Syllabus
• Concurrency – Threading and concurrency– Thread synchronization– Parallel extensions and platforms
• Programs in this section will be run from the command line. Find where csc.exe is on your machine, say
• C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
• You can add this location you the PATH system variable.• csc.exe file.cs
Processes
• Concurrent programs• The hardware interface• Support for processes• Processes and threads• Processes in language systems• Processes in C#
Sequential & Concurrent programs
• A sequential program has a single thread of control. Its execution is called a process.
• A concurrent program has multiple threads of control. These may be executed as parallel processes.
• Non-determinism: concurrent programs are nondeterministic in their execution order, and may also have nondeterministic results.
• Run-time overhead: thread construction, context switching and synchronization take time
UML state charts & interaction diagrams
What is a concurrent system
• A concurrent system consists of a set of activities in the process of being executed. That is, at a given time activities are at some stage between their start and finish.
• The activities may be competing or cooperating. • Activities cooperate to achieve a common goal
(e.g. they may be part of a larger activity). Concurrent activities may need to cooperate by synchronizing their activities or exchanging data.
What is a concurrent system
• The activities may be competing or cooperating. – Activities cooperate to achieve a common goal
(e.g. they may be part of a larger activity). Concurrent activities may need to cooperate by synchronizing their activities or exchanging data.
– Activities will compete when they share resources but the resources can only be used by one activity at a time, (e.g. printer, updating a database)
8
Devices, Buffers, Interrupts, Poling • External devices are much slower than processors.• Processors must proceed at their fastest rate and not be held up
by slow devices. Devices and processors operate concurrently (in parallel), and exchange data through synchronization.
• Buffers, interrupts and polling help achieve synchronization.• A buffer storage area accessed by both a device and the
processor.• If an activity (process) is interrupted, it stops execution an
interrupt service routine (ISR) is called to carry out the necessary tasks, the state of the process that has been interrupted must be saved in order to be able to resume execution of it later. Upon completion of execution ISR, the previous process or other process will be restarted. Context switching takes significant time.
• Poling is alternative to interrupts.
9
Process states
Runningon a processor
RunnableWaiting for a processor
Blockedwaiting for an event
ScheduledThe process runs
out of time or preempted
An event occurred
Process asked to wait
Each process as a process descriptor:Process idProcess priorityProcess state (blocked, running)Resources allocatedLists the events for which the process may be waiting,Start address
Desirable properties of concurrent systems
• Safety: ensuring consistency ,concurrent processes may corrupt shared data
• Liveness: processes may “starve” if not properly coordinated.
• Fairness: A fairness guarantee states that the next operation in a runnable thread eventually will execute. An implementation technique that is often used to provide fairness guarantee is pre-emption. Pre-emption means running threads are periodically stopped by software in order to let other threads proceed.
Desirable properties of concurrent systems
• Safety = ensuring consistency• A safety property says “nothing bad happens”.
Mutual exclusion: shared resources must be updated atomically
• Condition synchronization: operations may be delayed if shared resources are in the wrong state (e.g., read from empty buffer)
Desirable properties of concurrent systems
• Liveness = ensuring progress• A liveness property says “something good
happens” • No Deadlock: some process can always access
a shared resource• No Starvation: all processes can eventually
access shared resources
Desirable properties of concurrent systems
• Fairness and Liveness are related notions of describing which of a collection of concurrent processes make progress in their execution. A fairness constraint is imposed on (the scheduler of) the system that it fairly select the process to be executed next. A fairness constraint is a condition on executions (paths) of the system model.
Liveness
• Liveness describe the requirement that a process make progress toward a specific goal, the achievement of which depends on the fairness of the system. If a process never gets to execute, it usually cannot reach its goal. Therefore, liveness is often evaluated only along fair paths, i.e. paths that satisfy one of the above three fairness path requirements.
15
Synchronous and asynchronous input and output
• synchronous I/O policy : a process is blocked while it waits for I/O to be completed
• asynchronous I/O policy: a process is allowed to continue its execution and pick up the requested input or acknowledgement of output later
• Some systems offer these two policies as options at the application level.
16
Multi-threaded process implementation• A single program can be subdivided into subtasks that are executed by
separate processes. – One (main) process will be responsible for executing the program, in the
course of its execution, other processes will be created to execute subtasks concurrently.
• Each process must be associated with memory locations that contain its executable code, its data, its references to open files and so on (the process’s resources).
• Some OS allow subtask processes to share the resources of the main process. (lightweight processes) (or threads)– the full overhead associated with a context switch is avoided.
• main processes are referred to as heavyweight processes (or simply processes) – a context switch between such processes will incur the full overhead of
changing the resource information.• A multi-threaded process : A (heavyweight) process that is split into many
lightweight processes • A multi-threaded operating system :an operating system that supports
lightweight processes
17
Concurrent programming language with OS support for use thread as kernel thread
• Operating system knows about and manages the threads created by each process.
Operating system kernel
Language runtimesystem
Language runtimesystem
One program ,many user threads
One program ,many user threads
18
Threads• Multi-tasking: The ability of a computer to carry out more than
one task or program concurrently. (tasks is heavyweight processes.• Multi-threading: an individual program which can be divided up
into a number of concurrent tasks. (task is lightweight processes (or thread)).
• For user friendly programs – While program is running, it interacts with user.– To do this
• The execution of the program is divided into a separate thread, leaving the main program thread free to listen for user activity.
• In C#, threads are created using the Thread Class. Design choice :– Define class as subclass of Thread, and override the method run, which is
the method that is executed to perform the task assigned to a thread.– Or– Declare a thread as implementing the Runnable interface
C# Thread Pool
• A thread pool is a collection of threads that can be used to perform a number of tasks in the background. This leaves the primary thread free to perform other tasks asynchronously.
• Thread pools are often employed in server applications. Each incoming request is assigned to a thread from the thread pool, so the request can be processed asynchronously, without tying up the primary thread or delaying the processing of subsequent requests.
Sockets
• A Socket is a bidirectional end-point for a communication link between two programs. We use
• SynchronousSocketClient.cs• SynchronousSocketListener.cs
Sockets
• A Socket is a bidirectional end-point for a communication link between two programs. We use
• SynchronousSocketClient.cs• SynchronousSocketListener.cs
• C# Server Socket Program: A C# Server Socket Program running on a computer has a socket that bound to a Port Number on the same computer and listening to the client's incoming requests.
Remoting
• The .NET Remoting API is a framework that allows objects on a client machine to communicate with remote objects on a server.
• .NET remoting provides an abstract approach to interprocess communication that separates the remotable object from a specific client or server application domain and from a specific mechanism of communication.
Compiling from command line
• Find where csc.exe is on your machine, say• C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe
• You can add this location you the PATH system variable.
• csc.exe file.cs