Altering the Control Flow

Similar documents
Altering the Control Flow

Exceptional Control Flow Part I Oct. 17, 2002

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

Exceptional Control Flow Part I September 22, 2008

Giving credit where credit is due

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

Exceptional Control Flow Part I

Exceptional Control Flow: Exceptions and Processes

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?

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

OS Interaction and Processes

Processes, Exceptional

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

Exceptional Control Flow Part I Oct. 28, 2009"

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Processes: Introduction. CS 241 February 13, 2012

CS 201. Processes. Gerson Robboy Portland State University

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

CSE351 Inaugural Edi7on Spring

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

Excep onal Control Flow: Excep ons and Processes

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

Excep&onal Control Flow: Excep&ons and Processes

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?

Program and OS interac0ons: Excep0ons and Processes

University of Washington What is a process?

Processes. CSE 351 Autumn Instructor: Justin Hsia

Approaches to Concurrency

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

Exceptions, Processes and Signals

Processes. CSE 351 Autumn Instructor: Justin Hsia

Exceptions and Processes

Processes. CSE 351 Spring Instructor: Ruth Anderson

Exceptional Control Flow: Exceptions and Processes

CSC 252: Computer Organization Spring 2018: Lecture 19

PROCESS PROGRAMMING INTERFACE

Systems Programming and Computer Architecture ( ) Timothy Roscoe

CSC 1600 Unix Processes. Goals of This Lecture

Foundations of Computer Systems

Introduction to Unix. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Foundations of Computer Systems

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Process Control. Philipp Koehn. 23 April 2018

Excep&onal+Control+Flow:++ Excep&ons+and+Processes+ + 15C213':'Introduc;on'to'Computer'Systems' 14 th 'Lecture,'Oct.'15,'2015'

Operating System Structure

CMPSCI 230 Computer Systems Principles. Processes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Excep&onal Control Flow: Excep&ons and Processes. (Chapter 8)

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

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

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

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

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

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

Virtual Memory I. CSE 351 Autumn Instructor: Justin Hsia

Operating Systems Components. What is an Operating System? Process Management. Memory Management. Operating Systems 9/7/2018 CSC 256/456 1

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

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

Mon Sep 17, 2007 Lecture 3: Process Management

Process Management 1

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Processes, Virtual Memory I

518 Lecture Notes Week 3

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

Processes. Dr. Yingwu Zhu

Processes. CS3026 Operating Systems Lecture 05

CS240: Programming in C

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS 550 Operating Systems Spring Process III

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

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

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

CS 261 Fall Mike Lam, Professor. Processes

CS240: Programming in C

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

CS3733: Operating Systems

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

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

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

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

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

Processes. Johan Montelius KTH

Exceptional Control Flow Part II

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

TCSS 422: OPERATING SYSTEMS

A process. the stack

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

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

Introduction to Processes

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

Transcription:

Altering the Control Flow Up to Now: two mechanisms for changing control flow: Jumps and branches Call and return using the stack discipline. Both react to changes in program state. Insufficient for a useful system Difficult for the CPU to react to changes in system state. data arrives from a disk or a network adapter. Instruction divides by zero User hits ctl-c at the keyboard System timer expires System needs mechanisms for exceptional control flow 1

Exceptional Control Flow 2 Mechanisms for exceptional control flow exists at all levels of a computer system. Low level Mechanism exceptions change in control flow in response to a system event (i.e., change in system state) Combination of hardware and OS software Higher Level Mechanisms Process context switch Signals Nonlocal jumps (setjmp/longjmp) Implemented by either: OS software (context switch and signals). C language runtime library: nonlocal jumps.

System context for exceptions Keyboard Mouse Modem Printer Processor Interrupt controller Keyboard controller Serial port controller Parallel port controller Local/IO Bus Memory IDE disk controller SCSI controller Video adapter Network adapter disk SCSI bus Display Network disk CDROM 3

Exceptions An exception is a transfer of control to the OS in response to some event (i.e., change in processor state) User Process OS event current next exception exception processing by exception handler exception return (optional) 4

