Week 9 - User space topics and IO Flashcards
(22 cards)
What malloc and free must do
. Os assigns memory in page sized units
. Os obv assigns large region of memory for heap
. user space allocator (malloc , free/delete) manages smaller chunks insied eheap
malloc - gives you small portion of memory from heap
free marks - small portion as free so it can be used in future ( with future mallocs)
In terms of Arenas and chunks
what happens when we declare something like
int * pn = malloc (42 * sizeof(int))
We look across arenas to see if range free with size >= 168 bytes
If true we mark range as used and chop 168 bytes from range
return a pointer to start of range
Arenas and chunks read
Add a further layer of allocation (allocating small chunks) and fragmentation - inside arenas you can have small chunks that are free and some not free ( leads to external and internal frag )
free list
linked list where each node tells us size of the free list and points to the next free list
allocation in terms of free list
when deciding which suitable node to allocate ( ie the small chunk that has size >= required size)
We have 2 policies discussed in course
First fit : We just pick the first one with >= required size - likely to lead to horrible fragmentation but fast
best fit - we choose amongs the suitable ( >= required size) - This minimises fragementation but will be slower
what happens when heap is full
1 solution - brk()
When heap gets full (ACTUALLY DOESN’T NECESSARILY HAVE TO BE FULL — MAY BE THE CASE WHERE THERE’S NOT A SINGLE CONTIGUOUS CHUNK THAT SATISFIES REQUESTED SIZE) the malloc implementation will request os to allocate more memory - one way through a system call like brk() which moves the program break to extend the amount of space available in heap to satisfy the malloc request
other alternative mmap()
so with mmap
we just ask os to assign memory from an interely new memory region in virtual address space
arenas dont have to be contiguos WITH THE HEAP
malloc just extends the free list so that it points to the next available space which could be in a completly different arena / region of memory in virtual address space
difference between brk and mmap
With brk():
We move the program break upward to extend the heap.
This enlarges the single, contiguous heap region.
All allocations stay within one continuous “heap” block.
But we are limited by the contiguous virtual address space above the program break.
✅ With mmap():
We ask the OS for entirely new, independent memory regions.
These can come from anywhere in the virtual address space — they do not have to be next to the heap or program break.
This means we can have multiple, non-contiguous arenas (heap regions).
performance of malloc
NOTE MEASUREMENTS DONE FOR <PROGRAM ,INPUT > pair
so essentially thigns we can take from this slide
a lot of things that affect performance of malloc and which can be measured
how fast allocation
how fast freeing
how much external frag
how much internal frag
we can measure external frag 1 - (ratio of largest contiguous block against total free space accross arenals)
Why does malloc need to handle concurrency
so essentially we are saying free liss must handle concurrency as we may be calling malloc across threads
NOTE WE CANT SAY WE MAY BE CALLING MALLOC ACROSS PROCESSES AS THEY DONT SHARE HEAP
smash attack and why stack needs to be secure
The stack controls where we go next — if an attacker overwrites that, they hijack the program’s future.
oh so essentially stack saves unfinished previous calls ( and also where we need to return to )
A hacker can overwrite where we need to return to ie through a buffer overflow (takes us someowhere else in memory now) which instead of taking us to the chosen return address takes us to hackers chose which could be a shell or dangerous
Why do we use handles
Handles give processes a safe, abstract way to refer to OS resources without knowing or touching internal memory or hardware details.
The need for handles clearly explained
processes make requests to objects (files , directories , other threads) which are identified using opaque handles which have unique id (usually integer)
This is because:
Process cant access memrory (so we cant identify devices using memory addresses)
os abstracts hardware details and is cant use device level identifiers
some objects are pure os level constructs and hence have no physical identifiers
File descriptors
UNIX file descriptors are versatile handles used for I/O objects (files, sockets, devices), but not for identifying processes or threads
unix style of handles is through file descriptors
which we can use when making requests to :
files on disk
network
sound devices
terminal
BUT NOT OTHER PROCESSES OR THREADS
why are file descriptors logical communication cahnnels
okay so essentially everthing ive taken is:
fd is a logical communication channel:
processes communicate THROUGH THE OS with abstractions of hardware devices. The os uses fd to see which abstraction your refering to and executes the specific operation
communication can be one way ( we can only either read or only either write ) or it can be two way (both read and write)
Why cant we use File descriptors for inter process communication and hency how do ICP WORK
so this whole slide talks about IPC ( inter process communication mechanism)
file descriptor between process and (abstraction of) hardware
We need a way to communicate from process -> process and process -> thread
we use IPC mechanisms:
pipes -> one way communication one process writes to pipe and another reads
pseudo terminals -> kinda like a bidirectional pipe
problem with these is :
it slows down performance-> frequent context switches (between processes) -> larger overhead
map shared and map private
MAP_SHARED
Each process gets a free region in its virtual address space.
That region is mapped to the same physical frame(s) containing the file data.
When one process writes, the changes go to the shared physical frame →
other processes see the updates immediately.
MAP_PRIVATE
Each process again gets a free region in its virtual address space,
initially mapped to the same physical frame(s) as the file data.
BUT:
On first write → the OS creates a private copy (copy-on-write) for that process.
So:
Process A’s changes are not visible to Process B.
The original file is not modified.
After the first write, that process’s virtual pages point to separate private physical frames.
how do drivers work
The OS provides an abstraction layer for I/O devices → because there are many types (disks, keyboards, cameras, network cards) and it’s impractical for every program to handle hardware-specific details.
Instead, the OS relies on device drivers, which:
Know the exact details of how to communicate with specific hardware.
Provide a standard interface that the OS can call to interact with the device.
So:
→ Programs talk to the OS using system calls.
→ The OS talks to the driver.
→ The driver talks to the hardware.
What is polling
while we are waiting for disk to be read from data cpu wasting time checking if disk is ready (could be doing something else)
Very inefficient
what can we doing instead of polling to reduce idle time
Interrupts
👉 Instead of the CPU constantly polling the device (“are you ready yet?” → wasting CPU cycles),
we now let the CPU do useful work (run other processes) while the device works in parallel.
👉 When the device (like the disk) finishes, it raises an interrupt.
👉 The CPU pauses, runs the interrupt handler to transfer the data, and then returns to normal work.
So:
We reduce idle waiting time.
We reduce wasted CPU cycles.
But yes — there’s still a small moment when the CPU is briefly tied up to copy the data.
difference between interrupts and direct memory access
With interrupts (without DMA):
Device signals it’s ready → interrupt → CPU/OS handles interrupt →
→ CPU itself copies the data from device to memory → CPU resumes.
🔹 With Direct Memory Access (DMA):
DMA controller handles the entire data transfer between device and memory →
→ when finished, controller raises an interrupt → CPU/OS handles interrupt, but the data is already in memory → CPU resumes.