Advanced Unix/Linux System Program. Instructor: William W.Y. Hsu

Similar documents
CS631 - Advanced Programming in the UNIX Environment. Process Groups, Sessions, Signals

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

Advanced Unix Concepts. Satyajit Rai

Signals. Joseph Cordina

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

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

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. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Lecture 24: Multitasking and Signals

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

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

n Unconditionally kill process 8371 n Interactive interrupt key n See /usr/include/bits/signum.h (e.g., SIGKILL 9) 2 -Ken Wong, Sep 2008

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

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

CSE 421: Introduction to Operating Systems

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

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

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

System Programming. Signals I

Exceptions, Processes and Signals

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

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

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

CS240: Programming in C

Chapter 5. TCP Client-Server

CSCI 4061: Signals and Signal Handlers

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Kernel and processes

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

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

Exceptional Control Flow Part II

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

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.

So far, we know how to create processes. How do we communicate with them? Primary mechanism: signals

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

System Calls & Signals. CS449 Spring 2016

Giving credit where credit is due

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

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

CSE410 Operating Systems Spring 2018 Project 1: Introduction to Unix/Linux Signals

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

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

PVPSIDDHARTHA INSTITUTE OF TECHNOLOGY

Process Control. Philipp Koehn. 23 April 2018

Workshop on Inter Process Communication Solutions

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

System Programming. Signals II

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

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

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Unix Processes. What is a Process?

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

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

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Signals and Session Management. Signals. Mechanism to notify processes of system events

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

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

Linux System Administration

Sperimentazioni I LINUX commands tutorial - Part II

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

Exceptional Control Flow Part II Nov. 2, 2009"

UNIX System Programming. Overview. 2. Signal Types (31 in POSIX) Signal Sources. Signals. 1. Definition

Operating Systems 2010/2011

Preview. The pause() System Call. The pause() System Call. The signal() System Call 10/18/2017

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

System Programming. Introduction to Unix

APPLIED INFORMATICS Processes. Bash characteristics. Command type. Aliases.

Embedded Systems Programming

bash, part 3 Chris GauthierDickey

Lecture 4. Introduction to Unix Processes. Introduction to Systems Programming: Processes and Signals

Bamuengine.com. Chapter 7. The Process

ECE 650 Systems Programming & Engineering. Spring 2018

Interprocess Communication

Unix System Programming - Chapter 8

CS 33. Signals Part 1. CS33 Intro to Computer Systems XXII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Chapter 4 Controlling Processes

Acontecimentos assíncronos (POSIX signals) Sincronização com múltiplos acontecimentos

Signals and Signal Handling - Part 2

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Other Interprocess communication (Chapter 2.3.8, Tanenbaum)

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

ESE 333 Real-Time Operating Systems 2 What is an operating system? Two views: 1. Top-down view: Extended machine ffl Covers the details of the hardwar

IC221: Systems Programming 12-Week Written Exam [SOLUTIONS]

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

Signal types (some of them) Networks and Operating Systems ( ) Chapter 5: Memory Management. Where do signals come from?

Last Class. System Calls, Kernel Mode, and Process Implementation. Processes. Today. Operating Systems 9/11/2018 CSC 256/456 1

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

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

Chapter 9: Process management. Chapter 9 Process management

Engineering Robust Server Software

Operating System Structure

CS 33. Signals Part 2. CS33 Intro to Computer Systems XXIV 1 Copyright 2016 Thomas W. Doeppner. All rights reserved.

Processes. System tasks Campus-Booster ID : **XXXXX. Copyright SUPINFO. All rights reserved

Transcription:

Advanced Unix/Linux System Program Instructor: William W.Y. Hsu

CONTENTS Process Groups Sessions Signals 5/10/2018 INTRODUCTION TO COMPETITIVE PROGRAMMING 2

Login process 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 3

Login process init(8) reads /etc/ttys getty(8) opens terminal prints login: reads username login(1) getpass(3), encrypt, compare to getpwnam(3) register login in system databases read/display various files initgroups(3)/setgid(2), initialize environment chdir(2) to new home directory chown(2) terminal device setuid(2) to user s uid, exec(3) shell 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 4

Login process init(8) # PID 1, PPID 0, EUID 0 getty(8) # PID N, PPID 1, EUID 0 login(1) # PID N, PPID 1, EUID 0 $SHELL # PID N, PPID 1, EUID U ls(1) # PID M, PPID N, EUID U $ pstree -hapun more $ ps axfju 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 5

