CS703 Advanced
Operating Systems
By Mr. Farhan Zaidi
Lecture No.
22
Mark and Sweep Collecting
Mark bit set
root
Before mark
After mark
After sweep
free
free
Mark and Sweep
Mark using depth-first traversal of the memory graph
ptr mark(ptr p) {
if (!is_ptr(p)) return;
if (markBitSet(p)) return
setMarkBit(p);
for (i=0; i < length(p); i++)
mark(p[i]);
return;
}
//
//
//
//
Sweep using lengths to find next block
ptr sweep(ptr p, ptr end) {
while (p < end) {
if markBitSet(p)
clearMarkBit();
else if (allocateBitSet(p))
free(p);
p += length(p);
}
}
do nothing if not pointer
check if already marked
set the mark bit
mark all children
What is an operating system?
Top-down view
Bottom-up view
Time multiplexing
Space multiplexing
Type of Operating Systems
Main frame operating systems
Time-sharing systems
Multiprocessor operating systems
PC operating systems
Real-time operating systems
Embedded operating systems
OS Structure
Monolithic Design
Layering
Micro-kernel
ELF Object File Format
Elf header
Program header table
.text section
.data section
.bss section
Strong and Weak Symbols
Program symbols are either strong or weak
Linker’s Symbol Rules
Rule 1. A strong symbol can only appear once.
Rule 2. A weak symbol can be overridden by a strong
symbol of the same name.
Rule 3. If there are multiple weak symbols, the linker can
pick an arbitrary one.
Creating Static Libraries
atoi.c
printf.c
Translator
Translator
atoi.o
printf.o
random.c
...
random.o
Archiver (ar)
libc.a
Translator
ar rs libc.a \
atoi.o printf.o … random.o
C standard library
The Complete Picture
m.c
a.c
Translator
Translator
m.o
a.o
libwhatever.a
Static Linker (ld)
p
libc.so
Loader/Dynamic Linker
(ld-linux.so)
p’
libm.so
Process
A unit of activity characterized by the execution of a
sequence of instructions, a current state, and an
associated set of system instructions
Private Address Spaces
0xffffffff
kernel virtual memory
(code, data, heap, stack)
0xc0000000
0x40000000
user stack
(created at runtime)
read/write segment
(.data, .bss)
0
%esp (stack pointer)
memory mapped region for
shared libraries
run-time heap
(managed by malloc)
0x08048000
memory
invisible to
user code
read-only segment
(.init, .text, .rodata)
unused
brk
loaded from the
executable file
Asynchronous Exceptions (Interrupts)
Caused by events external to the processor
Synchronous Exceptions
Traps
Faults
Aborts
Process Creation
Assign a unique process identifier
Allocate space for the process
Initialize process control block
Unix SVR4 Processes
Process Control Block
Process state: e.g., running, ready, waiting, halted
Priority
Scheduling-related information
Event
fork: Creating new processes
int fork(void)
exit: Destroying Process
void exit(int status)
Zombies
Idea
Reaping
exec: Running new programs
int execl(char *path, char *arg0, char *arg1, …,0)
Safe and Unsafe Trajectories
Thread 2
T2
Safe trajectory
S2
critical
section
wrt cnt
Unsafe
trajectory
Unsafe region
U2
L2
H2
H1
L1
U1
S1
critical section wrt cnt
T1
Thread 1
Semaphores
semaphore = a synchronization primitive
– higher level than locks
– invented by Dijkstra in 1968, as part of the THE OS