


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Operating Systems ... - Manages resources and coordination (process synchronization, resource sharing). - Simplify programming (abstraction of hardware,.
Typology: Exercises
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Operating Systems ...
Kernel mode: complete access to all hardware resources User mode: limited access to hardware resources
Monolithic OS: Kernel is one big special program Microkernel OS: Kernel is very small and clean, and only provide basic and essential facilities (e.g. IPC, address space management, thread management); higher-level services (device driver, process management, memory management, file system) built on top of basic facilities and runs outside the OS (using IPC to communicate). Kernel is more robust, and there is more isolation and protection between kernel and higher-level services, but at lower performance.
Type 1 Hypervisor: Runs directly on hardware Type 2 Hypervisor: Runs on a host operating system
Generic 5-State Process Model:
Ready Running
[ CS2106 L2 - AY1819S1 ] 48
admit switch: scheduled exit
event occurs event wait
create switch: release CPU
Notes: generic process states, details vary in actual OS
New
Blocked
Terminated
Process Control Block (PCB): information about a process: registers, memory region info, PID, process state
Syscall mechanism:
Exception: Synchronous (occurs due to program execution, e.g. arithmetic errors, memory access errors). Executes an exception handler, like a forced syscall.
Interrupt: Asynchronous (occurs independent of program execution, e.g. timer, mouse movement, keypress). Executes an interrupt handler, program execution is suspended.
pid t fork(void): Parent returns PID of child, child returns zero
int execl(const char *path, const char *arg, ...) int execv(const char *path, char *const argv[]) e.g. execl("/bin/ls", "ls", "-la", NULL)
init process is the root process (traditionally PID=1)
int wait(int *status) Set status to NULL to ignore status Least significant 8 bits is the value passed to exit(int)
Non-preemptive (Cooperative): Process stays scheduled until it blocks or yields Preemptive: At the end of time quota, the process is suspended (it is still possible to block or yield early)
No user interaction, non-preemptive scheduling is predominant
Turnaround time: Total time taken from arrival to finish (including waiting time) Throughput: Number of tasks finished per unit time CPU utilization: Percentage of time CPU is doing work
First-come first-served: Use FIFO queue based on arrival time (when tasked is blocked it is removed; it is placed at the back of queue when it is ready again). Guaranteed to have no starvation Shortest Job First: Select task with smallest CPU time (until next I/O). Starvation is possible because long job may never get a chance (when short jobs keep arriving). Prediction of CPU time usually uses exponential average of history Shortest Remaining Time: Preemptive version of SJF
Convoy effect: Many tasks contend for CPU (while I/O is idle), and then contend for I/O (while CPU is idle)
Preemptive scheduling algorithms are used to ensure good response time
Response time: Time between request and response by system Predictability: Less variation in response time Time quantum: Execution duration given to a process, must be a multiple of timer interrupt
Round robin: Like First-come first-served, but will be interrupted when time quantum elapses Priority scheduling: Each task gets a priority, highest priority gets scheduled first. Preemptive variant: new higher priority process preempts currently running lower priority process
Non-preemptive variant: new higher priority process has to wait for next round of scheduling Low priority process can starve Priority inversion: higher priority task forced to block while lower priority task gets to run Multi-level feedback queue: If P riority(A) > P riority(B) then A runs If P riority(A) == P riority(B) then round-robin New job gets highest priority If a job fully utilized its time slice then priority reduced If a job yields/blocks then priority retained Lottery scheduling: Lottery tickets assigned to processes (possibly unevenly depending on priority), and randomly chosen winner is allowed to run (preemptive) Parent can distribute tickets to its child processes, and each shared resource (CPU, I/O) can have its own set of tickets
“Lightweight process” Benefits: Much less additional resources needed as compared to processes No need for additional mechanism to pass information between threads Multithreaded programs can appear much more responsive Multithread programs can take advantage of multiple CPUs Problems: Parallel syscall possible - have to guarantee correctness Process behaviour - fork()/exec()/exit() when there are multiple threads User thread: Thread is implemented as a user library (just library calls); kernel is not aware of the threads
[ CS2106 L4 - AY1819S1 ] 20 int pthread_create(pthread_t thread, const pthread_attr_t attr /NULL/, void (start_routine) (void ) /function ptr/, void arg /argument for start_routine/); int pthread_exit(void *retval); int pthread_join(pthread_t thread, void **retval);
Advantages: Efficient (only the initial steps involves OS) Easy to use (shared mem region behaves like normal mem)
Disadvantages: Synchronization (of access) Implementation is usually harder
int shmget(key_t key /can be IPC_PRIVATE/, size_t size, int shmflg /* IPC_CREAT | 600/); //IPC_CREAT means memory will be created if nonexistent void shmat(int shmid, const void shmaddr /NULL/, int shmflg /0/); int shmdt(const void shmaddr); int shmctl(int shmid, int cmd /IPC_RMID/, struct shmid_ds buf /unused for IPC_RMID*/);
Messages stored in kernel memory space
Direct communication: Sender/receiver explicitly names the other party One buffer per pair of (sender, receiver) Indirect communication: Sender sends to mailbox/port Receiver receives from mailbox/port
Blocking primitives (synchronous): Send() blocks until message is received Receive() blocks until message has arrived Non-blocking primitives (asynchronous): Send() does not block Receive() returns some indication if no message is available
Advantages: Portable (can implement in distributed system or network) Easier synchronization (blocking primitives implicitly synchronize sender/receiver) Disadvantages: Inefficient (needs OS intervention) Harder to use (messages limited in size/format)
Pipes function as fixed-size circular byte buffer with implicit synchronization
int pipe(int pipefd[2]); // create new pipe pipefd[0]: file descriptor for reading pipefd[1]: file descriptor for writing
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); // returns previous signal handler, or SIG_ERR on error
Properties of correct implementation: Mutual exclusion: If there is a process in CS then all other process cannot enter CS Progress: If no process is in CS then one waiting process should be granted access Bounded wait: After a process requests to enter the critical section, there exists an upper bound of number of times other processes can enter the CS before this process Independence: Process not in CS should never block other processes Symptoms of incorrect synchronization: Deadlock: All processes blocked Livelock: Processes are not blocked, but they keep changing state to avoid deadlock and make no other progress Starvation: Some processes are blocked forever Test and Set: TestAndSet Reg, Mem
Peterson’s Algorithm:
21
Assumption: Writing to Turn is an atomic operation
Want[0] 0
Want[0] = 1; Turn = 1; while (Want[1] && Turn == 1);
Want[0] = 0; Process P
Want[1] 0
Turn 0
Want[1] = 1; Turn = 0; while (Want[0] && Turn == 0);
Want[1] = 0; Process P
[ CS2106 L6 - AY1819S1 ]
Critical Section Critical Section
Disadvantages: busy waiting, low level, not general
Properties of correct implementation: Mutual exclusion: If there is a process in CS then all other process cannot enter CS Wait()/P()/Down() and Signal()/V()/Up() Threads queue up on a semaphore (fair scheduling)
General semaphore: value can be any non-negative integer Binary semaphore: value can be only 0 or 1 (undefined behaviour to Signal() on binary semaphore which is currently 1) Producer-Consumer:
38
Initial Values: count = in = out = 0 mutex = S(1), notFull = S(K), notEmpty = S(0)
while (TRUE) { Produce Item; wait( notFull ); wait( mutex ); buffer[in] = item; in = (in+1) % K; count++; signal( mutex ); signal( notEmpty ); } Producer Process
while (TRUE) { wait( notEmpty ); wait( mutex ); item = buffer[out]; out = (out+1) % K; count--; signal( mutex ); signal( notFull ); Consume Item; } Consumer Process
[ CS2106 L6 - AY1819S1 ]
Memory regions: Text: for instructions Data: for global variables Heap: for dynamic allocations Stack: for function invocations
Transient data: variables with automatic storage duration Persistent data: globals, dynamically allocated memory
Alternatives for memory abstraction: Address relocation: translate all addresses at load time Base + Limit registers: generate instruction to add Base to all memory references at compile time, and check against Limit for validity
Memory partitioning: every process gets a contiguous memory region Fixed partitioning: physical memory is split into fixed number of partitions, each process occupies exactly one partition
Linear search based: First-Fit: take the first hole that is large enough Best-Fit: take the smallest hole that is large enough Worst-Fit: take the largest hole Merging & Compaction: When partition is freed, try merging with adjacent holes Can move occupied partitions around to consolidate holes
Buddy system:
[ CS2106 L7 - AY1819S1 ] 38
the largest block is 512 (2 9 ) Only one free block of size 512 initially
… … …
A[9] A[8]
A[1] A[0]
0
Starting address = 0 Size = 2^9
NULL pointer to indicate no free block of this size
Free Memory = 512
0
T1 Request 100 Block P allocated at 0 size = 128
… … …
A[9] A[8]
A[0]
256 A[7] (^128)
P Free Free
0 128 256
(Physical) frame (Logical) page
Address translation:
p d
Page Number Offset
(m – n) bits n bits Translation mechanism
f d Frame Number Offset
Logical Address
Physical Address
[ CS2106 L8 - AY1819S1 ] (^) 8 Fragmentation: Paging removes external fragmentation (all free frames can be used without wastage), but pages can still have internal fragmentation (logical memory required may not be a multiple of page size)
Page table: Stores physical frame for each logical page Translation look-aside buffer (TLB): cache of a few table entries Memory access time with TLB: = T LBhit + T LBmiss = 40% × (1ns + 50ns) + 60% × (1ns + 50ns + 50ns)
[ CS2106 L8 - AY1819S1 ] 14
CPU P^ D
Physical Memory
F D
Page# Frame #
TLB
Frame #
Page Table
P
Context switching & TLB: On context switch:
(x86) On a TLB miss, the hardware searches through the page table (without invoking the OS); OS is informed only on page fault
Extensions for protection: Access-right (RWX) bits: memory access is checked against access right bits (by hardware) Valid bit: represent invalid logical addresses, invalid access will be caught by OS
Page sharing: Several processes use same physical frame
0 1 2 3 segment table
Base Limit
Physical Memory
Assume: User Code Segment = 0 Global Data Segment = 1 Heap Segment = 2 Stack Segment = 3
0 1 2 3
segment table
Base Limit
Memory Access < Segment Id, Offset > (^) User Code Segment
Stack Segment
Heap Segment
Global Data Segment
0
1300
2400
3500
5700
6000
7500
[ CS2106 L8 - AY1819S1 ] 33
CPU S P D
Page limit
Pg Table Base
Frame < Number
F D
Yes No Addressing Error!
S
P
Physical Memory
Segment Table
Page Table
Some pages can be stored on secondary storage, so that a process can use more logical memory than what is physically available
Page table stores memory resident bit:
Direct paging: All pages in single table, might occupy several memory pages 2-level paging: Keep a page directory, [[TODO]] Inverted page table: Single table for all processes, stores (pid, logical page) indexed by frame number
Optimum (OPT): Replace the page that will not be used again for the longest period of time, not feasible as it needs future knowledge First In First Out (FIFO): Evict the oldest page first
LayoutExt2 FS: Layout: MB partition 1 partition 2 partition 3 partition 4 R
BOOT Block Group 0 Block Group 1 Block Group 2 ...
Group Descriptors Data Blocks
Super- Block
Block Bitmap I-node Bitmap
I-node Table [ CS2106 L12 - AY1819S1 ] (^) 21 Superblock, group desc. duplicated in each block group
Block, I-node bitmap 1=occupied, 0=free
I-Node structureExt2: I-Node Structure (128 Bytes):
[ CS2106 L12 - AY1819S1 ] 24
Mode (2)
Owner Info (4) File Size (4/8) Timestamps (3 x 4)
Reference Count (2)
Data Block Pointers (15 x 4)
… Other … … Fields …
File type (regular, directory, special, etc) + File permission User Id (2 bytes)
Number of time this I-Node is referenced by directory entry
Directory entry:
Directory Entry (Illustration)
91 F 4 Hi.c 39 F 8 lab5.pdf
74 D 3 sub 0
I-Node Number
Entry Size
Entry Type
Name Length
Name (^) Use a 0 I-Node number to indicate unused entry
[ CS2106 L12 - AY1819S1 ] (^) 30 Deleting a file:
Hard link: multiple directory entries point to same I-node
Sym. link: file (not I-node) content is path of target file
Write information or actual data to separate log file before performing file operation, so it can recover from system crash