cs 3214 computer systemscs3214/spring2020/lectures/lecture-intro-… · cs 3214 spring 2020...

91
CS 3214 Computer Systems Godmar Back

Upload: others

Post on 20-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214

Computer Systems

Godmar Back

Page 2: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

PROCESSES

Part 1

CS 3214 Spring 2020

Page 3: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Processes

• Def: An instance of a program in execution

• OS provides each process with key abstractions– Logical control flow

• 1 flow – single-threaded process

• Multiple flows – multi-threaded process

– Private address space

– Abstracted resources: e.g., stdout/stdin file descriptors

• These abstractions create the illusion that each process has access to its own– CPU (or CPUs for multi-threaded processes)

– Memory

– Devices: e.g., terminal

CS 3214 Spring 2020

Page 4: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Context Switching

• Historical motivation for processes was introduction of multi-programming: – Load multiple processes into memory, and switch to

another process if current process is (momentarily) blocked

– This required protection and isolation between these processes, implemented by a privileged kernel: dual-mode operation.

• Time-sharing: switch to another process periodically to make sure all processes make equal progress

• Switch between processes is called a context switch

CS 3214 Spring 2020

Page 5: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Dual-Mode Operation

• Two fundamental modes:– “kernel mode” – privileged

• aka system, supervisor or monitor mode

• Intel calls its PL0, Privilege Level 0 on x86

– “user mode” – non-privileged• PL3 on x86

• Bit in CPU – controls operation of CPU– Privileged operations can only

be performed in kernel mode. Example: hlt

– Must carefully control transition between user & kernel mode

int main()

{

asm(“hlt”);

}

Page 6: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Mode Switching

• User → Kernel mode– For reasons external or internal to CPU

• External (aka hardware) interrupt: – timer/clock chip, I/O device, network card, keyboard, mouse

– asynchronous (with respect to the executing program)

• Internal interrupt (aka software interrupt, trap, or exception)– are synchronous

– can be intended (“trap”): for system call (process wants to enter kernel to obtain services)

– or unintended (usually): (“fault/exception”) (division by zero, attempt to execute privileged instruction in user mode, memory access violation, invalid instruction, alignment error, etc.)

• Kernel → User mode switch on iret instruction

CS 3214 Spring 2020

Page 7: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

A Context Switch Scenario

Process 1

Process 2

Kernel

user mode

kernel mode

Timer interrupt: P1 is preempted,

context switch to P2

System call: (trap):

P2 starts I/O operation, blocks

context switch to process 1

I/O device interrupt:

P2’s I/O complete

switch back to P2

Timer interrupt: P2 still has

time left, no context switch

Page 8: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Context Switching, Details

Process 1

Process 2

Kernel

user mode

kernel mode

intr_entry:

(saves entire CPU state)

(switches to kernel stack) intr_exit:

(restore entire CPU state)

(switch back to user stack)

iret

switch_threads: (in)

(saves caller’s state)

switch_threads: (out)

(restores caller’s state)(kernel stack switch)

Page 9: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

System Calls

Process 1

Kernel

user mode

kernel mode

User processes access kernel services by

trapping into the kernel, executing kernel

code to perform the service, then returning –

very much like a library call.

Unless the system call cannot complete

immediately, this does not involve a context

switch.

Kernel’s System Call Implementation

Page 10: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Syscall example: write(2)

• 32-bit Linux

CS 3214 Spring 2020

/* gcc -static -O -g -Wall write.c -o write */

#include <unistd.h>

int

main()

{

const char msg[] = "Hello, World\n";

return write(1, msg, sizeof msg);

}

0805005a <__write_nocancel>:

805005a: 53 push %ebx

805005b: 8b 54 24 10 mov 0x10(%esp),%edx #arg2

805005f: 8b 4c 24 0c mov 0xc(%esp),%ecx # arg1

8050063: 8b 5c 24 08 mov 0x8(%esp),%ebx # arg0

8050067: b8 04 00 00 00 mov $0x4,%eax # syscall no

805006c: cd 80 int $0x80

805006e: 5b pop %ebx

805006f: 3d 01 f0 ff ff cmp $0xfffff001,%eax

8050074: 0f 83 56 1e 00 00 jae 8051ed0 <__syscall_error>

805007a: c3 ret

/usr/include/asm/unistd.h:

….

#define __NR_write 4

….

Page 11: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Kernel

Threads

Process 1

Process 2

Kernel

user mode

kernel mode

Most OS support kernel threads that never run in

user mode – these threads typically perform book

keeping or other supporting tasks. They do not

service system calls or faults.

Kernel Thread

Careful: “kernel thread” not the same as

kernel-level thread (KLT) – more on KLT later

Page 12: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Context vs Mode Switching

• Mode switch guarantees kernel gains control when needed– To react to external events

– To handle error situations

– Entry into kernel is controlled

• Not all mode switches lead to context switches– Kernel decides when – subject of scheduling policies

• Mode switch does not change the identity of current process/thread– See blue/yellow colors in slide on ctxt switch details

• Hardware knows about modes, does not (typically) know about contexts

CS 3214 Spring 2020

Page 13: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Bottom Up View: Exceptions

• An exception is a transfer of control to the

