Docsity

Operating Systems: Processes, Signals, and Exceptions, Slides of Computer System Design and Architecture

Partial preview of the text

Download Operating Systems: Processes, Signals, and Exceptions and more Slides Computer System Design and Architecture in PDF only on Docsity!

Exceptional Control Flow

Objectives

Be exposed to different types of exceptional

control flow: hardware, system software,

application software

Be able to use software exceptional control

flow to create simple concurrent programs

Exceptional Control Flow

Hardware:

 Exceptions (= interrupts)

System software:

 Signals

 Thread context switch

 Process context switch

Application software (varies by language):

 Non-local jumps

 Exceptions – same name, similar idea

Hardware Exceptions

Exception = A transfer of control to the OS in response

to some event (i.e., a change in processor state)

exception return

(optional)

exception processing

by exception handler

exception

event

User Process OS

Time

Exception Table (Interrupt Vector)

How to find appropriate handler?

Initialized by OS at boot time

  1. Each event type has an exception number k0…n-
  2. Interrupt vector (a jump table) entry k points to an exception handler
  3. Handler k is called each time exception k occurs

Interrupt vector

Exception numbers 0 1 2

n-1 Address of handler n-

Address of handler 2

Address of handler 1

Address of handler 0

Exception Classes

Asynchronous (not caused by an instruction)

 Interrupt

  • Signal from an I/O device (i.e. network packet)
  • Always return to the next instruction

Synchronous (caused by an instruction)

 Trap

  • Intentional exception (i.e. system call)
  • Always return to the next instruction

 Fault

  • Potentially recoverable error (i.e. page fault)
  • Might return to the current instruction (if problem is fixed) to allow it to re-execute

 Abort

  • Non-recoverable error (i.e. machine check error)
  • Terminates the application

Process Address Space

Every program believes it

has exclusive use of the

system’s address space

Process address space is

private

 Can not be read/written by any other process  I.e., address 0x400000 is different for every process

User Stack

Shared Libraries

Heap

Read/Write Data

Read-only Code and Data

Unused

0x7FFFFFFFFFFF

%rsp

0x

0x

User and Kernel Mode

Process isolation

 Hardware restricts the instructions an application

can execute

Mode bit: user vs. kernel mode

 In kernel mode, everything is accessible

 In user mode, cannot execute privileged

instructions

  • Halt the processor
  • Change the mode bit
  • Initiate I/O
  • Access data outside process address space
  • etc.

Exceptions switch from user to kernel mode

Fault Example

Memory Reference

 User writes to memory location

 Address is not valid

 Page handler detects invalid address

 Send SIGSEGV signal to user process

 User process exits with “segmentation fault”

int a[1000]; int main(void) { a[5000] = 23; return (0); }

0x400448

: movl $0x17,0x2051ee(%rip) # 0x

page fault Detect invalid address

event (^) movl

Signal process

User Process OS

Fault Example

Memory Reference

 User reads from memory location

 That portion of user’s memory is currently on disk

 Page handler must load page into physical memory

 Returns to faulting instruction

 Successful on second try

User Process OS

page fault Load page into memory return

event (^) mov

int a[1000] = { 0, 1, 2, … }; int main(void) { printf(“%d\n”, a[500]); return (0); }

0x40049c <main+4>: mov 0x200bae(%rip),%esi # 0x601050 <a+2000>

Concurrent Processes

Two processes run concurrently (are

concurrent) if their flows overlap in time

Otherwise, they are sequential

Examples:

 Concurrent: A & B, A & C

 Sequential: B & C

Time

Process A Process B Process C

User View of Concurrent Processes

Control flows for concurrent processes are

physically disjoint in time

However, we can think of concurrent

processes are running in parallel with each

other

Time

Process A Process B Process C

Creating a Process

int fork(void)

 Creates new child process identical to calling

parent process

 Returns 0 to the child process

 Returns child’s pid to the parent process

 Returns -1 to the parent process upon error (no

child is created)

if (fork() == 0) printf("hello from child\n"); else printf("hello from parent\n");

Interesting & confusing – called once, but returns twice!

Process IDs

Each process is assigned a unique process ID

(PID)

 Positive, non-zero identifier

 Used by many functions to indicate a particular

process

 Visible with ps command

Obtaining process IDs

 PID of calling process:

pid_t getpid(void);

 PID of parent process:

pid_t getppid(void);