Exam Material Question and answers
rated A+
Multiprocessing - correct answer ✔TODO: define
Advantages over multithreading:
* Memory isolation for protection and reliability
Examples where used:
* Web browsers
Multithreading - correct answer ✔TODO: define
Threads in a process share the same address space:
* Code
* Data
* Heap
* Open files
Unique for each thread:
* Program counter (PC)
* Register set
* Stack
Reasons to use:
,* Resource sharing: Sharing memory
* Responsiveness: fast thread creating
* Parallelism: using multiple CPU( core)s to make some work get completed
faster
* Avoid blocking program process because of slow I/O
Advantages over multiprocessing:
* Fast in creating threads
* Saving memory space by sharing among different threads from the same
process if possible
Examples where used:
* Web servers
Thread - correct answer ✔An abstraction of a code sequence and (its
execution) state within one process
Types:
* Pthread: standardized thread programming interface seen in Unix and Linux
Thread control block (TCB) - correct answer ✔A data structure containing
information that an OS uses to keep track of and manage threads, like their
state
Contains:
* Program counter (PC)
* Thread state (running, ...)
* Register values
, * Stack pointer
* A pointer to the process control block (PCB)
* ...
Critical section - correct answer ✔A segment of code where multiple threads
may be changing shared resources
Multiple threads executing the same [this] can result in a race condition
Race condition - correct answer ✔An undesirable bug where it is non-
deterministic which of multiple threads (or processes) will access (and
especially modify) a resource first
Can result from: Multiple threads executing the same critical section
Why counter++ (i.e. incrementing a variable) can result in [this]: counter++
compiles to 3 assembly instructions rather than just 1; this means it's no
longer an atomic operation and execution could switch off between any
instruction, rather than all 3 having to finish first
1. mov [some address], %eax // load the value of counter from memory into a
register
2. add $0x1, %eax // increment the register
3. mov %eax, [that same address] // store the contents of the register back
into memory (where it came from)
Possible solutions: Make the critical operations atomic
* Guarding critical sections with locks (mutual exclusion)
* Disabling interrupts for critical sections
* TestAndSet