CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

Process Control. Philipp Koehn. 23 April 2018

Operating System Structure

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

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

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

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

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

Exceptional Control Flow Part I

Computer Systems Assignment 2: Fork and Threads Package

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

CSC 1600 Unix Processes. Goals of This Lecture

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

CMPSCI 230 Computer Systems Principles. Processes

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

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

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

System Calls & Signals. CS449 Spring 2016

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

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Lecture 24: Multitasking and Signals

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

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

518 Lecture Notes Week 3

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Altering the Control Flow

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

Kernel and processes

Exceptional Control Flow Part II

Exceptional Control Flow Part I

Exceptional Control Flow: Exceptions and Processes

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

Giving credit where credit is due

ECE 650 Systems Programming & Engineering. Spring 2018

CS240: Programming in C

Altering the Control Flow

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

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

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

Exceptional Control Flow Part II Nov. 2, 2009"

Operating Systemss and Multicore Programming (1DT089)

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

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

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

System Programming. Signals I

Exceptional Control Flow Part I Oct. 17, 2002

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Sciences and Engineering

Processes. Johan Montelius KTH

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

OS Interaction and Processes

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

A process. the stack

System Programming. Process Control II

Exceptional Control Flow Part I September 22, 2008

Giving credit where credit is due

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

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

CSC 252: Computer Organization Spring 2018: Lecture 19

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

CS 201. Processes. Gerson Robboy Portland State University

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

Advanced Unix Concepts. Satyajit Rai

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?

Processes: Introduction. CS 241 February 13, 2012

Processes, Exceptional

CSCI0330 Intro Computer Systems Doeppner. Project Shell 2. Due: November 8, 2017 at 11:59pm. 1 Introduction 2

A read or write being atomic means that its effect is as if it happens instantaneously.

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.

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

Signals. POSIX defines a variety of signal types, each for a particular

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

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Lecture 4 Threads. (chapter 4)

Exceptions and Processes

Introduction to Processes

University of Washington What is a process?

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

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

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

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

Operating systems and concurrency - B03

Unix System Programming - Chapter 8

Fall 2015 COMP Operating Systems. Lab #3

CS240: Programming in C

Processes, Signals, I/O, Shell Lab : Introduc>on to Computer Systems Recita>on 9: Monday, Oct. 21, 2013 Marjorie Carlson Sec>on A

Lesson 2. process id = 1000 text data i = 5 pid = 1200

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Exceptions, Processes and Signals

Shell Execution of Programs. Process Groups, Session and Signals 1

CMPSC 311- Introduction to Systems Programming Module: Concurrency

HW 1: Shell. Contents CS 162. Due: September 18, Getting started 2. 2 Add support for cd and pwd 2. 3 Program execution 2. 4 Path resolution 3

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

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

3. Process Management in xv6

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

Processes. Processes (cont d)

Transcription:

CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 19

LAST TIME Introduced UNIX signals A kernel facility that provides user-mode exceptional control flow Allows many hardware-level exceptions to be exposed to application processes Timer events (SIGALRM), invalid memory access (SIGSEGV), illegal instruction execution (SIGILL), etc. Also allows processes to send signals to each other e.g. terminal sends SIGINT (Ctrl-C) to your program e.g. kernel can send SIGKILL to a runaway process 2

REENTRANT FUNCTIONS A signal handler can interrupt any other code in the program including function calls that are in progress! Signal handlers must only use reentrant functions Functions that can be invoked multiple times concurrently, without causing errors i.e. multiple overlapping logical flows through the function Frequently, code in a signal handler will interrupt code in the main program that is using the exact same functions Example: malloc() is not reentrant! Updates large, complex data structures within the heap Two calls to malloc() can easily stomp on each other! Must not use malloc() within a signal handler! (Or, any other function that calls malloc()!) 3

REENTRANT FUNCTIONS AND STATE A reentrant function must not manage state across multiple invocations of the function Concurrent invocations will cause state to be corrupted No global state, and no static local variables! Example: void do_task() { /* Variable remains across function calls! */ static int count = 0; }... /* Do some important task. */ count++; if (count > 100) { count = 0; periodic_cleanup(); } Static local variables are retained across multiple function calls (like a global variable visible only within the function) 4

