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

Similar documents
Processes, Context Switching, and Scheduling. Kevin Webb Swarthmore College January 30, 2018

Processes. Dr. Yingwu Zhu

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College March 31, 2015

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

SMD149 - Operating Systems

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Altering the Control Flow

Altering the Control Flow

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

Processes. CS3026 Operating Systems Lecture 05

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

Operating System Structure

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

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

CS 31: Intro to Systems Operating Systems Overview. Kevin Webb Swarthmore College November 8, 2018

CS 322 Operating Systems Practice Midterm Questions

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

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

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

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

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

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

Process Concepts 8/21/2014. CS341: Operating System. Scheduling: Theoretical Analysis

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

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

Exceptional Control Flow Part I September 22, 2008

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

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

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

CS 201. Processes. Gerson Robboy Portland State University

2 Processes. 2 Processes. 2 Processes. 2.1 The Process Model. 2.1 The Process Model PROCESSES OPERATING SYSTEMS

Mon Sep 17, 2007 Lecture 3: Process Management

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 21

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

CS 326: Operating Systems. Process Execution. Lecture 5

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

COSC243 Part 2: Operating Systems

Operating Systems. Computer Science & Information Technology (CS) Rank under AIR 100

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

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

Processes: Introduction. CS 241 February 13, 2012

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

Process Control. Philipp Koehn. 23 April 2018

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

Giving credit where credit is due

Exceptional Control Flow Part I Oct. 17, 2002

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

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Lecture 4: Process Management

Process Description and Control. Chapter 3

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

CSC 1600 Unix Processes. Goals of This Lecture

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

3.1 Introduction. Computers perform operations concurrently

OPERATING SYSTEMS: Lesson 4: Process Scheduling

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

Announcements Processes: Part II. Operating Systems. Autumn CS4023

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

Problem Set: Processes

Exceptional Control Flow Part I

Killing Zombies, Working, Sleeping, and Spawning Children

Process Description and Control. Chapter 3

Operating System Labs. Yuanbin Wu

Chap 4, 5: Process. Dongkun Shin, SKKU

CHAPTER 2: PROCESS MANAGEMENT

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

o Reality The CPU switches between each process rapidly (multiprogramming) Only one program is active at a given time

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Computer Systems II. First Two Major Computer System Evolution Steps

OS 1 st Exam Name Solution St # (Q1) (19 points) True/False. Circle the appropriate choice (there are no trick questions).

csci3411: Operating Systems

Introduction to Processes

Fall 2015 COMP Operating Systems. Lab #3

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

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?

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

CSCE 313: Intro to Computer Systems

Process Time. Steven M. Bellovin January 25,

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

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

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

W4118 Operating Systems. Junfeng Yang

Exceptional Control Flow Part I

The Big Picture So Far. Today: Process Management

INF1060: Introduction to Operating Systems and Data Communication. Pål Halvorsen. Wednesday, September 29, 2010

CMPSCI 230 Computer Systems Principles. Processes

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

Process. Discussion session 3 1/30/2016

CS240: Programming in C

Lecture 4: Memory Management & The Programming Interface

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

Problem Set: Processes

3. Process Management in xv6

Processes. Process Scheduling, Process Synchronization, and Deadlock will be discussed further in Chapters 5, 6, and 7, respectively.

Computer Science 330 Operating Systems Siena College Spring Lab 5: Unix Systems Programming Due: 4:00 PM, Wednesday, February 29, 2012

Transcription:

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

Reading Quiz

Anatomy of a Process Abstraction of a running program a dynamic program in execution OS keeps track of process state What each process is doing Which one gets to run next Basic operations Suspend/resume (context switch) Start (spawn), terminate (kill)

Timesharing P 1 P 2 P 3 P 1 P 2 P 3 P 1 P 2 P 3 quantum time Multiple processes, single CPU (or small number) Conceptually, each process makes progress over time In reality, each periodically gets quantum of CPU time Illusion of parallel progress by rapidly switching CPU

How is Timesharing Implemented? Kernel keeps track of progress of each process Characterizes state of process s progress Running: actually making progress, using CPU Ready: able to make progress, but not using CPU Blocked: not able to make progress, can t use CPU Kernel selects a ready process, lets it run Eventually, the kernel gets back control Selects another ready process to run,

