Recall: Four fundamental OS concepts. Recall: give the illusion of multiple processors? Process Control Block

Similar documents
Interrupts, Fork, I/O Basics

September 2 nd, 2015 Prof. John Kubiatowicz

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

The Kernel Abstraction. Chapter 2 OSPP Part I

518 Lecture Notes Week 3

Lecture 4: Memory Management & The Programming Interface

SE350: Operating Systems

The Kernel Abstrac/on

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

The Kernel Abstraction

A process. the stack

Lecture 2 Fundamental OS Concepts. Bo 2018, Spring

The Kernel. wants to be your friend

CS 5460/6460 Operating Systems

Processes. Johan Montelius KTH

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Computer architecture. A simplified model

Protection. OS central role. Fundamental to other OS goals. OS kernel. isolation of misbehaving applications. Relaibility Security Privacy fairness

Processes. CS439: Principles of Computer Systems January 30, 2019

TCSS 422: OPERATING SYSTEMS

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CSC 1600 Unix Processes. Goals of This Lecture

Lecture 4: Process Management

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CS240: Programming in C

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Processes. CS439: Principles of Computer Systems January 24, 2018

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS510 Operating System Foundations. Jonathan Walpole

CS5460: Operating Systems

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

Processes. Sanzheng Qiao. December, Department of Computing and Software

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

CSE 153 Design of Operating Systems Fall 2018

Process Management! Goals of this Lecture!

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

CS 550 Operating Systems Spring Process II

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Processes & Threads. (Chapter 3) CS 4410 Operating Systems. [R. Agarwal, L. Alvisi, A. Bracy, M. George, E. Sirer, R. Van Renesse]

CSE 153 Design of Operating Systems

Exceptional Control Flow Part I

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS Advanced Operating Systems Structures and Implementation Lecture 5. Processes. Goals for Today

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

W4118 Operating Systems. Junfeng Yang

Exceptional Control Flow Part I

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

! The Process Control Block (PCB) " is included in the context,

Operating Systems Lab

Announcement. Exercise #2 will be out today. Due date is next Monday

Altering the Control Flow

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Process Management 1

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Altering the Control Flow

Process Control. Philipp Koehn. 23 April 2018

3. Process Management in xv6

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

CS240: Programming in C

System Programming. Process Control II

Problem Set: Processes

Control Flow. Systemprogrammering 2007 Föreläsning 2 Exceptional Control Flow Part I. Exceptional Control Flow. Altering the Control Flow

EECS 482 Introduction to Operating Systems

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Exceptional Control Flow Part I Oct. 17, 2002

Lecture 15: I/O Devices & Drivers

Linux Operating System

Operating Systems CS 571

Mon Sep 17, 2007 Lecture 3: Process Management

Process Management! Goals of this Lecture!

Operating Systems ECE344

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Fall 2015 COMP Operating Systems. Lab #3

CS 326: Operating Systems. Process Execution. Lecture 5

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Multithreaded Programming

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Problem Set: Processes

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

Interprocess Communication

CSE 153 Design of Operating Systems

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Transcription:

CS162 Operating Systems and Systems Programming Lecture 3 Processes (con t), Fork, Introduction to I/O September 1 st, 2016 Prof. Anthony D. Joseph http://cs162.eecs.berkeley.edu Thread Recall: Four fundamental OS concepts Single unique execution context Program Counter, Registers, Execution Flags, Stack Address Space w/ Translation Programs execute in an address space that is distinct from the memory space of the physical machine Process An instance of an executing program is a process consisting of an address space and one or more threads of control Dual Mode operation/protection Only the system has the ability to access certain resources The OS and the hardware are protected from user programs and user programs are isolated from one another by controlling the translation from program virtual addresses to machine physical addresses Lec 3.2 Process Control Block Kernel represents each process as a process control block (PCB) Status (running, ready, blocked, ) Register state (when not ready) Process ID (PID), User, Executable, Priority, Execution time, Memory space, translation, Kernel Scheduler maintains a data structure containing the PCBs Scheduling algorithm selects the next one to run Lec 3.3 Recall: give the illusion of multiple processors? vcpu1 vcpu2 Shared Memory vcpu3 vcpu1 vcpu2 vcpu3 vcpu1 vcpu2 Time Assume a single processor. How do we provide the illusion of multiple processors? Multiplex in time! Multiple virtual CPUs Each virtual CPU needs a structure to hold: Program Counter (PC), Stack Pointer (SP) Registers (Integer, Floating point, others?) How switch from one virtual CPU to the next? Save PC, SP, and registers in current state block Load PC, SP, and registers from new state block What triggers switch? Timer, voluntary yield, I/O, other things Lec 3.4