Interrupt Vectors 0 1 2 n-1 Exception numbers interrupt vector... code for exception handler 0 code for exception handler 1 code for exception handler 2... code for exception handler n-1 Each type of event has a unique exception number k Index into jump table (a.k.a., interrupt vector) Jump table entry k points to a function (exception handler). Handler k is called each time exception k occurs. 5

256 vectors max 80x86 System Level Registers 6 6

Asynchronous Exceptions (Interrupts) Caused by events external to the processor Indicated by setting the processor s interrupt pin handler returns to next instruction. Examples: I/O interrupts hitting ctl-c at the keyboard arrival of a packet from a network arrival of a data sector from a disk Hard reset interrupt hitting the reset button Soft reset interrupt hitting ctl-alt-delete on a PC 7

Synchronous Exceptions Caused by events that occur as a result of executing an instruction: 8 Traps Intentional Examples: system calls, breakpoint traps, special instructions Returns control to next instruction Faults Unintentional but possibly recoverable Examples: page faults (recoverable), protection faults (unrecoverable). Either re-executes faulting ( current ) instruction or aborts. Aborts unintentional and unrecoverable Examples: parity error, machine check. Aborts current program

Trap Example Opening a File User calls open(filename, options) 0804d070 < libc_open>:... 804d082: cd 80 int $0x80 804d084: 5b pop %ebx... Function open executes system call instruction int OS must find or create file, get it ready for reading or writing Returns integer file descriptor User Process OS int pop exception Open file 9 return

Fault Example #1 Memory Reference User writes to memory location That portion (page) of user s memory is currently on disk Page handler must load page into physical memory Returns to faulting instruction Successful on second try int a[1000]; main () { a[500] = 13; } 80483b7: c7 05 10 9d 04 08 0d movl $0xd,0x8049d10 User Process OS event movl return page fault Create page and load into memory 10

Fault Example #2 Memory Reference User writes to memory location Address is not valid int a[1000]; main () { a[5000] = 13; } 80483b7: c7 05 60 e3 04 08 0d movl $0xd,0x804e360 Page handler detects invalid address Sends SIGSEGV signal to user process User process exits with segmentation fault User Process OS event movl page fault Detect invalid address 11 Signal process

Processes Def: A process is an instance of a running program. One of the most profound ideas in computer science. Not the same as program or processor A process provides each program with two key abstractions: Logical control flow Each thread of a process seems to have exclusive use of a CPU. Private address space Each process seems to have exclusive use of main memory. How are these Illusions maintained? Process thread executions are interleaved (multitasking) Address spaces are managed by a virtual memory system 12

Thread States and Transitions EXIT SLEEP block Run K/U PREEMPT WAKEUP DISPATCH ready fork() 13

Threads The executable (schedulable) elements in a Linux system Each thread in the system is uniquely contained by some process Each user thread is contained by some user PID Each kernel thread is contained in PID 0 When a new process is created, it is populated by exactly one executable thread, known as the Initial Thread (IT) of the new process The IT of a process can create new threads only within its own process While the IT must create the second thread in a process, any subsequent threads can then create new threads, but only within their own process 14

Context Switching Processes are managed by a shared chunk of OS code called the kernel Important: the kernel is not a separate process, but rather runs as part of some thread in some user process Control flow passes from one thread in a process to another thread in the same or a different process via a context switch. Process A thread Process B thread Time user code kernel code user code kernel code user code context switch context switch 15

fork: Creating new processes int fork(void) creates a new process (child process) that is identical to the calling process (parent process) returns 0 to the child process returns child s pid to the parent process if (fork() == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } Fork is interesting (and often confusing) because it is called once but returns twice 16

switch (int pid = fork()){ case -1: perror( fork failed ); exit(1); case 0: printf( child alive\n ); execl(./myprog, myprog, NULL); default: printf( created PID %d \n, pid); } // end switch PID X proga proga code proga data IT stack Parent executes this case Child executes this case fork creates child 17 PID Y proga proga code proga data IT stack Child execl PID Y myprog myprog code myprog data main IT stack

