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

Similar documents
Processes. Dr. Yingwu Zhu

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

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

Operating System Structure

SE350: Operating Systems

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

The Kernel Abstraction. Chapter 2 OSPP Part I

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

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

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

CS 550 Operating Systems Spring Interrupt

3.1 Introduction. Computers perform operations concurrently

CSE 153 Design of Operating Systems Fall 2018

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Processes. Process Concept

Chap 4, 5: Process. Dongkun Shin, SKKU

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

Announcements Processes: Part II. Operating Systems. Autumn CS4023

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

Processes. Johan Montelius KTH

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

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

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

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

A process. the stack

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS 326: Operating Systems. Process Execution. Lecture 5

3. Process Management in xv6

ECE 650 Systems Programming & Engineering. Spring 2018

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

Process Control. Philipp Koehn. 23 April 2018

TCSS 422: OPERATING SYSTEMS

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

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

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

COSC243 Part 2: Operating Systems

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

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

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

The Kernel Abstraction

CS 537 Lecture 2 - Processes

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

W4118: interrupt and system call. Junfeng Yang

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

Processes. CS3026 Operating Systems Lecture 05

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

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

COS 318: Operating Systems

Lecture 4: Process Management

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Operating Systems Engineering Recitation #3 (part 2): Interrupt and Exception Handling on the x86. (heavily) based on MIT 6.

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Chapter 4 Multithreaded Programming

Basic OS Programming Abstractions (and Lab 1 Overview)

W4118 Operating Systems. Junfeng Yang

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

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

CSC369 Lecture 2. Larry Zhang

CSC 1600 Unix Processes. Goals of This Lecture

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Lecture 4: Memory Management & The Programming Interface

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

Processes (Intro) Yannis Smaragdakis, U. Athens

csci3411: Operating Systems

Fall 2015 COMP Operating Systems. Lab #3

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

Computer Systems Assignment 2: Fork and Threads Package

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

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

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

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

CSC 539: Operating Systems Structure and Design. Spring 2006

What is a Process? Processes and Process Management Details for running a program

Most of the work is done in the context of the process rather than handled separately by the kernel

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

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

Giving credit where credit is due

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

Exceptional Control Flow Part I

Process Time. Steven M. Bellovin January 25,

Project 2: Signals. Consult the submit server for deadline date and time

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Exceptional Control Flow Part I

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

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

Altering the Control Flow

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

Processes. Processes (cont d)

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

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

Transcription:

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

Previous Class Process state transition Ready, blocked, running Call Stack Execution Context Process switch CPU time slice is used Process is blocked (e.g., waiting for user input) Process has exited Process vs. Thread CSCE 311 Operating Systems 2

Previous class The three basic process states and the four transitions between them Three states: Running, Blocked, Ready (1) Running -> Blocked: I/O; wait for a lock (2) Blocked -> Ready: I/O is ready; the lock is acquired (3) Ready -> Running: a ready process is picked to run (4) Running -> Ready: time slice used up CSCE 311 Operating Systems 3

Previous class What is a Call Stack? A stack data structure that stores the information for the active function calls; it consists of stack frames, each of which corresponds to an active function call CSCE 311 Operating Systems 4

Previous class What is Execution Context? Give two examples that are part of the context The contents of the CPU registers at any point of time. Program counter: a register that points to the instruction to be executed Call stack pointer: a register that points to the top of the call stack, e.g., esp in x86 CPU CSCE 311 Operating Systems 5

Previous class What is Process Switch? When does it occur? Also called task switch. Suspending one process and resuming another. The current process has used up its time slice, blocks (due to I/O or requesting lock), or exits CSCE 311 Operating Systems 6

Process vs. Thread Process A process is an executing instance of a program Different processes have different memory address spaces Resource-heavyweight: significant resources are consumed when creating a new process Thread A thread is the entity within a process that can be scheduled for code execution A process has at least one thread Threads of a process share a lot of information, such as memory address space, opened files, etc. Thus, resource-lightweight to create a new thread CSCE 311 Operating Systems 7