What about network logins? telnet (ssh) 140.121.197.19 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 6

Network login The main difference between a serial terminal login and a network login is that the connection between the terminal and the computer isn t point-to-point. In this case, login is simply a service available, just like any other network service, such as FTP or SMTP. With the terminal logins, init knows which terminal devices are enabled for logins and spawns a getty process for each device. In the case of network logins, however, all the logins come through the kernel s network interface drivers (e.g., the Ethernet driver), and we don t know ahead of time how many of these will occur. Instead of having a process waiting for each possible login, we now have to wait for a network connection request to arrive. To allow the same software to process logins over both terminal logins and network logins, a software driver called a pseudo terminal is used to emulate the behavior of a serial terminal and map terminal operations to network operations, and vice versa. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 7

Network login 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 8

Network login In BSD, the inetd (xinetd in Linux) process, sometimes called the Internet superserver, waits for most network connections. Then, the telnetd process then opens a pseudo terminal device and splits into two processes using fork, which do the following: The parent (telnetd) handles the communication across the network connection. The child execs the login program. The parent and the child are connected through the pseudo terminal. Before doing the exec, the child sets up file descriptors 0, 1, and 2 to the pseudo terminal. If we log in correctly, login performs the same steps previously. It changes to our home directory and sets our group IDs, user ID, and our initial environment. Then login replaces itself with our login shell by calling exec. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 9

Process groups init login shell $ proc1 proc2 & [1] 10306 $ proc3 proc4 proc5 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 10

Process groups #include <unistd.h> pid t getpgrp(void); pid t getpgid(pid t pid); Returns: process group ID if OK, -1 otherwise In addition to having a PID, each process also belongs to a process group (collection of processes assocaited with the same job / terminal). Each process group has a unique process group ID. Process group IDs (like PIDs) are positive integers and can be stored in a pid_t data type. Each process group can have a process group leader leader identified by its process group ID == PID. Leader can create a new process group, create processes in the group. A process can set its (or its children s) process group using setpgid(2). 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 11

Process groups and sessions #include <unistd.h> pid t setsid(void); Returns: process group ID if OK, -1 otherwise A session is a collection of one or more process groups. If the calling process is not a process group leader, this function creates a new session. Three things happen: The process becomes the session leader of this new session. The process becomes the process group leader of a new process group. The process has no controlling terminal. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 12

Process groups and sessions init login shell $ proc1 proc2 & [1] 10306 $ proc3 proc4 proc5 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 13

Controlling terminal A session can have a single controlling terminal. This is usually the terminal device (in the case of a terminal login) or pseudo terminal device (in the case of a network login) on which we log in. The session leader that establishes the connection to the controlling terminal is called the controlling process. The process groups within a session can be divided into a single foreground process group and one or more background process groups. If a session has a controlling terminal, it has a single foreground process group and all other process groups in the session are background process groups. Whenever we press the terminal s interrupt key (often DELETE or Control-C), the interrupt signal is sent to all processes in the foreground process group. Whenever we press the terminal s quit key (often Control-backslash), the quit signal is sent to all processes in the foreground process group. If a modem (or network) disconnect is detected by the terminal interface, the hang-up signal is sent to the controlling process (the session leader). 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 14

Process groups and sessions $ ps -o pid,ppid,pgid,sid,comm./cat1./cat2 PID PPID PGRP SESS COMMAND 1989 949 7736 949 ps 1990 949 7736 949 cat1 1988 949 7736 949 cat2 949 21401 949 949 ksh 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 15

Job control Job control allows us to start multiple jobs (groups of processes) from a single terminal and to control which jobs can access the terminal and which jobs are run in the background. Job control requires three forms of support: A shell that supports job control. The terminal driver in the kernel must support job control. The kernel must support certain job-control signals. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 16

Job control $ make all > Make.out & [1] 1475 $ pr *.c lpr & [2] 1490 $ # just press RETURN [2] + Done pr *.c lpr & [1] + Done make all > Make.out & 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 17

Job control The make is job number 1 and the starting process ID is 1475. The next pipeline is job number 2 and the process ID of the first process is 1490. When the jobs are done and we press RETURN, the shell tells us that the jobs are complete. The interaction with the terminal driver arises because a special terminal character affects the foreground job. The terminal driver looks for three special characters, which generate signals to (all processes in ) the foreground process group: SIGINT: generated by the interrupt character (typically DELETE or Control-C). SIGQUIT: generated by the quit character (typically Controlbackslash). SIGTSTP: generated by the suspend character (typically Control-Z). 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 18

