Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Last Time: OS & Computer Architecture Modern OS Functionality (brief review) Architecture Basics Hardware Support for OS Features 1 Last Time: Services & Hardware Support OS Service Hardware Support Protection Interrupts Traps I/O Synchronization Virtual Memory Scheduling 2 This Time: OS Organizations & Processes OS Organizations (kernels) Processes 3 Monolithic Kernel Classic UNIX approach, Linux Everything in kernel + Fast - Risky 4 Layered OS Design “THE” operating system Dijkstra + Modular, simple, portable, easy to design/debug - Communication overhead, copying, bookkeeping 5 The Microkernel Approach Goal: Minimize contents of kernel Why? 6 Microkernel: Motivation Minimize contents of kernel: Reduces risk of crashing OS Put as much functionality as possible in user-level processes Simplifies extension, modification & customization First μ-kernel: Hydra (CMU) Current systems: Mach (also CMU), by Rick Rashid et al. (now head of MSR) 7 μ-kernels vs. Monolithic Kernels Past conventional wisdom: (1990’s) Today: much faster computers Mach – beautiful research idea but “failed” in practice Too slow! Linux – ugly, monolithic, but fast Mach: fast enough (Mac OS X) Reliability, simplicity, robustness now more important than performance 8 OS Structures & Processes OS Organizations (kernels) Processes 9 Process OS manages variety of activities: User programs, batch jobs, command scripts Daemons: print spoolers, name servers, file servers, etc. Each activity encapsulated in process: Context (PC, registers, etc.) required for activity to run 10 Process != program Process is a running program One program may comprise many processes Many processes may run same program 11 OS and Processes OS manages & schedules processes Creation, deletion Resource allocation (e.g., CPU, memory) Suspension & resumption Inter-process communication Synchronization 12 Processes Process Concept Process States Process Scheduling Process Management Interprocess Communication 13 Process Concept Process – program in execution Process includes: text section: program code current activity: program counter, contents in registers stack data section heap 14 Processes Process Concept Process States Process Scheduling Process Management Interprocess Communication 15 Process States New Process being created Running Instructions being executed Waiting Process waiting for some event to occur Ready Process waiting to be assigned to a processor Terminated (duh) 16 Process State Diagram Transitions: Program actions (system calls) OS actions (scheduling) External events (interrupts) 17 Process Execution Example void main() { printf (“Hello world\n”); } 1. 2. 3. 4. 5. 6. 7. New Ready Running Waiting for I/O Ready Running Terminated 18 Process Data Structures Process Control Block: Tracks state OS allocates, places on queue OS deallocates on termination Lots of info: Process state Program counter CPU registers Memory-management information Accounting information I/O status information 19 Processes Process Concept Process States Process Scheduling Process Management Interprocess Communication 20 Process Scheduling Queues Job queue Ready queue Set of processes residing in main memory ready & waiting to execute Device queues Set of all processes in system Set of processes waiting for I/O device One per device Process migration between the various queues. 21 Process Queues Example 22 CPU Scheduling time 23 PCBs and Hardware State Switching processes: context switch Relatively expensive Start: Time between switches (quantum) must be long enough to amortize this cost OS picks ready process Loads register values from PCB Stop: OS saves registers into PCB 24 Process Scheduling Queuing-diagram representation: 25 Processes Process Concept Process States Process Scheduling Process Management Interprocess Communication 26 Process Creation One process can create other processes Creator = parent, new processes = children Parent can wait for child to complete, or continue in parallel UNIX: fork() used to create child processes Copies variables & registers from parent to child Only difference between parent & child: return value Parent: returns process id of child Child: returns 0 27 Process Creation Example Logging into UNIX creates shell process Every command typed into shell: Child of shell process (spawned by fork) Executes command via exec Example: Type “emacs” OS forks new process exec executes emacs If followed by “&”, runs in parallel; otherwise, waits until done 28 Example UNIX Program: Fork 29 Fork Example: What happened? Parent process: #include <unistd.h> #include <sys/wait.h> #include <stdio.h> int main(){ int parentID = getpid(); char prgname[1024]; gets(prgname); int cid = fork(); if(cid == 0){ execlp(prgname, prgname, 0); printf(“I did not find program %s\n“, prgname); } else{ sleep(1); waitpid(cid, 0, 0); printf("Program %s is finished. \n"); } return 0; } Child process: #include <unistd.h> #include <sys/wait.h> #include <stdio.h> int main(){ int parentID = getpid(); char prgname[1024]; gets(prgname); int cid = fork(); if(cid == 0){ execlp(prgname, prgname, 0); printf(“I did not find program %s\n“, prgname); } else{ sleep(1); waitpid(cid, 0, 0); printf("Program %s is finished. \n"); } return 0; } 30 Parent & Child Process #include <unistd.h> #include <stdio.h> int main(){ int pid; pid = fork(); if(pid == 0){ printf("Child: My PID = %d\n", getpid()); printf("Child: Running...\n"); sleep(20); printf("Child: Done sleeping, returning.\n"); } else{ printf("Parent: My PID = %d\n", getpid()); printf("Parent: Running...\n"); sleep(10); printf("Parent: Done sleeping, returning.\n"); } return 0; } 31 Process Termination On termination, OS reclaims all resources assigned to process UNIX processes: Can terminate self via exit system call Can terminate child via kill system call 32 Example: Process Termination 33 Processes Process Concept Process States Process Scheduling Process Management Interprocess Communication 34 Cooperating Processes Cooperating processes: one process can affect or be affected by other processes Advantages: Can improve performance by overlapping activities or performing work in parallel Can enable simpler program design Simplifies distribution Processes can live on different machines 35 Interprocess Communication Processes communicate in one of two ways: Message passing: Send and receive information Numerous means: sockets, pipes, etc. Shared memory: Establish mapping to named memory object Use mmap Fork processes that share this structure 36 Process Summary Process = unit of execution Represented by Process Control Blocks Process state: New, Ready, Waiting, Running, or Terminated One running process at a time (on a uniprocessor) Contain process state, registers, etc. Context switch when changing process executing on CPU Communicate by message passing or shared memory 37