How are C strings represented in memory?
What is wrong with malloc(strlen(s)) when copying strings?
"hello" = [h][e][l][l][o][
\0]
A C string is just an array of characters with a null terminator (\0) at the end.
strlen("hello") == 5
strlen()
does not count the null terminator.
If used with malloc, there won’t be enough memory allocated for the null terminator!
Explain how a virtual address is converted into a physical address using a multi-level page table. You may use a concrete example e.g. a 64bit machine with 4KB pages.
Page size: 4 KB = 2^12 B
log2 (2^12)
= 12 bits
|-VPN1-|-VPN2-|-VPN3-|-Offset-|
level_1 = page_table_base_register[VPN1]
level_2 = level_1[VPN2]
level_3 = level_2[VPN3]
physical_address = level_3 + Offset
What is a page fault? When is it an error? When is it not an error?
Page fault – program attempts access to virtual memory that is not mapped to up-to-date physical memory.
There are three types
When it’s an Error - Invalid (The operating system usually segfaults)
When it’s not an Error - Major or Minor
What is Spatial and Temporal Locality? Swapping? Swap file? Demand Paging?
Spatial Locality - Objects in adjacent memory addresses get used (think arrays). That is why a page table is a certain size.
Temporal Locality - Objects used more recently get used. The TLB takes advantage of that
Swapping - Taking a page in memory and writing to disk.
Swap File - specific file on disk that the pages get written to. Another solution is a swap space partition.
Demand Paging - Only allocate pages as the process requests them
Explain the operating system actions required to perform a process context switch.
Step 1: Save state in process control block(pcb)
This includes:
Step 2: Flush Translation Lookaside Buffer (TLB)
BUT with ASID or PCID (intel) may not be necessary
Step 3: Choose next process (scheduler’s job)
Step 4: Load process state from control block
Step 5: Pop program counter, resume task
Explain the actions required to perform a thread context switch (to a thread in the same process)
Same as above EXCEPT
(i.e. not whole address space, just the stack)
Which scheduling algorithm results the smallest average wait time?
Round Robin (IF new processes sent to the back of the queue)
Why not any other scheduler?
FCFS: wait time ≤ sum of runtime of all processes on queue
SJF/PSJF: wait time ≤ ∞ if shorter jobs keep arriving
Priority: waittime ≤ ∞ if higher priority jobs keep arriving
What scheduling algorithm has the longest average response time?
Priority
Why not any other scheduler?
FCFS: response ≤ runtime of processes ahead of it on the queue
SJF/PSJF: ∞, but at least all jobs ahead of a process on the queue have shorter runtimes. Priority doesn’t guarantee this.
RR: response ≤ time quantum x number of processes ahead on the queue.
Define circular wait, mutual exclusion, hold and wait, and no-preemption. How are these related to deadlock?
Circular wait: There exists a cycle in the Resource Allocation graph
Mutual Exclusion: no two processes can hold the same resource at the same time
Hold and Wait: Once a resource is obtained, process holds it until finished
No pre-emption: Nothing can make a process give up a resource
What is the difference between Deadlock Prevention, Deadlock Detection and Deadlock Avoidance?
Deadlock Prevention: Eliminate a Coffman conditions. Deadlocks become impossible.
Deadlock Avoidance: Avoid by allocating resources in a safe manner. The OS implements concurrency control.
Deadlock Detection: When deadlock occurs, OS can detect and resolve it.
(i.e. use a Resource Allocation Graph to detect deadlocks. kill processes or preempt resources to resolve)
Give an example of kernel generated signal. List 2 calls that can a process can use to generate a SIGUSR1
.
Kernel generated signals:
SIGSEGV
SIGILL
SIGXCPU
Try running kill -l
to see all signals
raise(int signal)
signals yourself
kill(pid_t pid, int signal)
signal other processes
What signals can be caught and ignored? What signals cannot be caught?
You can catch anything except SIGKILL
and SIGSTOP
See man page for signal(2)
on how to set signal disposition.
Describe the services provided by TCP but not UDP. What applications use TCP? What applications use UDP?
TCP is used for web browsing, email, file transfers, etc.
UDP is used when data becomes stale quickly (e.g. audio/video streaming and DNS).
Briefly explain permission bits (including sticky and setuid bits) for files and directories.
drwxrwxr-x
d
or -
: directory or file
rwx
: What the owner of the file is allowed to do (read, write, execute)
rwx
: What users in the owner’s group are allowed to do (read, write, execute)
r-x
: What everyone else is allowed to od (read and execute, but not write)
What information is stored in an inode? What file system information is not?
uid
: user ID of the inode owner.gid
: the ID of the inode group (does not have to include the owner).mode
: a bitmask. Bottom 9 bits are read-write-execute for owner-group-others. Bits 11-10 are the type of the file.nlink
: hard link count. The number of directories that the file is linked to from (directories can’t be hard linked).atim
: access time. Time of last access or the last time a file was read(2).mtim
: last modification time. Last time the file was changed with write(2).ctim
: last change time. Last time the file’s metadata was changed.size
: size of the file in bytesdirect
: an array. direct[i]
is the ith data block’s offset (data_block_number
) from the data_root.indirect
: the offset number (data_block_number
) of a data block, which contains NUM_INDIRECT_BLOCKS
number of data_block_number
’s.