Process Control. Philipp Koehn. 23 April 2018

Similar documents
CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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 Oct. 17, 2002

Giving credit where credit is due

Exceptional Control Flow Part I September 22, 2008

Exceptions and Processes

Operating System Structure

Exceptional Control Flow Part I

Exceptional Control Flow Part I

Exceptions, Processes and Signals

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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?

COSC243 Part 2: Operating Systems

Altering the Control Flow

Altering the Control Flow

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

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

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

Exceptional Control Flow: Exceptions and Processes

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

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

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

Processes. Dr. Yingwu Zhu

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

Programs. Program: Set of commands stored in a file Stored on disk Starting a program creates a process static Process: Program loaded in RAM dynamic

Processes, Exceptional

Lecture 24: Multitasking and Signals

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

ECE 650 Systems Programming & Engineering. Spring 2018

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

System Programming. Signals I

Processes and Exceptions

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Preview. Process Termination. wait and waitpid() System Call. wait and waitpid() System Call. waitpid() System Call 10/23/2018

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Exceptional Control Flow Part I Oct. 28, 2009"

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

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

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

Exceptional Control Flow: Exceptions and Processes

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

OS Interaction and Processes

Process Management forks, bombs, zombies, and daemons! Lecture 5, Hands-On Unix System Administration DeCal

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

Princeton University Computer Science 217: Introduction to Programming Systems Exceptions and Processes

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

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

CS 201. Processes. Gerson Robboy Portland State University

System Calls & Signals. CS449 Spring 2016

Killing Zombies, Working, Sleeping, and Spawning Children

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

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

CSC 252: Computer Organization Spring 2018: Lecture 19

CS 550 Operating Systems Spring Process III

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

Processes: Introduction. CS 241 February 13, 2012

Processes. CSE 351 Spring Instructor: Ruth Anderson

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

CS240: Programming in C

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Processes. CSE 351 Autumn Instructor: Justin Hsia

Exceptional Control Flow

Chapter 4 Multithreaded Programming

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

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Processes. CSE 351 Autumn Instructor: Justin Hsia

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

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

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture VII

fork System-Level Function

CS240: Programming in C

Lecture 4: Process Management

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

Signals. Joseph Cordina

University of Washington What is a process?

What is an Operating System? Signals, Processes, & Threads. Resource Sharing. Resource Abstraction ... Operating Systems 10/20/2010

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

Program and OS interac0ons: Excep0ons and Processes

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

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

Handling of Interrupts & Exceptions

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

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

* What are the different states for a task in an OS?

4/9/17. Memory sharing. Device sharing. Princeton University Computer Science 217: Introduction to Programming Systems.

Exceptions, Processes and Signals

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

Operating Systems 9/6/ SIGINT Terminate Interrupt from keyboard (ctl-c) 9 SIGKILL Terminate Kill program (cannot override or ignore)

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

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

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

Process Management 1

Transcription:

Process Control Philipp Koehn 23 April 2018

Control Flow 1 The CPU executes one instruction after another Typically, they are next to each other in memory (unless jumps, branches, and returns from subroutine) Exceptional Control Flow, triggered by hardware exception software exception

Exceptions 2 Interrupts signal from I/O device also: timer interrupts for multi-tasking Traps and system calls intentional triggered by instruction ("syscall") Faults maybe recoverable, e.g., swapped out memory ("page fault") if recovered, return to regular control flow Aborts unrecoverable fatal error, e.g., memory corrupted application process is terminated

Abrupt Change in Control Flow 3 execute instructions current next interrupt, finish current instruction, control passes to kernel handler returns to next instruction interrupt handler

4 processes

Process 5 Exceptions basic building block for processes Modern computers seem to run several things at once retrieve and display web pages play music in the background accept emails and alert you to them Process := a running program appears to have full access to memory appears to run without interruptions Multi-tasking: modern OS that allow multiple processes at once

Logical Control Flow 6 Process A Process B Process C time

User and Kernel Mode 7 Mode bit in control register Kernel mode: User mode: may execute any instruction, access any memory limited to private memory Switch from user to kernel mode voluntary (sleep) triggered by interrupt system call