OS in response to some event (i.e., change

in processor state)

User Process OS

exception

exception processing

by exception handler

exception

return (optional)

event currentnext

CS 3214 Spring 2020

Page 14: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Reasoning about Processes:

Process States

• Only 1 process (per CPU) can be in RUNNING state

RUNNING

READYBLOCKED

Process

must wait

for event

Event arrived

Scheduler

picks process

Process

preempted

Page 15: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Process States

• RUNNING:– Process is on CPU, its instructions are executed

• READY:– Process could make progress if a CPU were available

• BLOCKED:– Process cannot make progress even if a CPU were available

because it’s waiting for something (e.g., a resource, a signal, a point in time, a child to terminate, I/O, …)

• Model is simplified– OS have between 5 and 10 states typically

• Terminology not consistent across OS:– E.g., Linux calls BLOCKED “SLEEPING” and both READY and

RUNNING processes are called “RUNNING”; a “RUNNING” process is also called the ‘current’ process on its CPU.

CS 3214 Spring 2020

Page 16: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

User View

• If process’s lifetimes overlap, they are said to

execute concurrently

– Else they are sequential

• Default assumption is concurrent execution

• Exact execution order is unpredictable

– Programmer should never make any assumptions

about it

• Any interaction between processes must be

carefully synchronized

CS 3214 Spring 2020

Page 17: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Process Creation

• Two common paradigms:

– Cloning vs. spawning

• Cloning: (Unix)

– “fork()” clones current process

– child process then loads new program

• Spawning: (Windows)

– “exec()” spawns a new process with new program

• Difference is whether creation of new process

also involves a change in program

Page 18: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

fork()#include <unistd.h>

#include <stdio.h>

int

main()

{

int x = 1;

if (fork() == 0) {

// only child executes this

printf("Child, x = %d\n", ++x);

} else {

// only parent executes this

printf("Parent, x = %d\n", --x);

}

// parent and child execute this

printf("Exiting with x = %d\n", x);

return 0;

}

Child, x = 2

Exiting with x = 2

Parent, x = 0

Exiting with x = 0

Page 19: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

fork()#include <sys/types.h>

#include <unistd.h>

#include <stdio.h>

int main(int ac, char *av[])

{

pid_t child = fork();

if (child < 0)

perror(“fork”), exit(-1);

if (child != 0) {

printf ("I'm the parent %d, my child is %d\n",

getpid(), child);

wait(NULL); /* wait for child (“join”) */

} else {

printf ("I'm the child %d, my parent is %d\n",

getpid(), getppid());

execl("/bin/echo", "echo", "Hello, World", NULL);

}

}

Page 20: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

fork/exec/exit/wait

CS 3214 Spring 2020

fork() wait()

exit()exec()

Page 21: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

fork() vs. exec()

• fork():

– Clone most state of parent, including memory

– Inherit some state, e.g. file descriptors

– Keeps program, changes process

– Called once, returns twice

• exec():

– Overlays current process with new executable

– Keeps process, changes program

– Called once, does not return (if successful)

CS 3214 Spring 2020

Page 22: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

exit(3) vs. _exit(2)

• exit(3) destroys current processes

• OS will free resources associated with it– E.g., closes file descriptors, etc. etc.

• Can have atexit() handlers– _exit(2) skips them

• Exit status is stored and can be retrieved by parent– Single integer

– Convention: exit(EXIT_SUCCESS) signals successful execution, where EXIT_SUCCESS is 0

CS 3214 Spring 2020

Page 23: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

wait() vs waitpid()

• int wait(int *status)

– Blocks until any child exits

– If status != NULL, will contain value child passed to exit()

– Return value is the child pid

– Can also tell if child was abnormally terminated

• int waitpid(pid_t pid, int *status, int options)

– Can say which child to wait for

CS 3214 Spring 2020

Page 24: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

If multiple children completed, will take in arbitrary order

– Can use macros WIFEXITED and WEXITSTATUS to get information about exit status

void fork10()

{

pid_t pid[N];

int i;

int child_status;

for (i = 0; i < N; i++)

if ((pid[i] = fork()) == 0)

exit(100+i); /* Child */

for (i = 0; i < N; i++) {

pid_t wpid = wait(&child_status);

if (WIFEXITED(child_status))

printf("Child %d terminated with exit status %d\n",

wpid, WEXITSTATUS(child_status));

else

printf("Child %d terminate abnormally\n", wpid);

}

}

Wait Example

CS 3214 Spring 2020

Page 25: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Observations on fork/exit/wait• Process can have many children at any point in time

• Establishes a parent/child relationship– Resulting in a process tree

• Zombies: processes that have exited, but their parent hasn’t waited for them– “Reaping a child process” – call wait() so that zombie’s

resources can be destroyed

• Orphans: processes that are still alive, but whose parent has already exited (without waiting for them)– Become the child of a dedicated process (“init”) who will

reap them when they exit

• “Run Away” processes: processes that (unintentionally) execute an infinite loop and thus don’t call exit() or wait()

CS 3214 Spring 2020

Page 26: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

The fork()/join() paradigm

• After fork(), parent & child execute in parallel– Unlike a fork in the road, here we

take both roads

• Used in many contexts

