Introduction

Concurrency vs Parallelism (again):

Concurrency

Parallel

Multiple processes vs Multiple Threads:

Processes:

Threads:

Exceptions and Interrupts:

Exceptions

Interrupts

User level thread vs Kernel thread

User-level

Kernel thread

Issues in concurrency

Race conditions

Race condition: Different ordering of concurrent events may leading to different (one of them incorrect) program execution.

Data race:

  1. Two threads access shared resource at the same time without synchronization
  2. At least one modifies the shared resource

Solution: synchronization (especially on shared buffer/queues, lists, hash table)

Mutual Exclusion and Correctness

Code sequence protected with mutex is Critical Section.

Crit. Section Requirements:

  1. Mutual Exclusion
  2. Progress (if T not in c.s., then T cannot stop S from entering c.s.)
  3. No starvation/Bounded wait (if S wants to enter, it eventually can)

Correct Concurrent Program Requirements:

  1. Safety: ‘nothing bad happens’
    1. end with invalid state
    2. data race
    3. no deadlock/livelock
  2. Liveness: ‘something good happens’
    1. the program must terminate in a valid state
    2. no starvation
    3. fair access in case of contention

Mechanism

Deadlock

Waiting program is holding onto a resource that is required for the current program to finish.

Conditions:

  1. Mutual exclusion
  2. Hold and wait
  3. No preemption
  4. Circular wait

Livelock

Similar to a deadlock, except

Starvation

A process is prevented from making progress due to another process holding the resource required.

Types of Parallelism

Task vs Data parallelism

Executing a concurrent program

  1. Compile
  2. Link
  3. Load
  4. Execute

Types of code formats

  1. Machine Code: Binary executable
  2. Object Code: Parts of machine code that requires linker to join
  3. Assembly code: Text format, human readable with 1:1 analog with machine insns.
  4. High level language, must be converted to assembly

C/C++ Compilation

  1. Preprocessing (C/C++ into intermediate HLL)
    1. Replacing pre-processing directives
  2. Compiler (HLL processed source to assembly)
  3. Assembler (assembly code into .obj)
  4. Linker (final output from the .obj files of the assembler)