Simultaneous MultiThreading/Hyperthreading Hardware technique Superscalar processors can execute multiple instructions that are independent Hyperthreading duplicates register state to make a second thread, allowing more instructions to run Can schedule each thread as if were separate CPU But, sub-linear speedup! Original technique called Simultaneous Multithreading http://www.cs.washington.edu/research/smt/index.html SPARC, Pentium 4/Xeon ( Hyperthreading ), Power 5 Colored blocks show instructions executed Lec 3.5 Scheduler if ( readyprocesses(pcbs) ) { nextpcb = selectprocess(pcbs); run( nextpcb ); else { run_idle_process(); Scheduling: Mechanism for deciding which processes/threads receive the CPU Lots of different scheduling policies provide Fairness or Realtime guarantees or Latency optimization or.. Lec 3.6 Server Kernel wait interrupt Hardware Putting it together: web server 1. network socket read 2. copy arriving packet (DMA) request buffer 3. kernel copy Network Interface 4. parse request RTU 9. format reply 10. network socket write 11. kernel copy from user buffer into network buffer 12. format outgoing packet and DMA wait reply buffer 5. file read 6. disk request Disk Interface 8. kernel copy RTU interrupt 7. disk data (DMA) Syscall Recall: 3 types of Kernel Mode Transfer Process requests a system service, e.g., exit Like a function call, but outside the process Does not have the address of the system function to call Like a Remote Procedure Call (RPC) for later Marshall the ID and arguments in registers and execute Interrupt External asynchronous event triggers context switch eg. Timer, I/O device Independent of user process Trap or Exception Internal synchronous event in process triggers context switch e.g., Protection violation (segmentation fault), Divide by zero, Lec 3.7 Lec 3.8

