Module 1: Threading Flashcards
Concurrency
- Two or more tasks seem to execute at the same time.
- Share the computer’s CPU.
Task
Unit of a program that can be executed concurrently with other units of a program.
Parallell programming
- Two or more tasks run exactly at the same time.
- Don’t share the same CPU.
Concurrency in an application is achieved as a collaboration between three components…
Underlying hardware: must support multithreading/processing.
Operating system: Must also support multithreading.
Application: Designed & contain algorithms to make best use of underlying OS & hardware.
Multiprocessing
Using multiple processes (applications at run-time)
Multithreading
Using multiple threads withing a process.
Application
- When an application starts running = becomes process.
Scheduler
- Program run by OS or runtime. Decides which process is allowed to run within next period of time.
- Preemptive: Priority-based. Gives every task exclusive access to computing resource for given time period, then switches to other task.
- Cooperative: Every task exclusive access to computing resource until task finishes or task yields access to the resource.
Interrupts
- EX: Modern printers
- Signal to processor emmitted by hardware or software; indicating event needs immediate attention.
- Can occur between processes. A process requests interrupt on another.
- Also known as: Exceptions.
Multitasking
OS quickly switches between computing tasks; gives impression of diff. applications executing multiple actions simultaneously.
Multithreading
Form of multitasking w/in application. Executes several tasks concurrently (at the same time).
Process
- Created when program is started by OS.
- Receives ID and memory space.
- Instance of an application being executed.
- Instructions in machine language.
- Stack memory (keep track of methods).
- Heap memory.
- Program counter (register).
- Other (eg. security info, state of process, etc).
Process and memory isolation
- Runs own address space managed by OS, totally isolated from other processes.
- Don’t share / access each other’s memory space.
- May or may not communicate w/ each other.
- Exchanging messages, IPC (managed by OS).
Threads & Processes
- Unit of execution (just like process).
- Runs w/in address space of a process. Executes of the whole or parts of a program.
- Shares process’ memory area.
- Every thread performs a task.
- Task = unit of work that is to be scheduled.
Thread
- Single sequential flow of control w/in a program.
- Light-weight process.
- Sequence of instructions, unit of work, executed independently by a program.
- Threads are parallel to each other. Each do its own job.
- Done = deleted.
- Own execution context. Own set of registers, stack but not own memory space.
- All threads w/in same process share: memory, program code & global data.
- Each process: at least one main thread. Can have many (multithreaded).
Single-threading
Single path of execution; Application run by a single thread. Runs sequentially.
Sequential application
- Each process is a sequential application
= sequence of statements executed one after another. - One single thread of execution.
Concurrent application
- Multiple threads of execution.
- Communication between processes = programmed using shared variables or message passing.
Parallelism
- A form of concurrency.
- Perform multiple tasks simultaneously.
- Achieved in an application by splitting it into smaller tasks, executed in parallel.
- Needs: 2 or more processing units, cores or CPUS.
- 2 or more tasks are executed simultaneously (in concurrent: tasks making progress at the same time).
- Single-core CPU: concurrency but not parallelism.
- Speeds up execution of eg. intense data processing.