Using Threads over Processes Pros Cheaper to create a new thread Cheaper task switch: when you switch the threads of the same process, you only need to update some of the registers (e.g., esp) Cheaper data sharing between threads of a process Cons One bug in one thread may render the whole process unstable Once one thread is attacked (more precisely, compromised), the whole process is in danger The main reason why oneprocess-per-tab browsers become so popular CSCE 311 Operating Systems 8

Outline Mode Switch Interrupt vs. exception vs. signal fork() and Shell Zombie CSCE 311 Operating Systems 9

Previous class What is Mode Switch? When does it occur? Program execution switches between different CPU modes User -> Kernel: system calls, interrupts, exceptions Kernel -> User: finish handling of sys calls/interrupts/ exceptions CSCE 311 Operating Systems 10

Mode switch Mode switch means program execution switches between different CPU modes User -> kernel Kernel -> user Other: kernel <-> hypervisor When does the user -> kernel mode switch occur? System calls Interrupts Exceptions We have covered system calls; next, we will introduce interrupts and exceptions CSCE 311 Operating Systems 11

Exceptions Programmed exceptions (or traps) int 0x80 // old method of issuing system calls int 3 // single-step debugging Anomalous executions (or faults) a/0 // divide by zero p = NULL; a = *p // dereference null pointer CSCE 311 Operating Systems 12

How Division by Zero is handled? What happens upon a/0? The CPU that is executing the division operation triggers an exception Mode switch to the kernel, and the divided-by-zero handler is invoked automatically The handler sends SIGFPE to the faulty process, and mode switch back to the user space Has the process registered a SIGFPE handler? Yes: execute the SIGFPE handler No: kills the process CSCE 311 Operating Systems 13

Signals due to exceptions An exception is usually converted to a user space signal by the kernel exception handlers CSCE 311 Operating Systems 14

Signals causes Tips: kill l to list all signal names cmd kill signame pid or function kill(pid, sig) : send signals to a process. The parameter pid above is very expressive >0: a real pid 0: all processes in the same process group as the sender process -1: all processes for which the sender has permission to send signals When a signal is sent to a process, it can be handled by any thread of the process pthread_kill(): send signals to a specific thread within the sender s process tgkill(pid, tid, sig): send a signal to thread tid in process pid. This is Linux specific sigqueue(pid, sig, value): send a signal and an associate value to process pid When a process exits, its parent process receives SIGCHLD CSCE 311 Operating Systems 15

Signal handler Signal handlers are shared among threads of a process, while the signal mask is per thread If you want to change the default signal handling behaviors, use sigaction() to install your own handlers; don t use signal() signal() is not reliable in the sense that, upon the invocation of your handler, the default signal handler is restored as default With signal(), when your handler is being invoked, the same type of signals are not blocked Plus, sigaction() is more capable. For example It supports blocking other signals when your handler is invoked If a blocking system call, e.g., read/write(), is interrupted by the signal handling, the system call can be restarted automatically Linux s new signal() implementation supports reliable signals. The implementation actually invokes sigaction(). Don t rely on that; other Unix OSes may not be that way CSCE 311 Operating Systems 16

Interrupts in x86 Processors Each interrupt and exception is identified by a number in [0, 255]. Intel calls this number vector IRQ: Interrupt ReQuest line PIC: Programmable Interrupt Controller NMI: Non-Maskable Interrupt IPI: Inter-Processor Interrupt (through local APIC) CSCE 311 Operating Systems 17

Interrupt Descriptor Table (IDT) Used by both Interrupt and Exception handling Each entry is a descriptor that refers to an Interrupt or Exception handler Difference between the Interrupt entry and the Exception entry CPU will clear the IF flag to disable local interrupts upon handling of an interrupt (using cli instruction) IF flag will not be disabled when handling exceptions CSCE 311 Operating Systems 18