REENTRANT FUNCTIONS AND STATE (2) Concurrent invocations of do_task() will not update count properly! void do_task() { /* Variable remains across function calls! */ static int count = 0; }... /* Do some important task. */ count++; if (count > 100) { count = 0; periodic_cleanup(); } Logical Control Flow Read count into register ( interrupted ) Add 1 to register Write reg back to count Signal Handler Read count into register Add 1 to register Write reg back to count Not reentrant; must not be called from a signal handler 5

REENTRANT FUNCTIONS AND MUTEXES Can t fix this problem with a mutex or semaphore First function invocation locks the mutex before accessing the persistent state, but is then interrupted but it still holds the lock! A second, concurrent function invocation interrupts the first one Can t acquire the lock because it s already held! Second invocation will be blocked by the first one! Entire system will grind to a halt. Whee. Locks can be used in a non-blocking manner e.g. try to acquire the lock, but return immediately with a failure code if someone else holds the lock (May limit the usefulness of your signal handler ) 6

GUARANTEED REENTRANT FUNCTIONS A signal handler can only use reentrant functions Ideally, the handler will also be reentrant Particularly in cases where one handler handles multiple signals Specific UNIX functions are guaranteed to be reentrant by the standard. For example: alarm(), pause(), signal() mkdir(), chdir(), rmdir(), chmod(), chown() open(), close(), read(), write() fork(), execve(), exit(), kill(), wait() Many others are not guaranteed to be reentrant e.g. malloc(), free(), printf(), etc. Must avoid using these functions in signal handlers! 7

USING NON-REENTRANT FUNCTIONS If non-reentrant functions will never be called concurrently, can use them in signal handlers but it s extremely difficult to guarantee this, especially in context of maintenance and upgrades! Don t tempt fate! J Program defensively. 8

SIGNAL HANDLER OPERATIONS Usually, handlers perform very simple operations Avoids the dangers of non-reentrant code! Set a flag to record that signal occurred, then return Main loop checks the flag every iteration When flag becomes set, main loop handles the signal then Main loop code can do whatever it wants, since signal handler won t be doing anything complicated Or, simply restart running the program at an appropriate location e.g. an error signal handler can restart a server at the beginning of the program Use something like setjmp() and longjmp() but there s a caveat 9

PENDING SIGNALS For each process, the kernel manages two bookkeeping variables for signal handling pending a bit-vector of signals that are currently pending for the process These signals have been sent to the process, but haven t yet been received or handled by the process Each kind of signal has one bit assigned to it Multiple signals of a particular type sent to a process will not necessarily all be received! If a particular type of signal is already pending, and then is sent again, the second signal is dropped! The pending flag for that signal is already set, so it can t be sent again 10

BLOCKED SIGNALS The kernel also keeps a blocked bit-vector for each process Again, each type of signal has a bit assigned to it If a particular type of signal is blocked then it won t be delivered to the process Can have a signal that is both pending and blocked When the kernel calls a signal handler on a process, that type of signal is automatically blocked Generally, signal handlers don t need to worry about being interrupted by the same kind of signal again Example: a process with a SIGINT handler First SIGINT received causes SIGINT handler to be called Also causes SIGINT to become blocked for the process! If another SIGINT occurs during handler execution, it is recorded in pending bit-vector, but it is not delivered! 11

BLOCKED SIGNALS (2) When a signal handler returns, the blocked signal-type is automatically unblocked When handler returns, signals of that type can begin being delivered again In the case of a blocked pending SIGINT, it will subsequently be delivered to the process Several functions for manipulating these signal bit-vectors int sigpending(sigset_t *set) Returns current set of pending signals for the process int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) Manipulates the set of blocked signals for the process Several other functions too! See CS:APP 8.5.6 12