18 Fork Example #1 Key Points Parent and child both run same code Distinguish parent from child by return value from fork Child inherits a copy-on-write (COW) version of parent Including all parent open file descriptors (stdin, stdout, etc.) Relative ordering of parent/child print statements undefined void fork1() { int x = 1; pid_t pid = fork(); if (pid == 0) { printf("child has x = %d\n", ++x); } else { printf("parent has x = %d\n", --x); } printf("bye from process %d with x = %d\n", getpid(), x); }

Some fork_test runs 19 cs cs mercury cs bill@cs3:~/cs305demo$./fork_test Parent has x = 0 Bye from process 24697 with x = 0 Child has x = 2 Bye from process 24698 with x = 2 bill@cs3:~/cs305demo$./fork_test Child has x = 2 Parent has x = 0 Bye from process 24700 with x = 2 Bye from process 24699 with x = 0 -bash-4.1$./fork_test Parent has x = 0 Bye from process 10279 with x = 0 Child has x = 2 Bye from process 10280 with x = 2 bill@cs3:~/cs305demo$./fork_test Parent has x = 0 Child has x = 2 Bye from process 24350 with x = 0 Bye from process 24351 with x = 2

Fork Example #2 Key Points Both parent and child can continue forking void fork2() { printf("l0\n"); fork(); printf("l1\n"); fork(); printf("bye\n"); } L1 L0 L1 Bye Bye Bye Bye 20

exit: Destroying Process void exit(int status) exits a process Normally return with status 0 atexit() registers functions to be executed upon exit void cleanup(void) { printf("cleaning up\n"); } void fork6() { atexit(cleanup); fork(); exit(0); } 21

Idea 22 Zombies When process terminates, still consumes system resources Various tables maintained by OS Called a zombie Living corpse, half alive and half dead Reaping Performed by parent on terminated child Parent is given exit status information Kernel discards process What if Parent Doesn t Reap? If any parent terminates without reaping a child, then child will be reaped by init process Only need explicit reaping for long-running processes E.g., shells and servers

wait: Synchronizing with children int wait(int *child_status) suspends current process until one of its children terminates return value is the pid of the child process that terminated if child_status!= NULL, then the object it points to will be set to a status indicating why the child process terminated Declare a typedef for the exit status information returned from the wait() call ( pid = wait(int *status) ) 23 typedef union{ int exit_status; struct{ unsigned sig_ num:7; unsigned core_dmp:1; unsigned exit_num:8; }parts; }LE_Wait_Status

wait: Synchronizing with children void fork9() { int child_status; } if (fork() == 0) { printf("hc: hello from child\n"); } else { printf("hp: hello from parent\n"); wait(&child_status); printf("ct: child has terminated\n"); } printf("bye\n"); exit(); HC Bye HP CT Bye 24

exec: Running new programs int execl(char *path, char *arg0, char *arg1,, (char *)NULL) loads and runs executable at path with args arg0, arg1, path is the complete path of an executable arg0 becomes the name of the process» typically arg0 is either identical to path, or else it contains only the executable filename from path real arguments to the executable start with arg1, etc. list of args is terminated by a (char *)NULL argument returns -1 if error, otherwise doesn t return! Toto, we re not in Kansas anymore 25 main() { if (fork() == 0) { execl("/usr/bin/cp", "cp", "foo", "bar", (char *)NULL); } wait(null); printf("copy completed\n"); exit(); }

26 Summarizing Exceptions Events that require nonstandard control flow Generated externally (interrupts) or internally (traps and faults) Processes At any given time, system has multiple active processes Each process must have at least one execution thread Only one thread can execute on a processor (core) at a time, but the address space used on a core is that of the process whose thread is currently running there All threads of a given process share a common address space Each running thread appears to have total control of its core and its process s private address space The address space of a process can be in simultaneous use on multiple cores if the process has multiple running threads deployed across these multiple cores

27 Summarizing (cont.) Spawning Processes Call to fork() One call, two returns; one to parent, one to child in new process Terminating Processes Call exit(int exit_code) One call, no return If called by any thread of a process, then all threads in the process will terminate, as will the process itself Reaping Processes Call wait (int * exit_status); Replacing Program Executed by Process Call execl(char* path, char* argv0, (char *)NULL); Actually can use any of 6 exec variants (execl, execlp, execv, etc.) One call, new program starts at main() (no return to caller)