• In Unix, ‘join()’ is called wait()

• Purpose:– Launch activity that can be done in

parallel & wait for its completion

– Or simply: launch another program and wait for its completion (shell does that)

Parent:

fork()

Parent:

join()

Parent

process

executes

Child

process

executes

Child

process

exits

OS notifies

Page 27: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

What do these

command lines do?

a) unix> one

b) unix> one first second third

c) unix> one &

d) unix> one < a

e) unix> one > b

f) unix> one | two

g) unix> one < a | two > b

h) unix> one | two | three | four &

i) unix> one & two & three

CS 3214 Spring 2020

Page 28: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

FILE DESCRIPTORS

CS 3214 Spring 2020

Page 29: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Unix File Descriptors

• Unix provides a file descriptor abstraction

• File descriptors are– Small integers that have a local meaning within

one process

– Can be obtained from kernel • Several functions create them, e.g. open()

– Can refer to various kernel objects (not just files)

– Can be passed to a standard set of functions:• read, write, close, lseek, (and more)

– Can be inherited when a process forks a child

CS 3214 Spring 2020

Page 30: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Examples

• 0-2 are initially assigned

– 0 – stdin

– 1 – stdout

– 2 – stderr

– But this assignment is not fixed – process can

change it via syscalls

• int fd = open(“file”, O_RDONLY);

• int fd = creat(“file”, 0600);

CS 3214 Spring 2020

Page 31: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Implementing I/O Redirection

• dup and dup2() system call

• pipes: pipe(2)

CS 3214 Spring 2020

Page 32: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

dup2

CS 3214 Spring 2020

#include <stdio.h>

#include <stdlib.h>

// redirect stdout to a file

int

main(int ac, char *av[])

{

int c;

int fd = creat(av[1], 0600);

if (fd == -1)

perror("creat"), exit(-1);

if (dup2(fd, 1) == -1)

perror("dup2"), exit(-1);

while ((c = fgetc(stdin)) != EOF)

fputc(c, stdout);

}

Page 33: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

The Big Picture

CS 3214 Spring 2020

Process 1

0

1

2

user view kernel view

Terminal

Deviceopen(“x”)3

Open

File

File

Descriptor x

dup2(3,0)

Process 2

0

1

2

3

close (3)

Final steps (not included in

animation): Parent does

close(3), leaves the child’s

stdin to be the only reference

to the file descriptor.

Once the child is done and

closes its stdin (or exits!)

OS closes file and removes

entry from Open File table.

Page 34: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

The Big Picture

CS 3214 Spring 2020

Process 1

0

1

2

user view kernel view

Terminal

Deviceopen(“x”)3

Open

File

File

Descriptor x

4

File

Descriptor

open(“x”) close(4)

Opening the same file within one process yields

separate read/write offsets for each descriptor

- Compare to dup/dup2/fork which create a

second reference to the same file descriptor

with a shared offset/file pointer.

Page 35: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Reference Counting

• Multiple file descriptors may refer to same open file– Within the same process:

• fd = open(“file”); fd2 = dup(fd);

– Across related processes:• fd = open(“file”); fork();

• But one process can also open a file multiple times:– fd = open(“file”); fd2 = open(“file”);

– In this case, fd and fd2 have different read/write offsets

• In both cases, closing fd does not affect fd2

• Reference Counting at 2 Levels:– Kernel keeps track of how many processes refer to a file descriptor –

fork() and dup()/dup2() may add refs

– And keeps track of how many file descriptors refer to an open file across all processes. Ditto for other kernel objects such as pipes.

• close(fd) removes reference in current process only!

CS 3214 Spring 2020

Page 36: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Practical Implications

• Number of simultaneously open file descriptors per process is limited– Soft limit of 1024 on current Linux, for instance;

can be increased to up to 64K (hard limit) on rlogin machines

• Must make sure fd’s are closed when done– Else ‘open()’ may fail

• Number space is reused– “double-close” error may inadvertently close a

new file descriptor assigned the same number

CS 3214 Spring 2020

Page 37: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

IPC via “pipes”

• A bounded buffer providing a stream of bytes flowing through

• Properties– Writer() can put data in pipe as long as there is space

• If pipe() is full, writer blocks until reader reads()

– Reader() drains pipe()• If pipe() is empty, readers blocks until writer writes

• Classic abstraction– Decouples reader & writer

– Safe – no race conditions

– Automatically controls relative progress – if writer produces data faster than reader can read it, it blocks – and OS will likely make CPU time available to reader() to catch up. And vice versa.

CS 3214 Spring 2020

Fixed Capacity Buffer

write() read()

Page 38: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

int main()

{

int pipe_ends[2];

if (pipe(pipe_ends) == -1)

perror("pipe"), exit(-1);

int child = fork();

if (child == -1)

perror("fork"), exit(-1);

if (child == 0) {

char msg[] = { "Hi" };

close(pipe_ends[0]);

write(pipe_ends[1], msg, sizeof msg);

} else {

char bread, pipe_buf[128];

close(pipe_ends[1]);

printf("Child said "); fflush(stdout);

while ((bread = read(pipe_ends[0], pipe_buf, sizeof pipe_buf)) > 0)

write(1, pipe_buf, bread);

}

}