SIGNALS AND setjmp()/longjmp() When a signal handler is called, that signal type is automatically blocked for the process When the handler returns, the signal type is unblocked again What happens if we longjmp() from inside a signal handler? setjmp() records: Execution state and registers, specifically the caller s instruction pointer and the stack pointer Does not record any other data beyond this! If we longjmp() from signal handler, it will not unblock the blocked signal 13

SIGNALS AND setjmp()/longjmp() (2) Example code: static jmp_buf restart_env; void restart_handler(int sig) { /* Just restart the program! */ longjmp(restart_env, 1); } This code only handles one SIGHUP! Signal is blocked when first SIGHUP handled, and is never delivered again. int main(int argc, char **argv) { signal(sighup, restart_handler); /* SIGHUP restarts */ if (setjmp(restart_env)) /* Returns 1 if restarting! */ fprintf(stderr, "Signal received, restarting!"); } load_config(...); while (!quit) {... /* Handle incoming requests, or something. */ } 14

sigsetjmp() AND siglongjmp() When using long-jumps from signal handlers, must use sigsetjmp() and siglongjmp() Nearly identical to setjmp() and longjmp() sigsetjmp() also records blocked-signal mask int sigsetjmp(sigjmp_buf env, int savesigs) If savesigs is true, blocked-signal state is saved siglongjmp() restores the blocked-signal mask void siglongjmp(sigjmp_buf env, int val) If corresponding sigsetjmp() set savesigs to true, then blocked-signal state is also restored here 15

SIGNALS AND LONG-JUMPS In general, long-jumps from signal handlers are useful in a very limited set of circumstances Must ensure that it s actually safe to never return to the code that was interrupted! e.g. don t leave files in a corrupted state; don t interrupt malloc() or other non-atomic operations Setting a flag is generally the safest approach Signal handler sets a flag and then returns Main loop checks the flag regularly, and responds accordingly Can only long-jump from a signal handler in certain circumstances e.g. when interrupted operation is atomic, or when signal indicates that normal operation was aborted 16

SUMMARY: SIGNALS Signals share many common traits with hardware exception handling A user-mode version of hardware exceptions Signal handlers must be aware of reentrancy issues, just like hardware exception handlers When a signal handler is invoked, that signal type is blocked until the handler returns Very similar to hardware interrupts and eflags register Signals allow us to leverage exceptional control flow in user-mode programs Enables powerful techniques in server programming Are used in most widely-used server programs Web-servers, email servers, DNS servers, databases, etc. 17

THE UNIX PROCESS MODEL UNIX has a simple but powerful process model Every process has a context Kernel must be able to uniquely identify the context of each running process Each process has a unique process ID (PID) pid_t getpid() returns the caller s process ID pid_t is simply an integer Every process is started by some other process All processes form a hierarchy Each process also has a parent process ID pid_t getppid() returns PID of the parent process Parent process must be a running process (More on this in a moment ) 18

THE UNIX init PROCESS Process 1 is the init process The ancestor of all processes on the UNIX system Started as the last step of kernel boot sequence Responsible for starting various sets of processes to support different operating system runlevels Each runlevel represents a set of services or capabilities provided by the system Use init to switch runlevels: init runlevel Runlevel 0 tells the system to shutdown Runlevel 1 is single-user mode Runlevel 2 is multi-user mode with limited networking Runlevel 3 is full multi-user mode Runlevel 5 starts X11 server for graphical logins Runlevel 6 tells the system to restart 19

THE UNIX init PROCESS (2) Most UNIX-like operating systems have a process akin to init The init program is no longer widely used...but there are many replacements, which serve the exact same purposes MacOSX has launchd Linux uses systemd 20

PROCESS STATES Generally, processes are in one of these states: Running The process is currently executing on the CPU, or is waiting to be executed Stopped The process is suspended (e.g. by Ctrl-Z from keyboard), and will not be scheduled for execution until it is resumed A process enters this state when it receives SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signals SIGSTOP stop signal not from the terminal (keyboard) SIGTSTP stop signal from terminal (i.e. Ctrl-Z from keyboard) SIGTTIN background process tries to read from terminal SIGTTOU background process tries to write to terminal When process is resumed, changes back to Running state 21