Interrupt/exception handling CPU uses the IDT to jump to a handler automatically. Below shows interrupt handling CSCE 311 Operating Systems 19

Interrupt/exception handling Basic steps: Mode switch to kernel mode if the current mode is user mode Save the current context Invoke the corresponding handler function Restore the context Mode switch back to user mode if the original mode was user mode CSCE 311 Operating Systems 20

What happens upon a keystroke? Interrupt handling Hardware part CPU refers to IDT to locate the handler Software part Execution of the handler according to the interrupt number CSCE 311 Operating Systems 21

Interrupts, Exceptions and Signals Type Triggered by Examples Exceptions (or, s/w interrupts) Signals (sent by kernel; handled in userspace) Interrupts (or, h/w interrupts) Language exceptions (as in C++ and Java) Instruction execution kill(); sent by exception handler interval timer and I/O throw breakpoint; page fault; divide-by-zero; system calls SIGTRAP; SIGSEGV; SIGFPE timer; input available throw std::invalid_argument( ); CSCE 311 Operating Systems 22

Which processes have PID 0 and PID 1 Try command ps -eaf PID 0: idle process The first process Invoke hlt instructions when being scheduled to save power PID 1: init process Initially it is a kernel thread created by idle Then exec(init) to become a regular process CSCE 311 Operating Systems 23

How is a process created? Userspace view fork(): create a new process int pid = fork(); if (pid < 0) { // error; no process created; } else if (pid > 0) { // this is the parent process else { // pid == 0 // this is the child process } CSCE 311 Operating Systems 24

CSCE 311 Operating Systems 25

Some APIs critical for implementing shell The exec() family of functions (execl, execlp, execle, ) changes the program being executed. E.g., execl("/bin/ls","ls","-l",null); /bin/ls determines the program to be executed, while ls, -l form argv[] The wait() system call suspends execution of the calling process until one of its children terminates. CSCE 311 Operating Systems 26

How is shell implemented? char *prog, **args; int child_pid; Q: How is system(char* cmd) implemented? A: Just remove the loop wrapper of the left // Read and parse the input a line at a time while (readandparsecmdline(&prog, &args)) { child_pid = fork(); if (child_pid < 0) exit(-1); if (child_pid == 0) { exec(prog, args); // NOT REACHED } else { wait(child_pid); } } CSCE 311 Operating Systems 27

Zombie Process in Linux/Unix Once a child process exits, it enters the zombie state process. A zombie process is cleaned up if Its parent calls wait() to retrieve the exit state, or if the parent explicitly ignores SIGCHLD by setting its handler to SIG_IGN If a parent process exits, its zombie child processes become children of the init (pid = 1) process, which periodically reaps zombies Zombie processes occupy precious kernel resources (e.g., PCB), so you want to reclaim them ASAP If a process has created a lot of child processes, and the parent does not reap them, what will happen? CSCE 311 Operating Systems 29

A more complete Process-State Transition Graph CSCE 311 Operating Systems 30

Summary Interrupt vs. exception vs. signal fork() and Shell Zombie CSCE 311 Operating Systems 31

Interesting Readings Causes of signals in Linux/Unix https://unix.stackexchange.com/a/6337/58273 ld (compile-time linker) vs ld-linux.so (load-time linker) http://www.linuxjournal.com/article/6463 ldd https://stackoverflow.com/questions/1488527/hierarchical-ldd1 What happens when you run a program https://stackoverflow.com/a/1220211/577165 Good slides https://www.cs.columbia.edu/~junfeng/10sp-w4118/lectures/l05- syscall-intr-linux.pdf https://www.cs.princeton.edu/courses/archive/fall10/cos318/ lectures/osstructure.pdf http://slideplayer.com/slide/1595473/ CSCE 311 Operating Systems 32