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

Similar documents
Hardware OS & OS- Application interface

3. Process Management in xv6

Anne Bracy CS 3410 Computer Science Cornell University

Processes. Dr. Yingwu Zhu

Lec 22: Interrupts. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Inf2C - Computer Systems Lecture 16 Exceptions and Processor Management

CSE 153 Design of Operating Systems

Operating System Control Structures

CS 550 Operating Systems Spring Interrupt

Anne Bracy CS 3410 Computer Science Cornell University

Syscalls, exceptions, and interrupts, oh my!

Process Description and Control. Chapter 3

Hakim Weatherspoon CS 3410 Computer Science Cornell University

CSC369 Lecture 2. Larry Zhang

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Precept 2: Non-preemptive Scheduler. COS 318: Fall 2018

CSE 153 Design of Operating Systems Fall 18

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Chap.6 Limited Direct Execution. Dongkun Shin, SKKU

Distributed Systems Operation System Support

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

Dan Noé University of New Hampshire / VeloBit

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

W4118: interrupt and system call. Junfeng Yang

The Kernel Abstraction

Threads and Concurrency

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

SE350: Operating Systems. Lecture 3: Concurrency

Major Requirements of an OS

x86 segmentation, page tables, and interrupts 3/17/08 Frans Kaashoek MIT

CS 537 Lecture 2 - Processes

Operating Systems. Lecture 3- Process Description and Control. Masood Niazi Torshiz

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

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

What is a Process? Answer 1: a process is an abstraction of a program in execution

Protection and System Calls. Otto J. Anshus

CS 326: Operating Systems. Process Execution. Lecture 5

Background: Operating Systems

Interrupts and System Calls

CSE 120 Principles of Operating Systems

CS 104 Computer Organization and Design

Interrupt: (verb) the interruption of a CPU s normal processing...using a mechanism provided for this purpose.

The Kernel Abstraction. Chapter 2 OSPP Part I

Architectural Support for Operating Systems

Lecture 2: September 9

Processes. Johan Montelius KTH

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

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Process Description and Control. Major Requirements of an Operating System

Major Requirements of an Operating System Process Description and Control

A process. the stack

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Process Scheduling Queues

EE458 - Embedded Systems Exceptions and Interrupts

Process Description and Control. Chapter 3

CPS104 Computer Organization and Programming Lecture 17: Interrupts and Exceptions. Interrupts Exceptions and Traps. Visualizing an Interrupt

CSE 120 Principles of Operating Systems

CS510 Operating System Foundations. Jonathan Walpole

Interrupts & System Calls

TCSS 422: OPERATING SYSTEMS

Implementation of Processes

Review: Easy Piece 1

Advanced Operating Systems (CS 202) Scheduling (1)

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

CS 5460/6460 Operating Systems

Lecture 5: Concurrency and Threads

Computer-System Architecture (cont.) Symmetrically Constructed Clusters (cont.) Advantages: 1. Greater computational power by running applications

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Processes & Threads. Process Management. Managing Concurrency in Computer Systems. The Process. What s in a Process?

Process Description and Control

CS333 Intro to Operating Systems. Jonathan Walpole

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Threads and Concurrency

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

EECS 482 Introduction to Operating Systems

Process Description and Control

Processes (Intro) Yannis Smaragdakis, U. Athens

Processes and Threads Implementation

CS 322 Operating Systems Practice Midterm Questions

IT 540 Operating Systems ECE519 Advanced Operating Systems

Xen and the Art of Virtualization. CSE-291 (Cloud Computing) Fall 2016

CSE 410. Operating Systems

Operating Systems ECE344

Architectural Support for Operating Systems

Lecture 2: Architectural Support for OSes

4 Lecture: Operating System Structures

Review: Hardware user/kernel boundary

Part I Introduction Hardware and OS Review

Operating Systems Comprehensive Exam. Spring Student ID # 3/16/2006

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

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

3.1 Introduction. Computers perform operations concurrently

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

John Wawrzynek & Nick Weaver

Process Description and Control. Chapter 3

hardware interrupts software interrupts

Lecture 4: Process Management

Transcription:

Lecture 4: Mechanism of process execution Mythili Vutukuru IIT Bombay

Low-level mechanisms How does the OS run a process? How does it handle a system call? How does it context switch from one process to the other? 2

Process Execution OS allocates memory and creates memory image Code and data (from exe) Stack and heap Points CPU program counter to current instruction Other registers may store operands, return values etc. After setup, OS is out of the way and process executes directly on CPU 3

A simple function call A function call translates to a jump instruction A new stack frame pushed to stack and stack pointer (SP) updated Old value of PC (return value) pushed to stack and PC updated Stack frame contains return value, function arguments etc. 4

How is a system call different? CPU hardware has multiple privilege levels One to run user code: user mode One to run OS code like system calls: kernel mode Some instructions execute only in kernel mode Kernel does not trust user stack Uses a separate kernel stack when in kernel mode Kernel does not trust user provided addresses to jump to Kernel sets up Interrupt Descriptor Table (IDT) at boot time IDT has addresses of kernel functions to run for system calls and other events 5

Mechanism of system call: trap instruction When system call must be made, a special trap instruction is run (usually hidden from user by libc) Trap instruction execution Move CPU to higher privilege level Switch to kernel stack Save context (old PC, registers) on kernel stack Look up address in IDT and jump to trap handler function in OS code 6

More on the trap instruction Trap instruction is executed on hardware in following cases: System call (program needs OS service) Program fault (program does something illegal, e.g., access memory it doesn t have access to) Interrupt (external device needs attention of OS, e.g., a network packet has arrived on network card) Across all cases, the mechanism is: save context on kernel stack and switch to OS address in IDT IDT has many entries: which to use? System calls/interrupts store a number in a CPU register before calling trap, to identify which IDT entry to use 7

Return from trap When OS is done handling syscall or interrupt, it calls a special instruction return-from-trap Restore context of CPU registers from kernel stack Change CPU privilege from kernel mode to user mode Restore PC and jump to user code after trap User process unaware that it was suspended, resumes execution as always Must you always return to the same user process from kernel mode? No Before returning to user mode, OS checks if it must switch to another process 8

Why switch between processes? Sometimes when OS is in kernel mode, it cannot return back to the same process it left Process has exited or must be terminated (e.g., segfault) Process has made a blocking system call Sometimes, the OS does not want to return back to the same process The process has run for too long Must timeshare CPU with other processes In such cases, OS performs a context switch to switch from one process to another 9

The OS scheduler OS scheduler has two parts Policy to pick which process to run (next lecture) Mechanism to switch to that process (this lecture) Non preemptive (cooperative) schedulers are polite Switch only if process blocked or terminated Preemptive (non-cooperative) schedulers can switch even when process is ready to continue CPU generates periodic timer interrupt After servicing interrupt, OS checks if the current process has run for too long 10

Mechanism of context switch Example: process A has moved from user to kernel mode, OS decides it must switch from A to B Save context (PC, registers, kernel stack pointer) of A on kernel stack Switch SP to kernel stack of B Restore context from B s kernel stack Who has saved registers on B s kernel stack? OS did, when it switched out B in the past Now, CPU is running B in kernel mode, return-from-trap to switch to user mode of B 11

A subtlety on saving context Context (PC and other CPU registers) saved on the kernel stack in two different scenarios When going from user mode to kernel mode, user context (e.g., which instruction of user code you stopped at) is saved on kernel stack by the trap instruction Restored by return-from-trap During a context switch, kernel context (e.g., where you stopped in the OS code) of process A is saved on the kernel stack of A by the context switching code Restores kernel context of process B 12