Recall: User/Kernel (Privileged) Mode Implementing Safe Kernel Mode Transfers User Mode interrupt exception Important aspects: Separate kernel stack Controlled transfer into kernel (e.g. table) exec rtn rfi Kernel Mode exit Carefully constructed kernel code packs up the user process state and sets it aside Details depend on the machine architecture Should be impossible for buggy or malicious user program to cause the kernel to corrupt itself Limited HW access Full HW access Lec 3.9 Lec 3.10 Need for Separate Kernel Stacks Before Kernel needs space to work Cannot put anything on the user stack (Why?) Two-stack model OS thread has interrupt stack (located in kernel memory) plus User stack (located in user memory) Syscall handler copies user args to kernel space before invoking specific function (e.g., open) Interrupts (???) User Stack running main proc1 proc2 ready to run main proc1 proc2 waiting for I/O main proc1 proc2 User-level Process code: foo () { while() { x = x+1; y = y-2; stack: Registers SS: ESP CS: EIP EFLAGS other registers: EAX, EBX, Kernel code: handler() { pusha Exception Stack Kernel Stack user CPU state user CPU state handler I/O driver top half Lec 3.11 Lec 3.12

During Kernel System Call Handler User-level Process code: foo () { while() { x = x+1; y = y-2; stack: Registers SS: ESP CS: EIP EFLAGS other registers: EAX, EBX, Kernel code: handler() { pusha Exception Stack SS ESP EFLAGS CS EIP error Vector through well-defined entry points! Table mapping system call number to handler Locate arguments In registers or on user(!) stack Copy arguments From user memory into kernel memory Protect kernel from malicious code evading checks Validate arguments Protect kernel from errors in user code Copy results back into user memory Lec 3.13 Lec 3.14 Administrivia: Getting started We have a new discussion section! 162-112 Th 6:30-7:30P 310 Soda Joseph Office Hours: Mondays/Tuesdays 11-12 in 465F Soda THIS Friday (9/2) is early drop day! Very hard to drop afterwards Work on Homework 0 immediately! Due on Monday! Get familiar with all the cs162 tools Submit to autograder via git Lec 3.15 Hardware support: Interrupt Control Interrupt processing not be visible to the user process: Occurs between instructions, restarted transparently No change to process state What can be observed even with perfect interrupt processing? Interrupt Handler invoked with interrupts disabled Re-enabled upon completion Non-blocking (run to completion, no waits) Pack up in a queue and pass off to an OS thread for hard work» wake up an existing OS thread OS kernel may enable/disable interrupts On x86: CLI (disable interrupts), STI (enable) Atomic section when select next process/thread to run Atomic return from interrupt or HW may have multiple levels of interrupt Mask off (disable) certain interrupts, eg., lower priority Certain Non-Maskable-Interrupts (NMI)» e.g., kernel segmentation fault Lec 3.17

Interrupt Controller How do we take interrupts safely? Network IntID Interrupt Interrupts invoked with interrupt lines from devices Interrupt controller chooses interrupt request to honor Mask enables/disables interrupts Priority encoder picks highest enabled interrupt Software Interrupt Set/Cleared by Software Interrupt identity specified with ID line CPU can disable all interrupts with internal flag Non-Maskable Interrupt line (NMI) can t be disabled Timer Interrupt Mask Software Interrupt Priority Encoder Control CPU Int Disable NMI Interrupt vector Limited number of entry points into kernel Kernel interrupt stack Handler works regardless of state of user code Interrupt masking Handler is non-blocking Atomic transfer of control Single instruction -like to change:» Program counter» Stack pointer» Memory protection» Kernel/user mode Transparent restartable execution User program does not know interrupt occurred Lec 3.18 Lec 3.19 Can a process create a process? Yes! Unique identity of process is the process ID (or PID) Fork() system call creates a copy of current process with a new PID Return value from Fork(): integer When > 0:» Running in (original) Parent process» return value is pid of new child When = 0:» Running in new Child process When < 0:» Error! Must handle somehow» Running in original process All state of original process duplicated in both Parent and Child! Memory, File Descriptors (next topic), etc Lec 3.20 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> fork1.c #define BUFSIZE 1024 int main(int argc, char *argv[]) { char buf[bufsize]; size_t readlen, writelen, slen; pid_t cpid, mypid; pid_t pid = getpid(); /* get current processes PID */ printf("parent pid: %d\n", pid); cpid = fork(); if (cpid > 0) { /* Parent Process */ printf("[%d] parent of [%d]\n", mypid, cpid); else if (cpid == 0) { /* Child Process */ printf("[%d] child\n", mypid); else { perror("fork failed"); exit(1); exit(0); Lec 3.21

UNIX Process Management UNIX fork system call to create a copy of the current process, and start it running No arguments! UNIX exec system call to change the program being run by the current process UNIX wait system call to wait for a process to finish UNIX signal system call to send a notification to another process fork2.c int status; cpid = fork(); if (cpid > 0) { /* Parent Process */ printf("[%d] parent of [%d]\n", mypid, cpid); tcpid = wait(&status); printf("[%d] bye %d(%d)\n", mypid, tcpid, status); else if (cpid == 0) { /* Child Process */ printf("[%d] child\n", mypid); UNIX man pages: fork(2), exec(3), wait(2), signal(3) Lec 3.22 Lec 3.23 UNIX Process Management Administrivia (Con t) Participation: Attend section! Get to know your TA! pid = fork(); if (pid == 0) exec(); else wait(pid); fork pid = fork(); if (pid == 0) exec(); else wait(pid); pid = fork(); if (pid == 0) exec(); else wait(pid); exec wait main () { Please use private Piazza posts only for student logistics issues Group sign up via autograder then TA form next week (after EDD) Get finding groups of 4 people ASAP Priority for same section; if cannot make this work, keep same TA Lec 3.24 Lec 3.25

A shell is a job control system Shell Allows programmer to create and manage a set of programs to do some task Windows, MacOS, Linux all have shells Example: to compile a C program BREAK cc c sourcefile1.c cc c sourcefile2.c ln o program sourcefile1.o sourcefile2.o./program HW1 Lec 3.26 Lec 3.27 Signals infloop.c Process Races: fork3.c #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <signal.h> void signal_callback_handler(int signum) { printf("caught signal %d - phew!\n",signum); exit(1); int main() { signal(sigint, signal_callback_handler); while (1) { int i; cpid = fork(); if (cpid > 0) { printf("[%d] parent of [%d]\n", mypid, cpid); for (i=0; i<100; i++) { printf("[%d] parent: %d\n", mypid, i); // sleep(1); else if (cpid == 0) { printf("[%d] child\n", mypid); for (i=0; i>-100; i--) { printf("[%d] child: %d\n", mypid, i); // sleep(1); Question: What does this program print? Does it change if you add in one of the sleep() statements? Lec 3.28 Lec 3.29

User Mode Recall: UNIX System Structure Applications Standard Libs How Does the Kernel Provide Services? You said that applications request services from the operating system via, but I ve been writing all sort of useful applications and I never ever saw a!!! Kernel Mode That s right. It was buried in the programming language runtime library (e.g., libc.a) Layering Hardware Lec 3.30 Lec 3.31 OS Run-Time Library A Kind of Narrow Waist Proc 1 Proc 2 OS Proc n Appln login Window Manager OS library OS library OS library Software User Compilers System Databases Word Processing Web Browsers Email Web Servers Portable OS Library System Call Interface Portable OS Kernel OS Platform support, Device Drivers Application / Service OS Hardware x86 PowerPC Ethernet (1Gbs/10Gbs) 802.11 a/g/n/ac SCSI ARM Graphics PCI Thunderbolt Lec 3.32 Lec 3.33

Key Unix I/O Design Concepts Uniformity file operations, device I/O, and interprocess communication through open, read/write, close Allows simple composition of programs» find grep wc Open before use Provides opportunity for access control and arbitration Sets up the underlying machinery, i.e., data structures Byte-oriented Even if blocks are transferred, addressing is in bytes Kernel buffered reads Streaming and block devices looks the same read blocks process, yielding processor to other task Kernel buffered writes Completion of out-going transfer decoupled from the application, allowing it to continue Explicit close I/O & Storage Layers Application / Service streams High Level I/O Low Level I/O handles Syscall registers File System descriptors I/O Driver Commands and Data Transfers Disks, Flash, Controllers, DMA Lec 3.34 Lec 3.35 The File System Abstraction High-level idea Files live in hierarchical namespace of filenames File Named collection of data in a file system File data» Text, binary, linearized objects File Metadata: information about the file» Size, Modification Time, Owner, Security info» Basis for access control Directory Folder containing files & Directories Hierachical (graphical) naming» Path through the directory graph» Uniquely identifies a file or directory /home/ff/cs162/public_html/fa16/index.html Links and Volumes (later) Summary Process: execution environment with Restricted Rights Address Space with One or More Threads Owns memory (address space) Owns file descriptors, file system context, Encapsulate one or more threads sharing process resources Interrupts Hardware mechanism for regaining control from user Notification that events have occurred User-level equivalent: Signals Native control of Process Fork, Exec, Wait, Signal Basic Support for I/O Standard interface: open, read, write, seek Device drivers: customized interface to hardware Lec 3.36 Lec 3.37