pipe

Note: there is no race condition in

this code. No matter what the

scheduling order is, the message sent

by the child will reach the parent.

Page 39: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

esh – extensible shell

• Open-ended assignment

• Encourage collaborative learning

– Run each other’s plug-ins

• Does not mean collaboration on your

implementation

• Secondary goals:

– Exposure to yacc/lex and exposure to OO-

style programming in C

CS 3214 Spring 2020

Page 40: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Big Picture Issues• State maintenance

– How to maintain an accurate depiction of the state of external entities subject to change outside of the program’s control, when…

– external entities can change state asynchronously, and …

– tools for monitoring state changes (e.g., signals) are imperfect

• Concurrency control– How to ensure the correctness of data when …

– the control flow is subject to asynchronous interruption (e.g., by signal handling), and…

– there are complex control flows in shell

CS 3214 Spring 2020

Page 41: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Big Questions … and what you need to know

• In what state is every child process of the shell? How can the shell affect the state of its child processes?

– How to create processes executing specified code

– Signals and their effects on the receiver

– How to send/receive signals

– Terminal control and I/O

• What functions in the shell code must not be interrupted by asynchronous events?

– System calls for receiver to manage incoming signals

– How to handle incoming signals

CS 3214 Spring 2020

Page 42: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Using the list

implementation

• Key features: “list cell” – here call ‘list_elem’ is embedded in each object being kept in list– Means you need 1 list_elem per list you want to keep an object in

CS 3214 Spring 2020

struct esh_pipeline:

….

struct list commands

struct list_elem head

struct list_elem tail

struct list_elem *next;

struct list_elem *prev;

struct list_elem *next;

struct list_elem *prev;

struct esh_command:

….

struct list_elem elem;

….

struct list_elem *next;

struct list_elem *prev;

struct esh_command:

….

struct list_elem elem;

….

struct list_elem *next;

struct list_elem *prev;

list_entry(e, struct esh_command, elem)

Page 43: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Unix Startup: Step 1

init [1]

[0] Process 0: handcrafted kernel process

Child process 1 execs /sbin/init

1. Pushing reset button loads the PC with the address of a small

bootstrap program.

2. Bootstrap program loads the boot block (disk block 0).3. Boot block program loads kernel binary (e.g., /boot/vmlinux)

4. Boot block program passes control to kernel.

5. Kernel handcrafts the data structures for process 0.

Process 0 forks child process 1

CS 3214 Spring 2020

Page 44: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Unix Startup: Step 2

init [1]

[0]

gettyDaemonse.g. sshd, httpd

/etc/inittabinit forks and execs

daemons per /etc/inittab, and forks

and execs a getty program

for the console

CS 3214 Spring 2020

Page 45: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Unix Startup: Step 3

init [1]

[0]

The getty process

execs a login

programlogin

CS 3214 Spring 2020

Page 46: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Unix Startup: Step 4

init [1]

[0]

login reads login and passwd.

if OK, it execs a shell.if not OK, it execs another getty

tcsh

CS 3214 Spring 2020

Page 47: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Shell Programs• A shell is an application program that runs

programs on behalf of the user.– sh – Original Unix Bourne Shell

– csh – BSD Unix C Shell, tcsh – Enhanced C Shell

– bash –Bourne-Again Shell

Execution is a sequence of read/evaluate steps

int main()

{

char cmdline[MAXLINE];

while (1) {

/* read */

printf("> ");

fgets(cmdline, MAXLINE, stdin);

if (feof(stdin))

exit(0);

/* evaluate */

eval(cmdline);

}

}

CS 3214 Spring 2020

Page 48: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

void eval(char *cmdline)

{

char *argv[MAXARGS]; /* argv for execve() */

int bg; /* should the job run in bg or fg? */

pid_t pid; /* process id */

bg = parseline(cmdline, argv);

if (!builtin_command(argv)) {

if ((pid = fork()) == 0) { /* child runs user job */

if (execve(argv[0], argv, environ) < 0) {

printf("%s: Command not found.\n", argv[0]);

exit(0);

}

}

if (!bg) { /* parent waits for fg job to terminate */

int status;

if (waitpid(pid, &status, 0) < 0)

unix_error("waitfg: waitpid error");

}

else /* otherwise, don’t wait for bg job */

printf("%d %s", pid, cmdline);

}

} Simple Shell eval Function

CS 3214 Spring 2020

Page 49: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Problem with Simple Shell Example

• Shell correctly waits for and reaps foreground jobs.

• But what about background jobs?

– Will become zombies when they terminate.

– Will never be reaped because shell (typically) will not terminate.

– Creates a memory leak and prevent pids from being reused

• Solution: Reaping background jobs requires a mechanism called a signal.

• Asynchronous – can arrive at any time. OS will interrupt process as soon as it does

CS 3214 Spring 2020

Page 50: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Page 51: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Signals• A signal is a small message that notifies a process

that an event of some type has occurred in the system.– Kernel abstraction for exceptions and interrupts of

many (unrelated) kinds.– Sent from the kernel to a process.– Different signals are identified by small integer ID’s– Signal usually carry along some information about why

they were sent.

ID Name Default Action Corresponding Event