Private Address Space 8 ffffffff Kernel memory User stack Stack pointer Memory-mapped region for shared libraries 400000 Run time heap (created by malloc) Read/write segment (.data /.bss) Read-only code segment (.init,.text.,.rodata) Loaded from executable 0

Process Context 9 Kernel maintains context for each process Context program counter register values address table (more on that next lecture) opened files various meta information (e.g., process name) In Linux, each process context viewable in /proc "file" system

Context Switches 10 Process A Process B Process C user kernel time user kernel user kernel user

11 system calls

Examples 12 Number Name Description 0 read read from file 1 write write to file 2 open open file 3 close close file 33 pause suspend process until signal arrives 39 getpid get process id 57 fork create new process 60 exit end process 61 wait4 wait for a process to terminate 62 kill kill another process

.section.data string:.ascii "hello, world!\n" string_end:.equ len, string_end - string Assembly Example 13.section.text.globl main main: movq $1, %rax ; write is system call 1 movq $1, %rdi ; arg1: stdout is "file" 1 movq string, %rsi ; arg2: hello world string movq len, %rdx ; arg3: length of string syscall movq $60, %rax ; exit is system call 60 movq $0; %rdi ; exit status syscall

System Call Control 14 execute instructions syscall next control passes to kernel handler returns to next instruction, provides return values syscall handler runs

15 process control

Creating New Processes 16 C code than spawns a child process int main() { int x = 1; pid_t pid = fork(); } if (pid == 0) { printf("child x=%d", ++x); exit(0); } printf("parent x=%d", --x); exit(0); When run, it returns parent x=0 child x=2

Syscall 57: Fork 17 fork() creates a child process Call once, return twice in child process: return value 0 in parent process: return value is process id of child Concurrent exception parent and child processes run concurrently no guarantee which proceeds first (and for how long) Duplicate by separate address space initially memory is identical each process makes changes to its private copy

Another Example 18 Multiple forks int main() { fork(); fork(); printf("hello\n"); exit(0); } Outputs "hello" 4 times printf exit fork printf exit printf exit main fork fork printf exit

Death in the Family 19 What happens when what dies when? Child process dies process still in kernel s process table waiting for parent to read exit status "zombie": dead, but still active Parent process dies children processes become orphaned orphan killing: terminate all orphaned processes re-parenting: make init process (pid: 1) parent ( a "daemon" process)

Waiting for Child to Die 20 1. Parent spawns child process 2. Both processes running 3. Parent waits for child to complete - C: waitpid() - Assembly: syscall 61 4. Parent stalls 5. Child dies (zombie) 6. Parent receives exit status of child 7. Child dies completely

Exec 21 Parent process may execute another program C: execve(filename, argv, envp) Assembly: syscall 59 Passes environment variables (envp) Executed command takes over If both should run: fork first

22 signals

Signals 23 Software-level communication between processes Sending the signal from one process Receiving the signal by another process ignore terminate catch signal Handled by kernel

Examples 24 Number Name Default Corresponding Event 1 SIGHUP terminate Terminate line hangup 2 SIGINT terminate Interrupt from keyboard 3 SIGUIT terminate quit from keyboard 4 SIGILL terminate illegal instruction 5 SIGTRAP terminate & dump core trace trap 9 SIGKILL terminate* kill process 18 SIGCONT ignore continue process if stopped 19 SIGSTOP stop until SIGCONT* stop signal not from terminal 20 SIGTSTP stop until SIGCONT stop signal from terminal * = SIGKILL and SIGSTOP cannot be caught

Sending Signals 25 From shell with command linux> /bin/kill -9 2423 From shell with keystroke to running process linux> start-my-process CTRL+C CTRL+C: sends SIGINT CTRL+Z: sends SIGTSTP There is also a C function and an Assembly syscall

Receiving Signals 26 When kernel about to continue process, checks for signals If there is a signal, forces process to receive signal Each signal has a default action ignore terminate terminate and dump core stop Process can also set up a signal handler for customized response

Signal Handler 27 Signal handler in C #include "csapp.h" void sigint_handler(int sig) { printf("caught SIGINT\n"); exit(0); } int main() { signal(sigint, sigint_handler); pause(); return 0; } Now, process writes "Caught SIGINT" to stdout before terminating