1 Processes and Threads Creation and Termination States Usage Implementations.

Post on 28-Mar-2015

221 views 0 download

Tags:

Transcript of 1 Processes and Threads Creation and Termination States Usage Implementations.

1

Processes and Threads

Creation and TerminationStatesUsageImplementations

2

Processes

• Program in execution

(cf. recipe vs. cooking)

• Multiprogramming - pseudo-parallelism

(vs. true hardware parallelism of multiprocessor systems)

3

The Process Model

• Multiprogramming of four programs• Conceptual model of 4 independent, sequential processes• Only one program active at any instant

4

Process CreationPrincipal events that cause process creation

1. System initialization (foreground a daemon processes)

2. Execution of a process creation system call (data from network)

3. User request to create a new process

4. Initiation of a batch job

UNIX: fork system call (+ execve)Windows: CreateProcess function call

5

Process Termination

Conditions which terminate processes

1. Normal exit (voluntary) - (exit, ExitProcess)

2. Error exit (voluntary)

3. Fatal error (involuntary), e.g. program bug

4. Killed by another process (involuntary) - kill, TerminateProcess

6

Process Hierarchies

• Parent creates a child process, child processes can create its own process

• Forms a hierarchy– UNIX calls this a "process group”

– init

• Windows has no concept of process hierarchy– all processes are created equal

7

Process States (1)

• Possible process states– running– blocked– ready

• Transitions between states shown

8

Process States (2)

• Lowest layer of process-structured OS– handles interrupts, scheduling

• Above that layer are sequential processes

9

Implementation of Processes (1)

Fields of a process table entry

10

Implementation of Processes (2)

Skeleton of what lowest level of OS does when an interrupt occurs

11

ThreadsProcess =

resource grouping (code, data, open files, etc.)

+

execution (program counter, registers, stack)

Multithreading:

• multiple execution takes place in the same process environment

• co-operation by sharing resources (address space, open files, etc.)

12

The Thread Model (1)

(a) Three processes each with one thread(b) One process with three threads

13

The Thread Model (2)

• Items shared by all threads in a process

• Items private to each thread

14

The Thread Model (3)

Each thread has its own stack to keep track execution history (called procedures)

15

Advantages

• Pseudo-parallelism with shared address space and data

• Easier to create and destroy than processes

• Better performance for I/O bound applications

16

Thread Usage (1)

A word processor with three threadsWriting a book: interactive and background

threads sharing the same file

17

Thread Usage (2)

A multithreaded Web server

18

Thread Usage (3)

• Rough outline of code for previous slide(a) Dispatcher thread(b) Worker thread

19

Thread Usage (4)

Three ways to construct a server

20

Implementing Threads in User Space

A user-level threads package

21

(Dis)advantages

+: no specific OS support needed

faster than kernel instructions

process-specific scheduling algorithms

-: blocking system calls (select)

page faults

22

Implementing Threads in the Kernel

A threads package managed by the kernel

23

(Dis)advantages

+: handling blocking and page faults

-: more costly (but recycling threads)

24

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

25

Scheduler Activations• Goal:

– mimic functionality of kernel threads– gain performance of user space threads

• Avoids unnecessary user/kernel transitions• Kernel assigns virtual processors to each process

– lets runtime system allocate threads to processors, upcall

• Problem: Fundamental reliance on kernel (lower layer)

calling procedures in user space (higher layer)

26

Pop-Up Threads

• Creation of a new thread when message arrives(a) before message arrives(b) after message arrives (quick)

27

Making Single-Threaded Code Multithreaded (1)

Conflicts between threads over the use of a global variable

28

Making Single-Threaded Code Multithreaded (2)

Threads can have private global variables.But non-reentrant library procedures.