2 SIGINT Terminate Interrupt from keyboard (ctl-c)

9 SIGKILL Terminate Kill program (cannot override or ignore)

11 SIGSEGV Terminate & Dump Segmentation violation

14 SIGALRM Terminate Timer signal

17 SIGCHLD Ignore Child stopped or terminated

CS 3214 Spring 2020

Page 52: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Sending Signals

• Sending a signal– Kernel sends a signal to a destination process by updating some

state in the context of the destination process. A signal that is sent but has not been delivered is said to be pending.

• Examples include:– Kernel has detected a system event such as divide-by-zero

(SIGFPE), or illegal memory access (SIGSEGV)

– The kernel wishes to inform a process about the termination of a child (SIGCHLD)

– Another process (or the process itself) has invoked the killsystem call to explicitly request a signal (SIGUSR1)

– The terminal driver relays a request by the user to interrupt (SIGINT) or suspend (SIGTSTP) a process in the foreground process.

– And others

CS 3214 Spring 2020

Page 53: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Receiving a signal

• A destination process receives a signal when it is forced by the kernel to react in some way to the delivery of the signal.

• Three possible ways to react (subject to the process’s discretion):– Ignore the signal (do nothing)

– Terminate the process.

– Catch the signal by executing a user-level function called a signal handler.

• Akin to a hardware exception handler being called in response to an asynchronous interrupt.

• Not all options apply to all signals, and different signals come with different defaults

CS 3214 Spring 2020

Page 54: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Default Actions

• Each signal type has a predefined default action, which is one of:

– The process terminates

– The process terminates and dumps core.

– The process stops until restarted by a SIGCONT signal.

– The process ignores the signal.

• Type ‘man 7 signal’ to learn what the default action for a given signal is.

CS 3214 Spring 2020

Page 55: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Pending Signals

• A signal is pending if it has been sent but not yet received

– There can be at most one pending signal of any particular type.

– A pending signal is received at most once.

– i.e., Signals are not queued• If a process has a pending signal of type k, then subsequent

signals of type k that are sent to that process are discarded.

• Example: [sigchlddoesnotqueue.c]

• A process can block the receipt of certain signals.

– Blocked signals can be sent, but will not be received until the signal is unblocked.

CS 3214 Spring 2020

Page 56: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Kernel Internals

• How does the kernel implement this?

• Kernel maintains pending and blocked bit vectors in the context of each process.– pending – represents the set of pending signals

• Kernel sets bit k in pending whenever a signal of type k is sent.

• Kernel clears bit k in pending whenever a signal of type k is received/delivered.

– blocked – represents the set of blocked signals

• Can be set and cleared by the application using the sigprocmask system call.

CS 3214 Spring 2020

Page 57: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Kernel Internals, cont’d

• When does the kernel deliver signals?– Only at certain delivery points

• If target process executes in user mode, delivery can occur at any time.– This is done by forcing the process to enter kernel mode, if

necessary via processor-to-processor interrupt

• If target process executes inside the kernel, delivery occurs only at the next point where kernel code checks for pending signals (so as to not complicate the kernel’s control flow)

• If target process is in the BLOCKED state (Linux: SLEEPING/INTERRUPTABLE), it is made READY and will check for pending signals.

CS 3214 Spring 2020

Page 58: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Kernel Internals, cont’d

• Suppose kernel is in kernel mode, and ready to deliver a signal

• Kernel computes pnb = pending & ~blocked

– The set of pending nonblocked signals for process p

• If (pnb == 0) – Pass control to next instruction in the logical flow for p.

– (in other words, don’t do anything)

• Else– Choose least nonzero bit k in pnb and force process p to

receive signal k.

– The receipt of the signal triggers some action by p

– Repeat for all nonzero k in pnb.

– Pass control to next instruction in logical flow for p.

CS 3214 Spring 2020

Page 59: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Examples

• The following slides show some examples

to demonstrate the usefulness and power

of the signal facility

CS 3214 Spring 2020

Page 60: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Sending Signals with kill Program• kill program

sends arbitrary signal to a process or process group

• Examples– kill –9 24818

• Send SIGKILL to process 24818

– kill –9 –24817

• Send SIGKILL to every process in process group 24817.

linux> ./forks 16

linux> Child1: pid=24818 pgrp=24817

Child2: pid=24819 pgrp=24817

linux> ps

PID TTY TIME CMD

24788 pts/2 00:00:00 tcsh

24818 pts/2 00:00:02 forks

24819 pts/2 00:00:02 forks

24820 pts/2 00:00:00 ps

linux> kill -9 -24817

linux> ps

PID TTY TIME CMD

24788 pts/2 00:00:00 tcsh

24823 pts/2 00:00:00 ps

linux>

CS 3214 Spring 2020

Page 61: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Sending Signals with kill Function

CS 3214 Spring 2020

void fork12()

{

pid_t pid[N];

int i, child_status;

for (i = 0; i < N; i++)

if ((pid[i] = fork()) == 0)

while(1); /* Child infinite loop */

/* Parent terminates the child processes */

for (i = 0; i < N; i++) {

printf("Killing process %d\n", pid[i]);

kill(pid[i], SIGINT);

}

/* Parent reaps terminated children */

for (i = 0; i < N; i++) {

pid_t wpid = wait(&child_status);

if (WIFEXITED(child_status))

printf("Child %d terminated with exit status %d\n",

wpid, WEXITSTATUS(child_status));

else

printf("Child %d terminated abnormally\n", wpid);

}

}