Job control While we can have a foreground job and one or more background jobs, only the foreground job receives terminal input (the characters that we enter at the terminal). It is not an error for a background job to try to read from the terminal, but the terminal driver detects this and sends a special signal to the background job: SIGTTIN. This signal normally stops the background job; 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 19

Job control cat > temp.foo & standard input # start in background, but it ll read from [1] 1681 $ # we press RETURN [1] + Stopped (SIGTTIN) cat > temp.foo & $ fg %1 # bring job number 1 into the foreground cat > temp.foo foreground hello, world ˆD # the shell tells us which job is now in the # enter one line # type the end-of-file character $ cat temp.foo # check that the one line was put into the file hello, world 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 20

Job control SIGTTIN: When the background cat tries to read its standard input (the controlling terminal), the terminal driver, knowing that it is a background job, sends the SIGTTIN signal to the background job. The shell detects the change in status of its child (wait and waitpid function) and tells us that the job has been stopped. The shell s fg command move the stopped job into the foreground, which causes the shell to place the job into the foreground process group (tcsetpgrp) and send the continue signal (SIGCONT) to the process group. Since it is now in the foreground process group, the job can read from the controlling terminal. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 21

Job control There is an option that we can allow or disallow a background job to send its output to the controlling terminal. Normally, we use the stty(1) command to change this option. When we disallow background jobs from writing to the controlling terminal, cat will block when it tries to write to its standard output, because the terminal driver identifies the write as coming from a background process and sends the job the SIGTTOU signal. When we use the shell s fg command to bring the job into the foreground, the job completes. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 22

Job control $ cat temp.foo & # execute in background [1] 1719 $ hello, world # the output from the background job appears after the prompt we press RETURN [1] + Done cat temp.foo & $ stty tostop # disable ability of background jobs to output to controlling terminal $ cat temp.foo & # try it again in the background [1] 1721 $ # we press RETURN and find the job is stopped [1] + Stopped(SIGTTOU) cat temp.foo & $ fg %1 # resume stopped job in the foreground cat temp.foo foreground hello, world # the shell tells us which job is now in the # and here is its output 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 23

Job control 4 3 1 2 7 6 5 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 24

Job control $ ps -o pid,ppid,pgid,sid,comm PID PPID PGRP SESS COMMAND 24251 24250 24251 24251 ksh 24620 24251 24620 24251 ps $ echo $? 0 $ 2 1 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 25

Job control 3 $ /bin/sleep 30 & [1] 24748 $ ps -o pid,ppid,pgid,sid,comm PID PPID PGRP SESS COMMAND 24251 24250 24251 24251 ksh 24748 24251 24748 24251 sleep 24750 24251 24750 24251 ps $ [1] + Done /bin/sleep 30 & $ 4 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 26

Job control 5 $ cat >file Input from terminal, Output to terminal. ^D $ cat file Input from terminal, Output to terminal. $ cat >/dev/null Input from terminal, Output to /dev/null. Waiting forever... Or until we send an interrupt signal. ^C $ 5 5 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 27

Job control 6 $ cat file & [1] 2056 $ Input from terminal, Output to terminal. [1] + Done cat file & $ stty tostop $ cat file & [1] 4655 7 $ [1] + Stopped(SIGTTOU) cat file & $ fg cat file Input from terminal, Output to terminal. $ 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 28

Orphaned process groups A process whose parent terminates is called an orphan and is inherited by the init process. The entire process groups that can be orphaned and this section discusses how POSIX.1 handles this situation. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 29

Orphaned process groups init -> login login -> fork exec parent fork child parent terminates 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 30

Signals 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 31

Signal concepts Signals are a way for a process to be notified of asynchronous events. Some examples: A timer you set has gone off (SIGALRM) Some I/O you requested has occurred (SIGIO) A user resized the terminal window (SIGWINCH) A user disconnected from the system (SIGHUP)... See also: signal(2)/signal(3)/signal(7) (note: these man pages vary significantly across platforms!) 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 32

