COMP 3000 Operating Systems
Further into the Abstractions (part 2)
Lianying Zhao
Concurrency vs. Parallelism
• Concurrency (one of the three “pieces”)
• Multiple tasks (contexts) progressing at the same time
• In overlapping time periods, but not necessarily exactly at the same time • Posing a host of challenges (we will discuss in this course)
• Parallelism
• Literally at the same time • E.g., on a multi-core CPU
• How to understand the two?
• Concurrent→interruptible Parallel→independent
• No true parallelism actually justifies the need for task scheduling
COMP 3000 (Winter 2021) 2
Working with Processes – Creation
• The fork system call
• The exec system call
• CreateProcess() on Windows
COMP 3000 (Winter 2021) 3
Processes vs. Threads
• Various ways to understand the difference • Multiple points of execution for one program • Threads are light-weight processes
• Actually, the kernel does not necessarily see (user) threads, i.e., they are the same at the low level, so
• Context switch is also needed for scheduling threads • Thread Control Block (TCB)
• Threads’ defining difference: shared address space
COMP 3000 (Winter 2021) 4
Working with Threads
• Pthreads (POSIX threads)
• E.g., pthread_create()
• Library calls, not system calls
• At the OS level, the actual system calls to create threads can be:
• Clone – Linux specific (recall: portability)
• by specifying CLONE_THREAD (there are also other levels of sharing for processes)
• Shared address space causes more concurrency issues
COMP 3000 (Winter 2021) 5
Virtualizing the Memory
• How is memory (RAM) used/organized?
• Addressable at the granularity of bytes
• Accessed at the granularity of the width of the processor architecture • Data storage + code execution
• The main problem to solve: • Space allocation
• Shared purposes with virtualizing the CPU:
• Security, reliability, simplicity and max efficiency
COMP 3000 (Winter 2021) 6
The Memory Hierarchy
Original (reality) Abstraction (illusion)
Also in terms of access latency
CPU registers
COMP 3000 (Winter 2021)
7
CPU registers
L1 cache
L2 cache
L3 cache
Main memory
Memory
Hard disk
The Memory Layout
Original (reality) Abstraction (illusion)
COMP 3000 (Winter 2021)
8
Memory
The Address Space Abstraction
• A (seemingly) independent numbering system starting from 0 • Addresses in an address space are virtual
• Address translation happens behind the scene
• Other than the space, there are also other meta data, such as • Stack
• Heap
COMP 3000 (Winter 2021) 9
Mechanism: Segmentation
• The idea: assign a segment for each purpose • Code – CS
• Data – DS • Stack – SS
• The start of a segment serves as the base address. An address is formed by applying an offset:
• Pure segmentation was in the old days
COMP 3000 (Winter 2021) 10
Mechanism: Paging
• The need for contiguous allocation in physical memory • External fragmentation
• Let’s go for finer granularity!
Caution: overloaded term, cf. swapping
COMP 3000 (Winter 2021) 11
Mechanism: Swapping
• Don’t want to be limited by physical memory?
• The swap partition (pagefile.sys on Windows)
• The page fault
• Paging (the other meaning): moving the pages • Swapping: moving the entire process
COMP 3000 (Winter 2021) 12
Abstractions are Provided by the OS (Kernel)
• So most of the abstractions are not directly seen in kernel space • E.g., don’t try to find processes in a kernel
• BUT the same or similar mechanisms still apply • E.g., paging also happens in kernel space
COMP 3000 (Winter 2021) 13