Page 62: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

A Program That Reacts to

Externally Generated Events (ctrl-c)#include <stdlib.h>

#include <stdio.h>

#include <signal.h>

void handler(int sig) {

printf("You think hitting ctrl-c will stop the bomb?\n");

sleep(2);

printf("Well...");

fflush(stdout);

sleep(1);

printf("OK\n");

exit(0);

}

main() {

signal(SIGINT, handler); /* installs ctrl-c handler */

while(1) {

}

}

CS 3214 Spring 2020

Page 63: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

A Program That Reacts to

Internally Generated Events#include <stdio.h>

#include <signal.h>

int beeps = 0;

/* SIGALRM handler */

void handler(int sig) {

printf("BEEP\n");

fflush(stdout);

if (++beeps < 5)

alarm(1);

else {

printf("BOOM!\n");

exit(0);

}

}

main() {

signal(SIGALRM, handler);

alarm(1); /* send SIGALRM in

1 second */

while (1) {

/* handler returns here */

}

}

linux> a.out

BEEP

BEEP

BEEP

BEEP

BEEP

BOOM!

bass>

CS 3214 Spring 2020

Page 64: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

static void

catch_segfault(int signo, siginfo_t *info, void * _ctxt)

{

ucontext_t * ctxt = _ctxt;

printf(

"Catching Segfault at sig=%d fault addr is %p eip was at %x\n",

signo, info->si_addr, ctxt->uc_mcontext.gregs[REG_RIP]);

// skip offending instruction

ctxt->uc_mcontext.gregs[REG_RIP] += 7;

// put 42 into %rsi

ctxt->uc_mcontext.gregs[REG_RSI] = 42;

// upon return, OS will apply any changes to ctxt

// to process's saved state, the restore the state

// execution will continue at 400668 + 7 = 40066f

}

int

main()

{

install_signal_handler(SIGSEGV, catch_segfault);

printf("Dereferencing NULL -> %d ...\n", *(int *)NULL);

return 0;

}

Catching

Segfaults

Page 65: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Terminal Related Signals

• Signals are also used to allow a user to

control the execution of jobs from a

terminal.

– E.g., ^C and ^Z

• This job control must be set up by the

shell, and occurs in a manner in which

kernel, shell, and terminal driver cooperate

• Talk about process groups first

CS 3214 Spring 2020

Page 66: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Motivation for Process Groups

• The key motivation for process groups was the need for a mechanism by which a signal can be sent to a group of processes

• E.g., ^C sends SIGINT to all processes spawned by a process running in the foreground

– Common misconception is that those are killed with ^C because they’re children of the foreground process.

• Rather, the shell must judiciously create process groups (and assign processes to them) to achieve the desired routing of SIGINT/SIGTSTP signals

CS 3214 Spring 2020

Page 67: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Process Groups• Every process

belongs to exactly one process group

Fore-

ground

job

Back-

ground

job #1

Back-

ground

job #2

Shell

Child Child

pid=10

pgid=10

Foreground

process group 20

Background

process group 32Background

process group 40

pid=20

pgid=20pid=32

pgid=32pid=40

pgid=40

pid=21

pgid=20

pid=22

pgid=20

getpgrp() – Return process

group of current process

setpgid() – Change process group of a process

CS 3214 Spring 2020

Page 68: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Process Groups, cont.

• Every process can form a new process group by declaring themselves a leader– setpgid(0, 0)

• But, this is not the only way – process groups can also be formed by having a parent place a process in its own or an existing group– E.g., shell places all children belonging to the same pipeline into the

same group

• Process groups are populated simply by adding processes to them– Restriction: process being added to process group must be part of the

same “session” – a concept that groups multiple procgroup’s.

• See /proc/<pid>/stat to learn pgid of a process

• Hint: in esh, call setpgid() both in shell and child:– Will not hurt to place a process into a pgroup it’s already in

– Avoid race condition that would occur if shell or child assumed that the respective other will have already done it

CS 3214 Spring 2020

Page 69: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Sending Signals from the Keyboard• Typing Ctrl-C (Ctrl-Z) sends a SIGINT (SIGTSTP) to every job in the

foreground process group.

– SIGINT – default action is to terminate each process

– SIGTSTP – default action is to stop (suspend) each process

Fore-

ground

job

Back-

ground

job #1

Back-

ground

job #2

Shell

Child Child

pid=10

pgid=10

Foreground

process group 20

Background

process

group 32

Background

process

group 40

pid=20

pgid=20pid=32

pgid=32

pid=40

pgid=40

pid=21

pgid=20

pid=22

pgid=20

CS 3214 Spring 2020

Page 70: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Stopped Processes

• Modern Unixes allow users to stop processes, which prevents them from being scheduled (even if READY) until the user changes their mind and continues them

• Can be modeled using additional states in standard process state model

– READY/STOPPED

– BLOCKED/STOPPED