Why might a process be blocked (unable to make progress / use CPU)? A. It s waiting for another process to do something. B. It s waiting for memory to find and return a value. C. It s waiting for an I/O device to do something. D. More than one of the above. (Which ones?) E. Some other reason(s).

Process State Diagram Ready dispatch preempt Running wake up Blocked sleep State transitions Dispatch: allocate the CPU to a process Preempt: take away CPU from process Sleep: process gives up CPU to wait for event Wakeup: event occurred, make process ready

Kernel Maintains Process Table Process ID (PID) State Other info 1534 Ready Saved context, 34 Running Memory areas used, 487 Ready Saved context, 9 Blocked Condition to unblock, List of processes and their states Also sometimes called process control block (PCB) Other state info includes contents of CPU context areas of memory being used other information Values of registers in use by process

Multiprogramming Given a running process At some point, it needs a resource, e.g., I/O device If resource is busy (or slow), process can t proceed Voluntarily gives up CPU to another process Mechanism: Context switching

Context Switching Allocating CPU from one process to another First, save context of currently running process Next, load context of next process to run

Context Switching Allocating CPU from one process to another First, save context of currently running process Next, load context of next process to run Loading the context Load general registers, stack pointer, etc. Load program counter (must be last instruction!)

How a Context Switch Occurs Process makes system call (TRAP) or is interrupted These are the only ways of entering the kernel In hardware Switch from user to kernel mode: amplifies power Go to fixed kernel location: interrupt/trap handler In software (in the kernel code) Save context of last-running process Conditionally Select new process from those that are ready Restore context of selected process OS returns control to a process from interrupt/trap

Why shouldn t processes control context switching? A. It would cause too much overhead. B. They could refuse to give up the CPU. C. They don t have enough information about other processes. D. Some other reason(s).

Time Sharing / Multiprogramming Given a running process At some point, it needs a resource, e.g., I/O device If resource is busy (or slow), process can t proceed Voluntarily gives up CPU to another process Mechanism: Context switching Policy: CPU scheduling

The CPU Scheduling Problem Given multiple processes, but only one CPU How much CPU time does each process get? Which process do we run next? Possibilities Keep CPU till done Each process uses CPU a bit and passes it on Each process gets proportional to what they pay

Which CPU scheduling policy is the best? A. Processes keep CPU until done (maximize throughput) B. Processes use a fraction of CPU and pass it on (ensure fairness) C. Processes receive CPU in proportion to their priority or what they pay (prioritize importance) D. Other (explain)

There is No Single Best Policy Depends on the goals of the system Different for Your personal computer Large time-shared (super) computer Computer controlling a nuclear power plant Often have multiple (conflicting) goals

Common Policies Details beyond scope of this course (Take OS) Different classes of processes Those blessed by administrator (high/low priority) Everything else

Common Policies Special class gets special treatment (varies) Everything else: roughly equal time quantum Round robin Give priority boost to processes that frequently perform I/O Why? I/O bound processes frequently block. If we want them to get equal CPU time, we need to give them the CPU more often.

Linux s Policy (You re not responsible for this.) Special real time process classes (high prio) Other processes: Keep red-black BST of process, organized by how much CPU time they ve received. Pick the ready with process that has run for the shortest time thus far. Run it, update it s CPU usage time, add to tree. Interactive processes: Usually blocked, low total run time, high priority.

Managing Processes Processes created by calling fork() Spawning a new process Parent process spawns Child process Brutal relationship involving zombies, killing and reaping. (I m not making this up!) Processes interact with one another by sending signals.

Managing Processes Given a process, how do we make it execute the program we want? Model: fork() a new process, execute program

fork() System call (function provided by OS kernel) Creates a duplicate of the requesting process Process is cloning itself: CPU context Memory address space (Almost) identical clones OS Text Data Heap OS Text Data Heap OS Text Data Heap Stack Stack Stack

