Course Syllabus 1. Introduction - History; Views; Concepts; Structure 2. Process Management - Processes; State + Resources; Threads; Unix implementation of Processes 3. Scheduling – Paradigms; Unix; Modeling 4. Synchronization - Synchronization primitives and their equivalence; Deadlocks 5. Memory Management - Virtual memory; Page replacement algorithms; Segmentation 6. File Systems - Implementation; Directory and space management; Unix file system; Distributed file systems (NFS) 7. Virtualization – Virtual machines, type I and II hypervisors, classic virtualization, sensitive and privileged instructions, binary translation, memory virtualization 7. Distributed Synchronization (if there's time) Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 1 Multiprogramming Time multiplexing means utilizing a resource at the same time by many users. A resource is shared between users for specific intervals of time. Multiprogramming 4 processes. Process switch → context switch. Conceptually, each process has its own virtual CPU and virtual program counter. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky only one process active (uses CPU) at once 2 Processes and programs ❑ ❑ Interrupt analogy Baking analogy: o o o o Recipe = Program Baker = Processor Ingredients = data Baking the pie = Process asynchronous event that needs immediate treatment process – program in execution program #include <stdio.h> int main() { printf("hello\n"); return 0; } o Baker's child rushes in… run program OS object which is: • resource container - memory address space content, open files, received signals, …. • thread(s) of execution - registers, … Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 3 main OS process-related goals ❑ Manage process entity - create, delete, suspend, block, … Allocate recourses to process ❑ Interleave the execution of existing processes to maximize processor utilization ❑ Provide reasonable response times ❑ Support inter-process communication and synchronization ❑ process table (pcb – process control block, pte – process table entry) data structure is needed safe and fair policy is needed scheduler is needed - maximal utilization of CPU time (also try to maximize other resources utilization) process priority is needed there is a user waiting for (fast) response Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 4 When is a new process created? like init process 1. System initialization (Daemons) 2. Execution of a process creation system call by a running process fork() run program 3. A user request to create a process on Shell When does a process terminate? 1. 2. 3. 4. Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary) Killed by another process (involuntary) Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 5 Processes: outline ❑Basic concepts ❑Process states and structures ❑Process management ❑signals ❑Threads ❑Specific implementations Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 6 Process Basic States also called “Runnable” • new process • process that is temporary stopped, to let another process to run for N CPUs there may be up to N running processes in parallel actually using CPU unable to run until some external event happens: execute IO, wait on critical section, ... A process can block itself, but cannot run itself Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 7 When do these transitions occur? • end of time quantum • process executes yield() • preemption – some process p becomes Ready, and OS scheduler decides to stop current process to run p scheduler switches back to process input becomes available, or event arrives Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky process blocks for IO or waits for some event 8 Five-State Process Model New admit Running Ready release Exit process is terminated but still appears in process table process is created, but still not in main memory (i.e. created on Disk) Blocked Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 9 Scheduling: Single Queue process is selected by OS scheduler to run Ready Queue release dispatch admit CPU time-out (time quantum is finished) event occurs Instead of keeping process state, we may put the process into appropriate queue event wait Blocked Queue Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 10 Scheduling: Multiple Queues admit release CPU time-out event 1 occurs event 1 wait event 1 Blocked Queue event 2 occurs event 2 wait event 2 Blocked Queue Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 11 Suspended Processes Disk ❑ CPU is much faster than IO, so many processes could be waiting for IO ❑ RAM Process 1 Swap some of these processes to disk may free up memory Process 2 Process 3 Process 4 ❑ Blocked state for swapped process is called Blocked-Suspended, Ready state is called Ready-Suspended Is swapping expensive ? swap-out when wait for IO Process 3 swap-in after IO is ready NO. Most of process image is already on Disk, so we need to write to Disk only the modified data. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 12 Process State Diagram with Suspend States New admit Ready, suspend suspend activate Ready Running Exit suspend event occurs Blocked, suspend activate Blocked suspend Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 13 Terminated processes ❑ Zombie – process is terminated but still holds PCB ▪ Zombie PCB is deleted by kernel when its parent executes wait() ❑ Orphan – process which parent is terminated, without waiting for it ❑ init process becomes parent of Orphan processes ▪ init executes wait() so that Zombie Orphan processes may be deleted from Process Table Zombie process parent may still run, so kernel should keep this process till parent would wait() for it, or till this process becomes Orphan. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 14 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) schedulingrelated information Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 16 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) inter-process communication Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 17 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) memory management Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 18 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) resources ownership and utilization Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 19 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) process relationships Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 20 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) general registers Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 21 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) program counter Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 22 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) program status word: status, mode (user/kernel), interrupts (enabled/disabled) Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 23 Process Table Main fields of Process Table Entry (PTE) / Process Control Block (PCB) stack pointer Each process has two stacks – user and kernel stack. Why two stacks ? Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Answer: kernel data is not deleted from stack, so user process might read this data – not secure. 24 Process Creation Assign a unique process identifier (PID) ❑ Initialize Process Control Block (PCB) ❑ Set up appropriate linkage to scheduling Queue ❑ new process PCB Assume that some process P1 is just terminated. May we give its PID to the new process we created ? NO. Maybe some other process P2 still keeps this PID, and might use it, for example for communication with P1. Thus communication would be with the new process, which is wrong behaviour. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 25 Processes: outline ❑Basic concepts ❑Process states and structures ❑Process management ❑signals ❑Threads ❑Specific implementations Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 26 Process Context Switch ❑ ❑ Save CPU context - registers Update stopped process control block new state and scheduling info Move process control block to appropriate queue - Ready, Blocked,… ❑ Select next running process - run Scheduler ❑ Update selected process control block ❑ Restore CPU context of selected process ❑ What is one of the most important Scheduler property for efficient context switch ? performance Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 27 Managing Processes (Unix) ❑ PID = fork() - create a child process ❑ wait(status) / waitpid(pid, status, opts) - wait for termination of a (specific) child process; status argument is a pointer to the variable that would hold child process termination status ❑ execvp(name, args) – replace image by name, with arguments args ❑ exit(status) Why fork() and execvp() are not a single function ? Answer: after fork() and before execvp() child process may change its running environment: stdin, stdout, working directory,... Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 28 Process Creation in Unix – fork() ▪ ▪ ▪ ▪ ▪ ▪ check if process table is full (if yes, reject process creation) allocate memory for child’s data and stack “copy” parent’s code, data and stack to child memory find a free PTE and copy parent’s PTE to it parent receives PID of child (return value of fork()) child receives 0 (return value of fork()) pid = fork(); if (pid < 0) {…} else if (pid > 0) {…} else {…} Seems like fork() should be expensive, but indeed it works very fast. Why ? /* upon success of fork() pid > 0 in parent */ /* fork failed - memory full, process table full, … */ /* parent code goes here ... */ /* child code goes here ... */ Answer: address space is Copied-On-Write (COW), means that we ineeded do not copy parent address space. Use getpid() system call to get process pid. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 29 Process Creation in Unix – fork() ▪ ▪ ▪ ▪ ▪ ▪ check if process table is full (if yes, reject process creation) allocate memory for child’s data and stack “copy” parent’s code, data and stack to child memory find a free PTE and copy parent’s PTE to it parent receives PID of child (return value of fork()) child receives 0 (return value of fork()) pid = fork(); if (pid < 0) {…} else if (pid > 0) {…} else {…} /* upon success of fork() pid > 0 in parent */ /* fork failed - memory full, process table full, … */ /* parent code goes here ... */ /* child code goes here ... */ Use getpid() system call to get process pid. In Windows OS, fork()+exec() is a single system call. What are the advantages and disadvantages of this ? Answer: between fork() and exec() we can configure child process. For example, we can redirect its stdin / stdout to be piped with parent process. But we pay for two system calls, which means double context switch. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 30 Process Creation in Unix – fork() ▪ ▪ ▪ ▪ ▪ ▪ check if process table is full (if yes, reject process creation) allocate memory for child’s data and stack “copy” parent’s code, data and stack to child memory find a free PTE and copy parent’s PTE to it parent receives PID of child (return value of fork()) child receives 0 (return value of fork()) pid = fork(); if (pid < 0) {…} else if (pid > 0) {…} else {…} /* upon success of fork() pid > 0 in parent */ /* fork failed - memory full, process table full, … */ /* parent code goes here ... */ /* child code goes here ... */ Use getpid() system call to get process pid. After fork, should OS Scheduler prefer to run child process ? Why ? Answer: parent address space is Copied-On-Write (COW). If run parent process first, all the memory changed by parent would be copied for child address space. This might be overhead, since child process probably would exec() and would not use these copied data. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 31 Process Creation in Unix – fork() ▪ ▪ ▪ ▪ ▪ ▪ check if process table is full (if yes, reject process creation) allocate memory for child’s data and stack “copy” parent’s code, data and stack to child memory find a free PTE and copy parent’s PTE to it parent receives PID of child (return value of fork()) child receives 0 (return value of fork()) pid = fork(); if (pid < 0) {…} else if (pid > 0) {…} else {…} /* upon success of fork() pid > 0 in parent */ /* fork failed - memory full, process table full, … */ /* parent code goes here ... */ /* child code goes here ... */ Note: all opened files of parent process stay opened also in child process. If we want to close them before running exec(), we can do this by system call fcntl() with FD_CLOEXEC flag on. Use getpid() system call to get process pid. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 32 Example: fork() and exec() in details, when run “ls” from Shell User mode Kernel mode Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 33 Processes: outline ❑Basic concepts ❑Process states and structures ❑Process management ❑Signals ❑Threads ❑Specific implementations Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 34 Unix signals ❑signal is a software interrupt ❑signals are generated: o from keyboard: Ctrl-C, Ctrl-Z, … o from command line: kill -<SIG> <PID> o by a system call: kill(PID, SIG) ❑process can send a signal to all processes within its process group Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 35 Handling signals ❑ Upon receiving a signal the process can: o ignore it (not always) o let OS take default action • • • • function name= run this function SIG_IGN = ignore signal SIG_DFL = take OS default action SIG = signal id o catch it by a process signal handler ❑ Defining action to take on signals done by calling: signal(signum, [function name | SIG_IGN | SIG_DFL ]); int main() { for (;;) printf(“hello world\n”); return 0; } void handleSIG(int SIG) { printf(“caught signal %d\n", SIG); } int main() { signal(SIG, handleSIG); for (;;); printf(“hello world\n”); return 0; } Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 36 Handling signals ❑ Upon receiving a signal the process can: o ignore it (not always) o let OS take default action • • • • function name= run this function SIG_IGN = ignore signal SIG_DFL = take OS default action SIG = signal id o catch it by a process signal handler ❑ Defining action to take on signals done by calling: signal(signum, [function name | SIG_IGN | SIG_DFL ]); int main() { for (;;) printf(“hello world\n”); return 0; } hello world hello world ... (Ctrl+C) signal() provides one-time mapping, so that at the next time SIG would be treated by default function void handleSIG(int SIG) { printf(“caught signal %d\n", SIG); hello world } int main() { signal(SIG, handleSIG); for (;;) printf(“hello world\n”); return 0; } hello world ... (Ctrl+C) caught signal 2 hello world hello world ... (Ctrl+C) Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 37 Signals: simple example int main(void) { signal(SIGUSR1, sig_usr); signal(SIGUSR2, sig_usr); while (true) { printf(“waiting for signal…\n"); pause(); } pause() blocks process till some signal received void sig_usr(int SIG) { switch (SIG) { SIGUSR1 and SIGUSR2 case SIGUSR1: are signals for interwrite("received SIGUSR1"); process communication. break; case SIGUSR2: The default action is to write("received SIGUSR2"); terminate the process. break; } } Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 38 Signals: simple example int main(void) { signal(SIGUSR1, sig_usr); signal(SIGUSR2, sig_usr); while (true) { printf(“waiting for signal…\n"); pause(); } void sig_usr(int SIG) { switch (SIG) { case SIGUSR1: write("received SIGUSR1"); break; case SIGUSR2: write("received SIGUSR2"); break; } } When write signal handler, we should write its code carefully. For example, the following code is wrong for sig_usr. Why ? void sig_usr(int SIG) { switch (SIG) { case SIGUSR1: printf("received SIGUSR1"); break; case SIGUSR2: printf("received SIGUSR2"); break; } } Using “printf()” C library function is the problem. This function is not reentrant, means, we cannot call printf() during execution of printf(). This function uses global variables, which would be overwritten by the second call, which leads to wrong execution of the first call when go39 back to it. Signals: simple example int main(void) { signal(SIGUSR1, sig_usr); signal(SIGUSR2, sig_usr); while (true) { printf(“waiting for signal…\n"); pause(); } When fork(), pending signals would not be copied to child, but signal handlers would. If child run exec(), may child still keep these signal handlers ? void sig_usr(int SIG) { NO. In our example, we define special switch (SIG) { function sig_usr() to treat SIGUSR1 and case SIGUSR1: SIGUSR2. If child process switches its write("received SIGUSR1"); code to another executable, there might break; not be sig_usr() function there. So, after case SIGUSR2: exec() all signal handlers are discarded. write("received SIGUSR2"); Blocked signals stay blocked. break; } } Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 40 More on Unix signals ❑ Kernel sets signal bit in PCB upon receiving signal ❑ Some predefined signal numbers: o o o o o o process PCB creates a file called core SIGABRT - abort process (core dump) containing process image, for debugging SIGALRM - alarm clock (alarm, sleep, pause) SIGSEGV - segmentation violation (invalid address, kill process) SIGKILL – kill process (user defined action cannot be taken on this signal) SIGILL - illegal instruction SIGCHILD – is sent to parent when child process terminates. If parent executes wait(), it gets the exit code of child. > kill -l 41 More on Unix signals ❑ Kernel sets signal bit in PCB upon receiving signal ❑ Some predefined signal numbers: OS sends this signal, o o o o o o process PCB when a process asked SIGABRT - abort process (core dump) OS to sent it a signa after some time period SIGALRM - alarm clock (alarm, sleep, pause) SIGSEGV - segmentation violation (invalid address, kill process) SIGKILL – kill process (user defined action cannot be taken on this signal) SIGILL - illegal instruction SIGCHILD – is sent to parent when child process terminates. If parent executes wait(), it gets the exit code of child. > kill -l 42 Unix signals: terminology & semantics Signal is generated for a process when the event that causes it occurs. This causes the setting of a bit in the PCB. ❑ Signals are not immediately treated process PCB ❑ generated but yet not treated signal is pending ❑ signal bit is reset (turned off) when the action for that signal is taken ❑ If a signal is generated multiple times till process gets it, the process gets it only once since there is only bit per signal ❑ A process may block some signals If signal receives but is blocked by process, its action would not take place, but its bit would be set to ‘1’. If at some point of time process decides to unblock this signal, process would “see” this signal and would treat it. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 43 System Calls for Process Management s – error code pid – process ID residual – remaining time from the previous alarm Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 44 Inter-Process Communication ❑ Signals ❑ Shared memory – the fastest way o Need to avoid race conditions ❑ Non-shared Memory: o o o o o o File/Pipe Unbuffered messages - Rendezvous Buffered messages – Mailboxes and Sockets Sockets: Address – Domain + Port Sockets: Types – Stream or Datagrams Sockets: API: Socket, Bind, Connect, Read/Write Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 45 Processes: outline ❑Basic concepts ❑Process states and structures ❑Process management ❑Signals ❑Threads ❑Specific implementations Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 46 Threads Benefits ? ❑ Perform different tasks concurrently within same process environment ❑ thread is a basic unit for OS Scheduler Communicating without involving kernel system calls. P2 P1 Process - execution environment Thread - code executor, local execution container environment container Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 47 Threads Benefits ? ❑ Perform different tasks concurrently within same process environment ❑ thread is a basic unit for OS Scheduler Takes less time to create / terminate a new thread than a process. P2 P1 Process - execution environment Thread - code executor, local execution container environment container Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 48 Threads Benefits ? ❑ Perform different tasks concurrently within same process environment ❑ thread is a basic unit for OS Scheduler Takes less time to switch between threads within the same process. P2 P1 Process - execution environment Thread - code executor, local execution container environment container Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 49 Threads Benefits ? ❑ Perform different tasks concurrently within same process environment ❑ thread is a basic unit for OS Scheduler CPU cache content is not discarded since threads share same address space. P2 P1 Process - execution environment Thread - code executor, local execution container environment container Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 50 Creation time: process vs. thread threads are sometimes called “light-weight processes” Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 51 Suspend and terminate process ❑ Suspending a process suspends all process threads (since all threads share same PTE) ❑ Termination of a process terminates all threads within the process Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 52 Thread Model code registers data opened files user/kernel stacks process control block process control block code data opened files registers registers registers thread control blocks user/ kernel stacks user/ kernel stacks user/ kernel stacks thread thread thread thread Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 53 Implementation issues of threads process P2 process P1 t2 runs fork() t1 t2 t3 ? Should we keep all the threads in child process ? Answer: theoretical it is possible, but according to POSIX only thread calling fork() is duplicated. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Ben-Gurion University 54 Implementation issues of threads process P2 process P1 t2 runs fork() t1 t2 t3 ? After exec() we cannot keep threads, even theoretically. Why ? Answer: we change the code, and each thread should execute its code. The code threads should run might not be in the new process… Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Ben-Gurion University 55 Implementation issues of threads process P2 process P1 t2 runs fork() t1 t2 t3 ? P1 has 2 threads, P2 has 100 threads. Should P2 get more CPU time ? NO. OS should take this in account and make a balance between CPU time for all the processes. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Ben-Gurion University 56 Kernel-level threads vs. User-level threads P1 User threads are typically implemented by user-level cooperative scheduling P2 user mode user mode kernel mode kernel mode Process table Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky P1 Thread tables 57 P2 Process table Kernel-level threads vs. User-level threads P1 Kernel is not aware of the existence of threads. All thread management is done by user. P2 user mode user mode kernel mode kernel mode Process table Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky P1 Thread tables 58 P2 Process table Kernel-level threads vs. User-level threads Benefits ? P1 P2 YES. Scheduling is application specific, and thus may be more efficient. user mode user mode kernel mode kernel mode Process table Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky P1 Thread tables 59 P2 Process table Kernel-level threads vs. User-level threads Benefits ? P1 P2 YES. Thread context P1 P2 switch happens in user mode - better performance. Why ? user mode user mode Answer: No system call is needed ! kernel mode Process table kernel mode Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Thread tables 60 Process table Kernel-level threads vs. User-level threads Problems ? P1 P2 user mode kernel mode YES. System call by one thread blocks the entire process. To avoid this problem, we may nonblocking system calls when user mode possible. But this is not always possible. P1 P2 kernel mode Is this always possible ? Process table Thread table NO. For example, when one thread accesses page which is not in RAM and gets ‘page fault’, this thread is BLOCKED automatically. Thread tables 61 Process table Kernel-level threads vs. User-level threads Problems ? P1 P1 P2 YES. Only single CPU can be used. user mode user mode kernel mode kernel mode Process table P2 Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Thread tables 62 Process table Kernel-level threads vs. User-level threads Problems ? P1 P2 YES. All threads share single time quantum given by OS to the process, by using yield() system call. user mode user mode kernel mode kernel mode Process table Thread table Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky P1 Thread tables 63 P2 Process table Intel 8-core CPU CPU consists of many hardware components. One of them is CPU processor. We call it CPU Core. Ben-Gurion University How CPU hardware structure increases concurrency ? Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky Simultaneous Multi-Threading (SMT) Hardware multi-threading (hyper-threading) is the ability of CPU core to execute multiple threads concurrently. This converts single CPU core into several virtual CPU cores, each one with its own hardware register file. Logical CPU hardware thread single-core CPU with 2 logical cores extremely fast contextswitch - just sets register file index from 0 to 1, and vice versa Example: Sun's UltraSPARC T2 chip contains 8 cores, each comprising 8 HW cores for a total of 64 concurrent hardware threads. Indeed threads run on Logical CPU cores. In our example with 64 hardware threads, 64 threads may be run concurrently. 65 Simultaneous Multi-Threading (SMT) Hardware multi-threading (hyper-threading) is the ability of CPU core to execute multiple threads concurrently. This converts single CPU core into several virtual CPU cores, each one with its own hardware register file. Logical CPU hardware thread If some thread needs to bring data from RAM, its processor would be idle till the data returns from RAM. In this case other thread may meanwhile use this processor. Also, if a thread cannot use all the computing resources of CPU, running another thread may prevent those resources from becoming idle. single-core CPU with 2 logical cores 66 Execution on single-core pipelining permits Instruction-Level Parallelism (ILP) process instructions Tip for high-performed programming: when use ifelse, write in “if” condition the one with higher probability to happen. each (assembly) instruction is executed in five steps: fetch-decode-read-execute-write Note that when coed is non-sequential (like if-else instruction block), CPU makes brunch prediction to prefetch next instruction. CPU’s guess might be wrong, and then another (right) instruction should be fetched, wasting CPU time. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 67 Threads in POSIX The principal POSIX thread calls. main() contains implicit call to system call exit(). When exit() is executed, the whole process is killed. When thread calls pthread_exit(), this function is non-returnable, means that main thread would not return to main() any more. This is why exit() system call would not be executed in this case, and the process (together with all rest of its threads) would continue. Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 68 Thread / process creation – resources sharing options (Linux) binary array that specifies which parts of process to share code to run PID = clone(function, stackPtr, sharingFlags, arg); “...creates a new process, in a manner similar to fork()...” Bits in the sharingFlags bitmap Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 69 Processes: outline ❑Basic concepts ❑Process states and structures ❑Process management ❑Signals ❑Threads ❑Specific implementations Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 70 Solaris 2-8: Combined Approach for Threads Multiple user-level threads may be mapped onto some (smaller or equal) number of kernel-level threads via a LWP (light-weight process) object. API is provided to perform this mapping. Java (JVM) works like this, implicitly user-level threads library scheduler many-to-many model kernel daemon thread processor affinity – map kernel-level thread to specific CPU Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 71 Windows OS – Processes and Threads Windows OS user-level threads Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 72 Windows OS: Process, Thread & Fiber - API Calls fork + exec together Operating Systems, Spring 2020, I. Dinur , D. Hendler and M. Kogan-Sadetsky 73