CS 3214 Spring 2020

Page 71: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Stopped Processes

CS 3214 Spring 2020

BLOCKED

STOPPED

READY

STOPPED

BLOCKED

READY

RUNNING

(Mainly) under user control

(except for terminal-related SIGTTIN etc.)

Dependent on

occurrence of

event the process

is interested in

Page 72: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Managing Terminal Access

• What if multiple processes wish to read from the terminal?– (default behavior: undefined)

• Kernel uses process groups to arbitrate– One foreground process group per terminal

• Kernel will automatically suspend (via SIGTTIN) any process in a non-foreground process group that attempts to read from terminal

– Try: “vim &”

• Ditto if a process wants EXCLUSIVE write access to a terminal– E.g., vim does this, a normal write(1, …) does not

• It’s up to shell to manage access to the terminal– Learn when your background children were stopped because they attempted to read

from a terminal. Inform the user.

– Use tcsetpgrp() to set the terminal’s foreground process group when user says to put a job into the foreground

– Save and restore terminal state when making process group owner of terminal

• Example of resource arbitration

CS 3214 Spring 2020

Page 73: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Installing Signal Handlers• The signal function modifies the default action

associated with the receipt of signal signum:– sighandler_t *signal(int signum, sighandler_t *handler)

• Different values for handler:

– SIG_IGN: ignore signals of type signum

– SIG_DFL: revert to the default action on receipt of signals of type signum.

– Otherwise, handler is the address of a signal handler• Called when process receives signal of type signum

• Referred to as “installing” the handler.

• Executing handler is called “catching” or “handling” the signal.

• When the handler executes its return statement, control passes back to instruction in the control flow of the process that was interrupted by receipt of the signal.

CS 3214 Spring 2020

Page 74: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

POSIX Signal Handling

• Instead of signal, use ‘sigaction()’

• Signal handler has slightly different signature as in signal()

– Provides additional functionality

• Recommended

– signal(2) is now obsolete

– In project, recommend you use esh_signal_sethandler which is a convenient wrapper for sigaction()

CS 3214 Spring 2020

Page 75: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

esh code/* Install signal handler for signal 'sig' */

void

esh_signal_sethandler(int sig, sa_sigaction_t handler)

{

sigset_t emptymask;

sigemptyset(&emptymask);

struct sigaction sa = {

.sa_sigaction = handler,

.sa_mask = emptymask,

/* do not block any additional signals (besides

* ‘sig’) when signal handler is entered. */

.sa_flags = SA_RESTART | SA_SIGINFO

/* restart system calls when possible */

/* use three argument handler */

};

if (sigaction(sig, &sa, NULL) != 0)

esh_sys_fatal_error("sigaction failed for signal %d", sig);

}

CS 3214 Spring 2020

Page 76: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Living With Nonqueuing Signals• Must check for all terminated jobs

– Typically loop with waitpid(,..WNOHANG)

void child_handler2(int sig)

{

int child_status;

pid_t pid;

while ((pid = waitpid(-1, &child_status, WNOHANG)) > 0) {

/* update data structures

that child ‘pid’ changed status */

/* do not call printf() */

}

}

void fork15()

{

. . .

signal(SIGCHLD, child_handler2);

. . .

}

CS 3214 Spring 2020

Page 77: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Signals & Concurrency

• Signal handlers for external events can occur *anytime*– Unless blocked – must think of signal handler as concurrent

flow of control

CS 3214 Spring 2020

user mode

kernel mode

handler

regular program

signal delivered

Signal handler returns

sigreturn()

Page 78: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Concurrent Accesses To Data Structures

• Consider shell maintaining a list of jobs

– Main program forks, adds jobs

– SIGCHLD handler may reap jobs, perhaps

remove jobs from joblist

CS 3214 Spring 2020

void

list_insert (struct list_elem *before, struct list_elem *elem)

{

elem->prev = before->prev;

elem->next = before;

before->prev->next = elem;

before->prev = elem;

}

If signal arrives inside the

instructions doing the list

manipulation, signal handler will

see inconsistent list – calls to

list_insert will lead to havoc

Page 79: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Signals & Concurrency

• Blocking a signal guarantees that signal handler execution will not occur even when signal is delivered– Will occur as soon as the signal is unblocked

CS 3214 Spring 2020

user mode

kernel mode

handler

block(SIGNAL)

signal sent

Signal handler returns

sigreturn()

unblock(SIGNAL)signal pending

Page 80: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Reentrancy

• A function is said to be reentrant if it can be safely called again even while a call is still in progress (i.e., has not returned)– Could be on a regular control flow path, e.g. recursion

– Or 2nd call could be from signal handler

– Or (discuss this later in more detail) from another thread

• Examples of functions that are not reentrant– inet_ntoa(), strtok() – uses private buffer

– printf() – takes a lock

• You cannot call non-reentrant functions from a signal handler for signal ‘s’– Unless you prevent the delivery of ‘s’ during calls in your

main program via { block(s); ….; unblock(s); }

CS 3214 Spring 2020

Page 81: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Async-Signal Safety• ‘async-signal safe functions’ - safe to call from a

signal handler– Provide the signal is allowed to occur (i.e., is not

blocked) while calls to these functions are in progress – else no issue arises