Signal concepts Besides the asynchronous events listed previously, there are many ways to generate a signal: Terminal generated signals (user presses a key combination which causes the terminal driver to generate a signal). Hardware exceptions (divide by 0, invalid memory references, etc). kill(1) allows a user to send any signal to any process (if the user is the owner or superuser). kill(2) (a system call, not the unix command) performs the same task. Software conditions (other side of a pipe no longer exists, urgent data has arrived on a network file descriptor, etc.) 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 33

kill(2) and raise(3) #include <sys/types.h> #include <signal.h> int kill(pid t pid, int signo); int raise(int signo); id > 0 signal is sent to the process whose PID is pid. pid == 0 signal is sent to all processes whose process group ID equals the process group ID of the sender. pid == -1 POSIX.1 leaves this undefined, BSD defines it (see kill(2)). 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 34

Signal concepts Once we get a signal, we can do one of several things: Ignore it. (note: there are some signals which we CANNOT or SHOULD NOT ignore) Catch it. That is, have the kernel call a function which we define whenever the signal occurs. Accept the default. Have the kernel do whatever is defined as the default action for this signal 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 35

Signal concepts $ cc -Wall../01-intro/simple-shell.c $./a.out $$ ^C $ echo $? 130 $ cc -Wall../01-intro/simple-shell2.c $./a.out $$ ^C Caught SIGINT! $$ 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 36

signal(3) #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); Returns: previous disposition of signal if OK, SIG ERR otherwise func can be: SIG_IGN which requests that we ignore the signal signo. SIG_DFL which requests that we accept the default action for signal signo. Or the address of a function which should catch or handle a signal. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 37

Signal examples $ cc -Wall siguser.c $./a.out ^Z $ bg $ ps grep a.ou[t] 11106 ttys002 0:00.00./a.out $ kill -USR1 11106 received SIGUSR1 $ kill -USR2 11106 received SIGUSR2 $ kill -INT 11106 $ [2]- Interrupt./a.out $ 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 38

Program startup When a program is execed, the status of all signals is either default or ignore. When a process fork(2)s, the child inherits the parent s signal dispositions. A limitation of the signal(3) function is that we can only determine the current disposition of a signal by changing the disposition. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 39

About that timeout(1) then... $ timeout 60 /bin/sh -c "ls more" vs $ /bin/sh -c "timeout 60 /bin/sh -c \"ls more\"" 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 40

About that timeout(1) then... $ /bin/sh -c timeout 60 "/bin/sh -c \"ls more\"" $ pstree -hapun [...] -ksh,10981 -sh,12434 -c timeout 60 /bin/sh -c "ls more" -timeout,12435 60 /bin/sh -c ls more -sh,12436 -c ls more -ls,12437 -more,12438 [...] $ ps x -o pid,ppid,pgid,sid,tpgid,stat,comm egrep -v "(ssh ps egrep)" PID PPID PGID SESS TPGID STAT COMMAND 7676 7675 7676 7676 7676 Ss+ ksh 10981 10980 10981 10981 12434 Ss ksh 12434 10981 12434 10981 12434 S+ sh 12435 12434 12435 10981 12434 S timeout 12436 12435 12435 10981 12434 T sh 12437 12436 12435 10981 12434 T ls 12438 12436 12435 10981 12434 T more 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 41

About that timeout(1) then... Use the source, Luke! coreutils/src/timeout.c /* Ensure we re in our own group so all subprocesses can be killed. Note we don t just put the child in a separate group as then we would need to worry about foreground and background groups and propagating signals between them. */ if (!foreground) setpgid (0, 0); [...] signal (SIGTTIN, SIG_DFL); signal (SIGTTOU, SIG_DFL); execvp (argv[0], argv); 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 42

About that timeout(1) then... util-linux/text-utils/more.c #define stty(fd,argp) tcsetattr(fd,tcsanow,argp) if (!no_tty) { signal(sigquit, onquit); signal(sigint, end_it); #ifdef SIGWINCH signal(sigwinch, chgwinsz); #endif /* SIGWINCH */ if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) { signal(sigtstp, onsusp); catch_susp++; } stty (fileno(stderr), &otty); 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 43

About that timeout(1) then... https://is.gd/x95efp https://is.gd/rqn03r https://is.gd/gyye32 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 44

sigaction(2) #include <signal.h> int sigaction(int signo, const struct sigaction *act, struct sigaction *oact); This function allows us to examine or modify the action associated with a particular signal. struct sigaction { }; void (*sa_handler)(); /* addr of signal handler, or SIG_IGN or SIG_DFL */ sigset_t sa_mask; /* additional signals to block */ int sa_flags; /* signal options */ signal(3) is (nowadays) commonly implemented via sigaction(2). 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 45