fork() return value The two processes are identical in every way, except for the return value of fork(). The child gets a return value of 0. The parent gets a return value of child s PID. pid_t pid = fork(); // both continue after call if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); } Which process executes next? Child? Parent? Some other process? Up to OS to decide. No guarantees. Don t rely on particular behavior!

How many hello s will be printed? fork(); printf( hello ); if (fork()) { printf( hello ); } fork(); printf( hello ); A.6 B.8 C.12 D.16 E.18

How many hello s will be printed? fork(); printf( hello ); if (fork()) { printf( hello ); } fork(); printf( hello );

Common fork() usage: Shell A shell is the program controlling your terminal (e.g., bash). It fork() s to create new processes, but we don t want a clone (another shell). We want the child to execute some other program: exec() family of functions.

exec() Family of functions (execl, execlp, execv, ). Replace the current process with a new one. Loads program from disk: Old process is overwritten in memory. Does not return unless error.

Common fork() usage: Shell 1.fork()child process. 2.exec()desired program to replace child s address space. 2.wait()for child process to terminate. 3. repeat The parent and child each do something different next.

Common fork() usage: Shell 1.fork()child process. Shell fork() Shell (p) Shell (c)

Common fork() usage: Shell 2. parent: wait()for child to finish Shell fork() Shell (p) Shell (c) wait()

Common fork() usage: Shell 2. child: exec()user-requested program Shell fork() wait() Shell (p) Shell (c) exec()

Common fork() usage: Shell 2. child: exec()user-requested program Shell fork() wait() Shell (p) exec() Shell new prog Runs to completion

Common fork() usage: Shell 3. child program terminates, cycle repeats Shell fork() wait() Shell (p) exec() Shell new prog Runs to completion Child terminates

Common fork() usage: Shell 3. child program terminates, cycle repeats Shell fork() wait() Shell (p) exec() Shell new prog Original parent shell resumes Shell (p) Runs to completion Child terminates

Process Termination When does a process die? It calls exit(int status); It returns (an int) from main It receives a termination signal (from the OS or another process) Key observation: the dying process produces status information. Who looks at this? The parent process!

Reaping Children (Bet you didn t expect to see THAT title on a slide when you signed up for CS 31?) wait(): parents reap their dead children Given info about why child died, exit status, etc. Two variants: wait(): wait for and reap next child to exit waitpid(): wait for and reap specific child This is how the shell determines whether or not the program you executed succeeded.

Common fork() usage: Shell 1. fork()child process. 2. exec()desired program to replace child s address space. 3. wait()for child process to terminate. Check child s result, notify user of errors. 4. repeat

What should happen if dead child processes are never reaped? (That is, the parent has not wait()ed on them?) A. The OS should remove them from the process table (process control block / PCB). B. The OS should leave them in the process table (process control block / PCB).

Zombie Processes Zombie: A process that has terminated but not been reaped by parent. (AKA defunct process) Does not respond to signals (can t be killed) OS keeps their entry in process table: Parent may still reap them, want to know status Don t want to re-use the process ID yet Basically, they re kept around for bookkeeping purposes, but that s much less exciting...

Signals How does a parent process know that a child has exited (and that it needs to call wait)? Signals: inter-process notification mechanism Info that a process (or OS) can send to a process. Please terminate yourself (SIGTERM) Stop NOW (SIGKILL) Your child has exited (SIGCHLD) You ve accessed an invalid memory address (SIGSEGV) Many more (SIGWINCH, SIGUSR1, SIGPIPE, )

Signal Handlers By default, processes react to signals according to the signal type: SIGKILL, SIGSEGV, (others): process terminates SIGCHLD, SIGUSR1: process ignores signal You can define signal handler functions that execute upon receiving a signal. Drop what program was doing, execute handler, go back to what it was doing. Example: got a SIGCHLD? Enter handler, call wait() Example: got a SIGUSR1? Reopen log files. Some signals (e.g., SIGKILL) cannot be handled.

Summary Processes cycled off and on CPU rapidly Mechanism: context switch Policy: CPU scheduling Processes created by fork()ing Other functions to manage processes: exec(): replace address space with new program exit(): terminate process wait(): reap child process, get status info Signals one mechanism to notify a process of something