CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

Similar documents
CS 261 Fall Mike Lam, Professor. Processes

OS Interaction and Processes

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

Process Control. Philipp Koehn. 23 April 2018

Exceptional Control Flow Part I

TCSS 422: OPERATING SYSTEMS

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

Exceptional Control Flow Part I September 22, 2008

Exceptional Control Flow Part I Oct. 17, 2002

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

Exceptional Control Flow Part I

Exceptions and Processes

Exceptional Control Flow: Exceptions and Processes

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

Altering the Control Flow

Announcement (1) sys.skku.edu is now available

Giving credit where credit is due

Altering the Control Flow

Q & A (1) Where were string literals stored? Virtual Address. SSE2033: System Software Experiment 2 Spring 2016 Jin-Soo Kim

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

CSC 1600 Unix Processes. Goals of This Lecture

Are branches/calls the only way we can get the processor to go somewhere in a program? What is a program? A processor? A process?

Processes: Introduction. CS 241 February 13, 2012

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

Operating systems and concurrency - B03

UNIX Processes. by Armin R. Mikler. 1: Introduction

Exceptions, Processes and Signals

Processes. Dr. Yingwu Zhu

Exceptional Control Flow: Exceptions and Processes

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Operating System Structure

Processes, Exceptional

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

System Programming. Process Control II

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

Operating systems fundamentals - B06

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

CS240: Programming in C

CS240: Programming in C

Program and OS interac0ons: Excep0ons and Processes

Processes. Process Concept

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

CS510 Operating System Foundations. Jonathan Walpole

CSC 252: Computer Organization Spring 2018: Lecture 19

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

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

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

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

The course that gives CMU its Zip! Concurrency I: Threads April 10, 2001

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

University of Washington What is a process?

Carnegie Mellon. Processes. Lecture 12, May 19 th Alexandre David. Credits to Randy Bryant & Dave O Hallaron from Carnegie Mellon

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing

Exceptional Control Flow Part I Oct. 28, 2009"

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Processes. Johan Montelius KTH

CS 201. Processes. Gerson Robboy Portland State University

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

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

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

A process. the stack

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Processes and Exceptions

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

System Programming. Signals II

Processes. CS3026 Operating Systems Lecture 05

Recitation 8 Processes, Signals, Tshlab

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Introduction to Computer Systems , fall th Lecture, Oct. 7 th

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

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

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

CS 201. Exceptions and Processes. Gerson Robboy Portland State University

The Big Picture So Far. Today: Process Management

Processes. CSE 351 Autumn Instructor: Justin Hsia

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Operating Systems. II. Processes

Process Creation and Control

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

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

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Exceptional Control Flow

Processes and Threads

Processes. CSE 351 Autumn Instructor: Justin Hsia

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Computer Science & Engineering Department I. I. T. Kharagpur

518 Lecture Notes Week 3

SOFTWARE ARCHITECTURE 3. SHELL

Maria Hybinette, UGA. ! One easy way to communicate is to use files. ! File descriptors. 3 Maria Hybinette, UGA. ! Simple example: who sort

Transcription:

CS 261 Fall 2017 Mike Lam, Professor Exceptional Control Flow and Processes

Exceptional control flow Most control flow is sequential However, we have seen violations of this rule

Exceptional control flow Exceptions violate sequential control flow Unconditional transfer to another location in code Often the result of an error condition But not necessarily we can also use exceptions for time-sharing! Categorized as asynchronous vs. synchronous Whether it happens as a result of an external source or not Categorized by recovery possibility Always returns, sometimes returns, or never returns If recovery is possible, further categorized by recovery location Same instruction vs. next instruction Common exceptions are assigned code numbers in Linux

Interrupts Interrupt: communication mechanism Asynchronous, always returns to next instruction Interrupts execution as the result of an outside event An I/O operation has finished The process has finished its time slice

Traps Trap: intentional control transfer to kernel Synchronous, (almost) always returns to next instruction Like a function call, except the target runs in kernel mode Also referred to as system calls x86-64 instruction syscall w/ ID in %rax Parameters are passed in %rdi-%r9; return value stored in %rax Well-known standards (e.g., POSIX)

Faults Fault: error that is potentially correctable Synchronous, sometimes returns to same instruction We have already seen some of these!

