Preview Operating System Structure Monolithic Layered System Microkernel Virtual Machine Process Management Process Models Process Creation Process Termination Process State Process Implementation Operating System s two main functions Providing abstractions (interface) to user program (usually I/O services) Managing resources. If a process is running a user program (in user s mode) and need a system service such as reading data from a file, it must execute a trap instruction to transfer control from a user s process to kernel. OS check request and carries out the system call and returns control to the user 1 2 #include <unistd.h> ssize_t read(int fd, const void *buffer, size_t nbyte); Return number of byte read for ok, -1 for error The read() system call attempts to read nbyte bytes to the buffer pointed to by buffer from the file associated with the open file descriptor, fd. It returns the number of bytes actually read. 3 4 System call handler table: Save each handler s location Step 1 ~ 3: push parameters onto the stack Step 4: actual function call Step 5: patch read instruction to IR in CPU Step 6: (trap) change mode to kernel Step 7: dispatch system call service (or hander) number (read), Step 8: run system call service routine (or handler) Step 9, 10: change mode to user result might be saved in a register Step 11: clear stack by increment stack pointer 5 6 1
Operating System Structures Monolithic Layered System Microkernels Virtual machine Client- module Exokernels (Monolithic System) It is written as a collection of procedures, each of which can call any of the other ones whenever it needs to. Each procedure in the system has a welldefined interface in terms of parameters and results, and each one is free to call any other one, if the latter provides some useful computation that the former needs. It is possible to have some structure for a monolithic system 7 8 (Monolithic System) (Monolithic System) Possible structure for a monolithic system A main program invoke service functions Service functions carry out the system calls Utility functions helps service functions 9 10 (Layered System) (Microkernels) Operating system is divided into several layers and each layer works for different rule. Layer 0 process management Layer 1 memory management Layer 2 inter-process communication Layer 3 Input /Output management Layer 4 user program Layer 5 system operator process With the layered approach, the designers have a choice where to draw the kernel-user boundary. Traditionally, all the layers went in the kernel, but it is not necessary since one bug in kernel might cause entire system down. Various researchers studied the number of bugs (logical) per 1000 lines of code. About 5 million lines of codes for layered kernel likely to contains 10000 to 50000 kernel bugs. Not all of bugs are fatal. 11 12 2
applications 9/7/2018 (Microkernels) (Microkernels) The basic idea of the microkernel is to achieve high reliability by splitting the operating system up into small welldefined module. Only one of module (microkernel) run in kernel mode and the rest run as users mode. Ex) A bug in the audio driver will cause the sound to be garbled or stop, but not crash the computer. OS Application Software System call Handlers File System Device Drivers Scheduler Memory Manager.. Hardware Layered OS Run in user s mode Run in kernel s mode Application Software File System I/O Device Display Devices.... Microkernel Memory management Process management Deadlock management Hardware Microkernel OS Device Drivers OS 13 14 Virtual machine monitor(hypervisor) runs on the bare hardware and does multiprogramming, by providing several virtual machines. Each virtual machines are exact copies of the bare hardware, including kernel/user mode, I/O and everything else the real machine has. Different virtual machine can run different operating system. Example of virtualization Many companies have traditionally run their mail servers, Web servers, FTP servers and other servers on separate computer with different operating system. Several server can run on the same machine without having a crash of one server bring down the rest since each server running on different virtual machine. 15 16 Mail Virtual Machines Web VM 1 VM 2 VM n FTP Mail Web FTP Operating System A Bare Hardware Other VM 1 VM 2 VM 3 VM 4 VM n OS OS OS OS OS Hypervisor Physical Hardware OS OS OS Hypervisor Application software Host Operating System Physical Hardware Other Type 1 (bare metal hypervisor) Type 2 (Hosted hypervisor) 17 18 3
The Processes Chapter 2 Process & Thread We assume that there is only one CPU (processor) in our system. Multiprogramming several jobs are loaded in a memory, operating system simulate Pseudo parallelism (virtual). OS schedule the CPU times for processes by switches from one process to another based on the scheduling algorithm and process state (by short-term scheduler). 19 20 The Processes The Process Model CPU Registers ALU Control Unit RAM Job1 Job2 Job3 OS 21 We can consider processes as two different point of view. Real Model Multiprogramming Conceptual (virtual) Model each process has its own virtual CPU, PC, registers, stack pointer Check the following concept Virtual machine-each OS run on its own machine Virtual memory each process has its own Memory Virtual process model each process run on it s own machine 22 The Process Model The Process Model With the CPU switching back and forth among the processes, a process can hold CPU during its time term. The time term which assigned to each processes might not be uniform. The time term for a process might need be calculated based on the types of jobs I/O bounded, CPU bounded or priority (usually CPU bounded job need more CPU time then I/O bounded job). 23 24 4
Process Creation Process Creation System initialization - When an OS is booted, several processes are created. A running process create a process by system call running process request a system call to create one or more new processes web server example A system user create by executing a program in interactive system, users can start a program by typing a command or click icon Initiation of a batch job mainframe computer Process Creation UNIX system call fork create a new process In Window system call CreateProcess create a new process Once a child process is created, both parent and child have their own distinct address two process might need interprocess communication (for sharing resources) 25 26 Process Termination A process terminate due to one of the following condition Normal exit a process finish their job (voluntary) Error exit the process discovers a fatal error compiler try to compile a program, there is no such a file (voluntary). Fatal error error caused by the process a process try to modify the memory location where other process is located (involuntary) Killed by another process a deadlock is occurred. OS terminate a process at a time to resolve the deadlock (involuntary) 27 Process State 1 Block Running 4 3 Ready 1. Process blocks for input 2. Short-Term Scheduler picks a process 3. A process time out its time term 4. Input becomes available 2 28 Process Implementation To implement the process, operating system maintain each process s information in its process table (or process control block (PCB)) When a running process s state changes from running to block state or ready state, OS need to save information for the process. Shot term scheduler (CPU scheduler) select a process from the ready queue. OS need to restore information for the selected process from its process table into CPU Process Implementation Contents of a Process Table (or PCB) Process status The state may be new ready, running, blocked Program counter Address of next instructions for the process CPU registers registers vary in number and type, depending on the computer architecture. (Accumulators, index registers, stack pointers, general-purpose registers, plus any condition code information CPU scheduling information a process priority, pointers to the scheduling queues. Memory management information the base and limit registers, the page tables, the segmentation tables Accounting information the amount of CPU and real time used, time limits, account numbers, process numbers, I/O status information list of I/O devices allocated to the process, list of open files, and so on 29 30 5
In a traditional OS, each process has an address space and a single thread of control. In many modern software, multiple activities are running on at once. Each of them are doing the different job. By decomposing an application into multiple sequential threads that runs in quasi-parallel Each threads inside a process need its independent spaces (similar to process table) for running but sharing the same address space of program. Thread might be controlled by OS or runtime system. Each threads are running in the same memory space, but each threads are usually running in the different part of program. When a thread is created, system need create thread table for saving thread s runtime information. System need keep each threads information in thread table. 31 32 Between processes, when a state of a running process changes, system need interrupt for context switches. By decomposing an application into multiple sequential threads, each threads run different part of an process independently without interrupts, context switches. 33 Threads Threads are mini-processes in a process!!! multiple execution in the same process environment. It is made up with a thread ID, a program counter, a register set, and a stack. Different threads are not quite as independent as different processes since they share same address space It shares with other threads belonging to the same process its code section, data section and other operating system resources such as files and signals. 34 Multiprogramming vs. multi thread Multiple process running on a computer Process are share CPU, physical memory and I/O devices Multiple threads running on a process the threads are share resources of a process (address space, open files, and other recourses) With thread the ability for multiple threads of execution to share a set of resources so they can work together closely to perform same task. Each threads working on a program runs a different part of program Process 1 Process 2 Process 3 Process User space thread thread Kernel kernel kernel space a) A process with one thread a) A process with multiple thread 35 36 6
The CPU switches rapidly back and forth among the threads in the single CPU system (Same idea as multiprogramming) No protection between threads using same address space (share the global variables) A thread can be in any one of several states: running, blocked, ready 37 38 Since each thread usually call different functions from the same address space (each thread executing different part of program), each thread need its own stack. A multithread software start with single thread. The thread has ability to create new threads by calling a library procedure (i.e. thread_create) When a thread has finish its work, it can exit by calling a library procedure ( i.e. thread_exit) pthread functions for POSIX 39 40 41 7