More advanced signal handling via signal sets int sigemptyset(sigset t *set) intialize a signal set to be empty int sigfillset(sigset t *set) initialize a signal set to contain all signals int sigaddset(sigset t *set, int signo) int sigdelset(sigset t *set, int signo) int sigismember(sigset t *set, int signo) 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 46

Resetting signal handlers Note: on some systems, invocation of the handler resets the disposition to SIG_DFL! $ cc -DSLEEP=3 -Wall pending.c $./a.out => Establishing initial signal hander via signal(3). ^\sig_quit: caught SIGQUIT (1), now sleeping sig_quit: exiting (1) => Time for a second interruption. ^\sig_quit: caught SIGQUIT (2), now sleeping sig_quit: exiting (2) => Establishing a resetting signal hander via signal(3). ^\sig_quit_reset: caught SIGQUIT (3), sleeping and resetting. sig_quit_reset: restored SIGQUIT handler to default. => Time for a second interruption. ^\Quit: 3 $ 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 47

Signal queuing Signals arriving while a handler runs are queued. $./a.out >/dev/null ^\sig_quit: caught SIGQUIT (1), now sleeping ^\^\^\^\^\^\sig_quit: exiting (1) sig_quit: caught SIGQUIT (2), now sleeping ^\^\^\^\sig_quit: exiting (2) sig_quit: caught SIGQUIT (3), now sleeping ^\sig_quit: exiting (3) sig_quit: caught SIGQUIT (4), now sleeping sig_quit: exiting (4) [...] (Note that simultaneously delivered signals may be merged into one.) 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 48

Signal queuing Signals arriving while a handler runs are queued. Unless they are blocked. $./a.out [...] => Establishing a resetting signal hander via signal(3). ^\sig_quit_reset: caught SIGQUIT (1), sleeping and resetting. sig_quit_reset: restored SIGQUIT handler to default. => Time for a second interruption. => Blocking delivery of SIGQUIT... => Now going to sleep for 3 seconds... ^\ => Checking if any signals are pending... => Checking if pending signals might be SIGQUIT... Pending SIGQUIT found. => Unblocking SIGQUIT... Quit: 3 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 49

Signal queuing Multiple identical signals are queued, but you can receive a different signal while in a signal handler. $./a.out >/dev/null ^\sig_quit: caught SIGQUIT (1), now sleeping ^\^\^\^\^Csig_int: caught SIGINT (2), returning immediately sig_quit: exiting (2) sig_quit: caught SIGQUIT (3), now sleeping ^\^\sig_quit: exiting (3) sig_quit: caught SIGQUIT (4), now sleeping sig_quit: exiting (4) [...] 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 50

Interrupted system calls Some system calls can block for long periods of time (or forever). These include things like: read(2)s from files that can block (pipes, networks, terminals) write(2) to the same sort of files open(2) of a device that waits until a condition occurs (for example, a modem) pause(3), which purposefully puts a process to sleep until a signal occurs Certain ioctl(3)s Certain IPC functions Catching a signal during execution of one of these calls traditionally led to the process being aborted with an errno return of EINTR. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 51

Interrupted system calls Previously necessary code to handle EINTR: again: if ((n = read(fd, buf, BUFFSIZE)) < 0) { if (errno == EINTR) */ goto again; /* just an interrupted system call } /* handle other errors */ Nowadays, many Unix implementations automatically restart certain system calls. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 52

Interrupted system calls $ cc -Wall eintr.c $./a.out ^C read call was interrupted $./a.out ^\a read call was restarted a $ 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 53

Reentrant functions An example of calling nonreentrant functions from a signal handler: $ cc -Wall reentrant.c;./a.out in signal handler in signal handler in signal handler no root found! $./a.out in signal handler return value corrupted: pw_name = root $./a.out in signal handler in signal handler User jschauma not found! $./a.out in signal handler in signal handler Memory fault (core dumped) 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 54

Reentrant functions If your process is currently handling a signal, what functions are you allowed to use? See p. 306 in Stevens for a list. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 55

Homework Read: Controlling Terminals: tty(4), termios(4) Read, try, play with and understand all examples. 5/10/2018 ADVANCED UNIX/LINUX SYSTEM PROGRAMMING 56