• See list in man 2 signal. Includes waitpid(), etc.

• The kicker: printf() is not safe to call in a signal handler– Frequent source of bugs (even in some textbook

sample code….!)

– Can use ‘snprintf() + write(1, …)’ if needed

• Please read Web Aside ECF:SAFETY on Async-Signal Safety

CS 3214 Spring 2020

Page 82: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Avoiding Race Conditions in esh

• Identify data structures shared between signal handler and main program

– E.g., everything the signal handler (or functions called from it) accesses

• Then protect accesses to those data structures by blocking the signal around the access

• Use ‘assert()’ to verify that you did this correctly– assert(esh_signal_is_blocked(SIGCHLD));

• Aside: the technique of delaying such interrupts is used inside OS kernels in a very similar way, e.g. when devices trigger interrupts

CS 3214 Spring 2020

Page 83: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Signals & System Calls

• What if a signal becomes pending while the recipient is BLOCKED in a system call such as wait() or read()?

• This system call could take a long time to complete, but signals are intended to be a timely mechanism

• OS will resume (make READY) the process, deliver the signal, and then (under typical instructions, i.e. SIG_RESTART) restart the system call.– Nice – program need not be aware of this scenario

– But you’ll occasionally see it in strace

CS 3214 Spring 2020

Page 84: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Signals – Summary

• Universal mechanism to notify a process of events– Internal events (memory access violation, process-

internal timers, …)

– External events• User-driven: ^C, ^Z

• Resulting from other processes: explicit kill(2), or SIGCHLD

• Resulting from kernel event: e.g., SIGTTOU, SIGTTIN

– Process groups are vessels for the delivery of signals to an entire group

• Signal handler can change program state before returning– Extremely powerful

CS 3214 Spring 2020

Page 85: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

CS 3214 Spring 2020

Page 86: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Nonlocal Jumps: setjmp/longjmp

• Powerful (but dangerous) user-level mechanism for transferring control to an arbitrary location.– Controlled way to break the procedure call/return discipline

– Useful for error recovery and signal handling

• int setjmp(jmp_buf j)

– Must be called before longjmp

– Identifies a return site for a subsequent longjmp.

– Called once, returns one or more times

• Implementation:– Remember where you are by storing the current register

context, stack pointer, and PC value in jmp_buf.

– Return 0

CS 3214 Spring 2020

Page 87: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

setjmp/longjmp Example#include <setjmp.h>

jmp_buf buf;

main() {

if (setjmp(buf) != 0) {

printf("back in main due to an error\n");

else

printf("first time through\n");

p1(); /* p1 calls p2, which calls p3 */

}

...

p3() {

<error checking code>

if (error)

longjmp(buf, 1)

}

CS 3214 Spring 2020

Page 88: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

setjmp/longjmp (cont)

• void longjmp(jmp_buf j, int i)

– Meaning:• return from the setjmp remembered by jump buffer j again...

• …this time returning i instead of 0

– Called after setjmp

– Called once, but never returns

• longjmp Implementation:

– Restore register context from jump buffer j

– Set %eax (the return value) to i

– Jump to the location indicated by the PC stored in jump buf j.

CS 3214 Spring 2020

Page 89: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

A Program That Restarts Itself When ctrl-c’d

while(1) {

sleep(1);

printf("processing...\n");

}

}

Ctrl-c

Ctrl-c

Ctrl-c

CS 3214 Spring 2020

bass> a.out

starting

processing...

processing...

restarting

processing...

processing...

processing...

restarting

processing...

restarting

processing...

processing...

#include <stdio.h>

#include <signal.h>

#include <setjmp.h>

sigjmp_buf buf;

void handler(int sig) {

siglongjmp(buf, 1);

}

main() {

signal(SIGINT, handler);

if (!sigsetjmp(buf, 1))

printf("starting\n");

else

printf("restarting\n");

Page 90: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Limitation of setjmp/longjmp

• Longjmp restores stack pointer– Thus activates a new stack frame

– Stack frame must still be valid

• Consequence:– Can only longjmp “up the stack” to functions that haven’t yet

returned when longjmp() is called

– repositioning the stack pointer automatically “destroys” intermediate stack frames

• But does not call cleanup functions provided in some languages (e.g. C++ destructors or Java ‘finally’ clauses)

– Longjmp’ing “down the stack” would “reactivate” already destroyed stack frames

• Does not necessarily crash, but leads to unpredictable results

• Think of setjmp/longjmp as a low-level mechanism to implement a variant of C++/Java style exceptions

CS 3214 Spring 2020

Page 91: CS 3214 Computer Systemscs3214/spring2020/lectures/Lecture-Intro-… · CS 3214 Spring 2020 Dual-Mode Operation • Two fundamental modes: –“kernel mode” –privileged • aka

Summary

• Signals provide process-level exception handling– Can generate from user programs

– Can define effect by declaring signal handler

• Some caveats– Very high overhead

• >10,000 clock cycles

• Only use for exceptional conditions

– Don’t have queues (exception: “real-time signals”)• Just one bit for each pending signal type

• Nonlocal jumps provide exceptional control flow within process– Within constraints of stack discipline

CS 3214 Spring 2020