Faults Fault: error that is potentially correctable Synchronous, sometimes returns to same instruction Page fault (#14): virtual memory cache miss Recoverable read the required page from slower memory Segmentation fault (#13): invalid memory access Not recoverable undefined behavior Divide-by-zero error (#0) Not recoverable undefined result

Aborts Abort: unrecoverable error Synchronous, never returns Machine check (#18): fatal hardware error

Exception implementation Kernel exception table Maps from exception ID to handler address

System calls Some of the functions we've been using in C are actually wrappers for a system call (or multiple system calls) fopen, fread, malloc System calls: open (id=2), read (id=0), mmap (id=9) System call interfaces are defined by standards SysV vs. POSIX (IEEE standard: http://pubs.opengroup.org/onlinepubs/9699919799/) In general, system call wrappers are called system-level functions It is important to check for errors after calling these functions Textbook uses wrapper functions (e.g., "Open") for this int fd = open("file.txt", O_RDONLY); if (fd < 0) { fprintf(stderr, "Error opening file: %s\n", strerror(errno)); exit(exit_failure); }

Processes Exceptions enable processes Process: a running program One program w/ multiple running instances Abstraction provided by OS kernel One kernel, many user processes Shared portion of virtual address space Kernel memory (above stack) This region is not visible to user programs Toggle control (kernel and processes) Interrupts cycle through processes ("round robin") Traps function call from processes into kernel ("syscalls") Faults software error (recover or abort) Aborts stop process without taking down the machine

Processes Process: instance of an executing program Independent single logical flow and private virtual address space Logical flow: sequence of executed instructions Concurrency: overlapping logical flows Multitasking: processes take turns Parallelism: concurrent flows on separate CPUs/cores Time CPU Core1 Core2 Logical flow Concurrent flows Multitasking concurrent flows Parallel concurrent flows

Implementing processes Processes are abstractions Implemented/provided by the operating system kernel Kernel maintains data structure w/ process information Including an ID for each process (pid) Multitasking via exceptional control flow Periodic interrupt to switch processes Called round-robin switching Context switch: swapping current process Save context of old process Restore context of new process Pass control to the restored process

Linux process tools ps list processes "ps -fe" to see all processes on the system "ps -fu <username>" to see your processes top list processes, ordered by current CPU Auto-updates /proc virtual filesystem exposing kernel data structures pmap display memory map of a process strace prints a list of system calls from a process Compile with "-static" to get cleaner traces

Process creation The fork() syscall creates a new process Initializes new entry in the kernel data structures To user code, the function call returns twice Once for original process (parent) and once for new process (child) Returns 0 in child process Returns child pid in parent process Both processes will continue executing concurrently Parent and child have separate address spaces Child's space is a duplicate of parent's at the time of the fork They will diverge after the fork! Child inherits parent's environment and open files

Process creation example Fork returns twice! int main () { printf("before fork\n"); int pid = fork(); printf("after fork: pid=%d\n", pid); } return 0;

Process creation example What does this code do? int main () { printf("before fork\n"); int pid1 = fork(); printf("after fork: pid1=%d\n", pid1); int pid2 = fork(); printf("after second fork: pid1=%d pid2=%d\n", pid1, pid2); } return 0;

Process creation example Fork returns twice! (every time) Beware of non-determinism and I/O interleaving int main () { printf("before fork\n"); int pid1 = fork(); printf("after fork: pid1=%d\n", pid1); int pid2 = fork(); printf("after second fork: pid1=%d pid2=%d\n", pid1, pid2); } return 0;

Process creation example Fork returns twice! (every time) Beware of non-determinism and I/O interleaving int main () { printf("before fork\n"); int pid1 = fork(); printf("after fork: pid1=%d\n", pid1); int pid2 = fork(); printf("after second fork: pid1=%d pid2=%d\n", pid1, pid2); } return 0; Exercise: Modify this program to fork a total of three processes

Parent/child process example Parents can wait for children to finish int main () { printf("before fork\n"); int pid = fork(); if (pid!= 0) { // parent wait(null); printf("child has terminated.\n"); } else { // child printf("child is running.\n"); } printf("after fork: pid=%d\n", pid); } return 0;

Process control syscalls #include <stdlib.h> getenv: get environment variable value setenv: change environment variable value #include <unistd.h> fork: create a new process getpid: return current process id (pid) getppid: return parent s process id (pid) exit: terminate current process execve: load and run another program in the current process sleep: suspend process for specified time period #include <sys/wait.h> waitpid: wait for a child process to terminate wait: wait for all child processes to terminate

Fork/execve example Shells use fork() and execve() to run commands int main () { printf("before fork\n"); int pid = fork(); if (pid!= 0) { // parent wait(null); printf("child has terminated.\n"); /bin/uname } else { // child printf("child is running.\n"); char *cmd = "/bin/uname"; char *args[] = { "uname", "-a", NULL }; char *env[] = { NULL }; execve(cmd, args, env); printf("this won't print unless an error occurs.\n"); } } printf("after fork: pid=%d\n", pid); return 0;