1 Processes and Threads Creation and Termination States Usage Implementations.

28
1 Processes and Threads Creation and Termination States Usage Implementations

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

Page 1: 1 Processes and Threads Creation and Termination States Usage Implementations.

1

Processes and Threads

Creation and TerminationStatesUsageImplementations

Page 2: 1 Processes and Threads Creation and Termination States Usage Implementations.

2

Processes

• Program in execution

(cf. recipe vs. cooking)

• Multiprogramming - pseudo-parallelism

(vs. true hardware parallelism of multiprocessor systems)

Page 3: 1 Processes and Threads Creation and Termination States Usage Implementations.

3

The Process Model

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

Page 4: 1 Processes and Threads Creation and Termination States Usage Implementations.

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

Page 5: 1 Processes and Threads Creation and Termination States Usage Implementations.

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

Page 6: 1 Processes and Threads Creation and Termination States Usage Implementations.

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

Page 7: 1 Processes and Threads Creation and Termination States Usage Implementations.

7

Process States (1)

• Possible process states– running– blocked– ready

• Transitions between states shown

Page 8: 1 Processes and Threads Creation and Termination States Usage Implementations.

8

Process States (2)

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

• Above that layer are sequential processes

Page 9: 1 Processes and Threads Creation and Termination States Usage Implementations.

9

Implementation of Processes (1)

Fields of a process table entry

Page 10: 1 Processes and Threads Creation and Termination States Usage Implementations.

10

Implementation of Processes (2)

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

Page 11: 1 Processes and Threads Creation and Termination States Usage Implementations.

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.)

Page 12: 1 Processes and Threads Creation and Termination States Usage Implementations.

12

The Thread Model (1)

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

Page 13: 1 Processes and Threads Creation and Termination States Usage Implementations.

13

The Thread Model (2)

• Items shared by all threads in a process

• Items private to each thread

Page 14: 1 Processes and Threads Creation and Termination States Usage Implementations.

14

The Thread Model (3)

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

Page 15: 1 Processes and Threads Creation and Termination States Usage Implementations.

15

Advantages

• Pseudo-parallelism with shared address space and data

• Easier to create and destroy than processes

• Better performance for I/O bound applications

Page 16: 1 Processes and Threads Creation and Termination States Usage Implementations.

16

Thread Usage (1)

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

threads sharing the same file

Page 17: 1 Processes and Threads Creation and Termination States Usage Implementations.

17

Thread Usage (2)

A multithreaded Web server

Page 18: 1 Processes and Threads Creation and Termination States Usage Implementations.

18

Thread Usage (3)

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

Page 19: 1 Processes and Threads Creation and Termination States Usage Implementations.

19

Thread Usage (4)

Three ways to construct a server

Page 20: 1 Processes and Threads Creation and Termination States Usage Implementations.

20

Implementing Threads in User Space

A user-level threads package

Page 21: 1 Processes and Threads Creation and Termination States Usage Implementations.

21

(Dis)advantages

+: no specific OS support needed

faster than kernel instructions

process-specific scheduling algorithms

-: blocking system calls (select)

page faults

Page 22: 1 Processes and Threads Creation and Termination States Usage Implementations.

22

Implementing Threads in the Kernel

A threads package managed by the kernel

Page 23: 1 Processes and Threads Creation and Termination States Usage Implementations.

23

(Dis)advantages

+: handling blocking and page faults

-: more costly (but recycling threads)

Page 24: 1 Processes and Threads Creation and Termination States Usage Implementations.

24

Hybrid Implementations

Multiplexing user-level threads onto kernel- level threads

Page 25: 1 Processes and Threads Creation and Termination States Usage Implementations.

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)

Page 26: 1 Processes and Threads Creation and Termination States Usage Implementations.

26

Pop-Up Threads

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

Page 27: 1 Processes and Threads Creation and Termination States Usage Implementations.

27

Making Single-Threaded Code Multithreaded (1)

Conflicts between threads over the use of a global variable

Page 28: 1 Processes and Threads Creation and Termination States Usage Implementations.

28

Making Single-Threaded Code Multithreaded (2)

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