PROCESS STATES (2) Terminated The process stops executing permanently Occurs when: The process calls exit() or returns from main() The process receives a signal whose default action is to terminate A process can terminate by calling exit() void exit(int status) (Or, return an integer value from main() function) The exit status of the process is recorded for retrieval by other processes exit() supports exit-handlers Functions to perform tasks at process-termination int atexit(void (*function)(void)) registers an exit-handler for the process 22

PROCESS GROUPS Every process also belongs to a process group Another pid_t value associated with each process Signals can be sent to entire groups of processes int killpg(pid_t pgrp, int sig); Every process group has a leader The leader s PID is equal to the group s PID pid_t getpgid(pid_t pid) Reports the process-group ID of the specified process pid_t getpgrp() Reports the process-group ID of the calling process int setpgid(pid_t pid, pid_t pgid) Sets the process-group ID of the specified process pid_t setpgrp() Sets the caller s process-group ID to its own PID 23

STARTING A PROCESS A process can start a child process with fork() pid_t fork() fork() is called once, but returns twice! In parent process, return-value of fork() is PID of newly spawned child process No other way for the parent to find out the child s PID In child process, return-value of fork() is 0 Common usage pattern: pid_t child; child = fork(); if (child == 0) {... /* Do child-process stuff. */ } else {... /* Do parent-process stuff. */ } 24

STARTING A PROCESS (2) The child process is an identical duplicate of the parent process Main difference is that child process has a different process ID, and a different parent-process ID Child is in same process-group as the parent process Parent and child processes have identical registers, memory, and stack contents Separate copies, but identical contents Address-spaces of both processes are identical Kernel provides illusion of exclusive access to memory Parent and child processes execute concurrently Kernel also provides illusion of exclusive access to CPU Logical control flows of parent and child proceed separately Execution is interleaved as determined by the kernel 25

STARTING A PROCESS (3) Parent and child processes also have the same open files! Child can also read/write any files that parent had open when the process was forked e.g. standard input, standard output Very important behavior used to set up pipes between processes for inter-process communication 26

SIMPLE fork() EXAMPLE #include <unistd.h> #include <stdio.h> int main() { pid_t child_pid; int x = 1; child_pid = fork(); if (child_pid == 0) { /* Child code. */ x++; printf("hello from child! x = %d\n", x); } else { /* Parent code. */ x--; printf("hello from parent! x = %d\n", x); } } return 0; 27

SIMPLE fork() EXAMPLE (2) Both parent and child process print to stdout Compile and run the program: [user@host:~]> gcc Wall o procs procs.c [user@host:~]>./procs Hello from parent! x = 0 Hello from child! x = 2 [user@host:~]> Output from both processes is sent to stdout Parent s stdout is sent to the console When child is spawned, stdout from parent is copied Both processes have identical memory contents Both have variable x at the same memory address Child sees x = 1, since x was 1 in parent process 28

ZOMBIES!!! A child process doesn t immediately go away when it terminates Child process terminates with some status value Parent process may need to find out the child s status A terminated child process is called a zombie The process is dead, but it hasn t yet been reaped (Seriously. That s the terminology.) Parent processes reap zombie children by calling: pid_t wait(int *status) Waits for some child process to terminate pid_t waitpid(pid_t pid, int *status, int options) Waits for a specific child process to terminate Can also wait on children in a process-group, or all children Both report an error if calling process has no children 29

REAPING ZOMBIE CHILDREN Status value includes several important details Did the child process terminate normally? i.e. via a call to exit() or a return from main() Did the child process terminate because of a signal? e.g. SIGINT or SIGKILL Is the child process actually stopped (suspended) instead of being terminated? Several macros to extract these details WIFEXITED(status) returns true if child exited normally WEXITSTATUS(status) returns the actual exit status WIFSIGNALED(status) returns true if the child process was terminated by a signal WTERMSIG(status) returns signal number that terminated it WIFSTOPPED(status) returns true if process was stopped instead of being terminated WSTOPSIG(status) returns signal number that stopped it 30

NEXT TIME Continue exploring the UNIX process API 31