596.97K
Category: softwaresoftware

3- Process & Tread model (4)

1.

Ch3 Process & Tread model
NUIST
ZHAO YINGNAN

2.

Process and Thread model
◉ Process
◉ Multiprogramming
◉ Process concept
◉ Process state and switching
◉ PCB
◉ Process queue
◉ Process control
◉ Thread
◉ Why do we use thread?
◉ Thread components
◉ Thread implementation
◉ User-level thread, kernel-level thread, hybrid model
2

3.

1
Process
3

4.

Multiprogramming
◉ In a multiprogramming system there are one or more programs
loaded in main memory which are ready to execute.
◉ Only one program at a time is able to get the CPU for executing its
instructions (i.e., there is at most one process running on the system)
while all the others are waiting their turn.
◉ The main idea of multiprogramming is to maximize the use of CPU
time.
4

5.

Concurrent programs
◉ Concurrent environment
◉ There are two or more programs begin to run, but not ended, at the same
time period with a single CPU. The order is not predetermined
5

6.

concurrent programs
◉ Characteristics of concurrent program execution
◉ Intermittent
◉ Loss of closure
◉ Non-reproducible
6

7.

Process Concept
Abstration of running
program
◉ Process
◉ A process is basically a program in execution
◉ It is the unit of system resource allocation and scheduling by OS
◉ Transform one CPU into multiple virtual CPUs
◉ System resources are allocated in units of processes, such as memory, files, ...
◉ Each process has an independent address space
Q1 What's the
difference and
◉ OS dispatches the CPU to the required processes
relationship
◉ Multiple processes can be related to the same program.
between program
and process?
7

8.

program vs process
8

9.

Process Concept
◉ Process types
◉ System process, user process
◉ Foreground process, background process
◉ I/O bound process, CPU bound process
◉ Process hierarchy
◉ UNIX: Process family tree, init as the root
◉ WINDOWS: Same status
9

10.

Process in Memory
◉ Text: also known as a Code segment, is the section
Process in memory
of memory which contains executable instructions
of a program.
◉ Data: It contains the global and static



variables that are initialized by the
programmer.
BSS: contains all global and static variables
that are initialized to zero or do not have
explicit initialization in source code.
Heap: It is the segment where dynamic
memory allocation usually takes place.
Stack: It contains the program stack. This area
is devoted to store all the data needed by a
function call in a program.
10

11.

Process states and state transitons





In a multitasking computer system, processes may occupy a
variety of states
When a process executes, it passes through different states.
These stages may differ in different OS, and the names of these
states are also not standardized.
Three states
◉ ready, running, waiting/blocked
Five states
◉ new, ready, running, waiting/blocked, terminated
Seven states
◉ new, ready, running, waiting/blocked, blocked suspend,
ready suspend, terminated
11

12.

Three states with their transitions



Ready: The process is waiting
to be assigned to a processor
Running: Instructions are
being executed
Waiting/blocked: The process
is waiting for some event to
occur(such as an I/O
completion or reception of a
signal)
Time slice used up;
Preempted
Ready
Dispatched
Running
Waiting on an event:
Event
completed
Request OS service
Access to resources is not yet
Waiting
available
Waiting for I / O results
Waiting for information from
another process
12

13.

Other states of Process

New------The process is being created



PID, PCB
Terminated------- Process is killed as well as PCB is deleted
Suspended ready-----Process that was initially in the ready state
but were swapped out of main memory and placed onto external
storage by scheduler are said to be in suspend ready state.

Suspended blocked – Similar to suspend ready but uses the
process which was performing I/O operation and lack of main
memory caused them to move to secondary memory
13

14.

Five states and their transitions
Time slice used up;
Preempted
Admit
New
Ready
Dispatched
Event
completed
Release
Running
Terminated
Waiting
for an
event
Waiting
14

15.

Seven states and their transitions
Timeout;
Preempted
Admit
New
Ready
Suspend
Suspended
Ready
Suspended
Waiting
Activate
Dispatched
Event
completed
Release
Running
Terminated
Waiting
on an
event
Activate
Waiting
Suspend
15

16.

PCB






A process handles the operating system activities through PCB
(Process control Block)
A PCB is a data structure used by OS to store all the information
about a process.
It is also known as a process descriptor.
When a process is created (initialized or installed), the operating
system creates a corresponding PCB.
There is a one-to-one correspondence between the process and
the PCB
The process table is an array of PCBs, that means logically
contains PCBs for all the current processes in the system.
16

17.

PCB
Q2: Which information do you
require to manage a process in OS?
17

18.

Components of PCB
The following are the various information that is contained by PCB:

Naming the process

State of the process

Resources allocated to the process

Memory allocated to the process

Scheduling information

Input / output devices associated with process
18

19.

Components of PCB








Prcess ID: In computer system there are various process
running simultaneously and each process has its unique ID.
Process State: It refers the states of the processes.
Program counter: It is used to point to the address of the
next instruction to be executed in any process
Register Information: It is comprising with the various
registers, such as index and stack that are associated with the
process.
Scheduling information: It is used to set the priority of
different processes.
Accounting information: It stores the details relate to
central processing unit (CPU) utilization and execution time of a
process.
I/O information:It stores the details pertaining to resource
utilization and file opened during the process execution
Memory related information: It comprises of page and
segment tables. It also stores the data contained in base and limit
registers.
19

20.

Process Scheduling
CPU switch, context switching, process queue
20

21.

CPU switch from
process to process
21

22.

Context Switching

Context Switching involves storing the context or state of a process so that it can
be reloaded when required and execution can be resumed from the same point as
earlier.

This is a feature of a multitasking operating system and allows a single CPU to be
shared by multiple processes.

Context Switching Triggers

Multitasking: In a multitasking environment, a process is switched out of the
CPU so another process can be run.


Interrupt Handling
User and Kernel Mode Switching
22

23.

Context Switching

When a process is running


its hardware state is stored in registers on the CPU.
Registers

program counter, PSW, stack pointer, values ​of general-purpose registers and
other control registers

When the process is not running


the values ​of these registers are stored in PCB;
when the operating system wants to run a new process, the relevant values ​in the
PCB are sent to the corresponding registers
23

24.

Process Queue



OS creates one or more queues for each type of process
Element of queue is PCB
With the state transitions, its PCB goes from one queue to another
Note:
Events in multiple waiting
queues are different
The ready queue can also
be multiple
Only one process in the
running queue with single
CPU
24

25.

Create, terminate, block, wakeup......
Process Control
25

26.

Process Control


Process management functions are usually placed in the OS kernel
The OS kernel contains two aspects:

Supporting functions


interrupt processing, clock management, primitive operation
Resource management functions

process management, memory management, device
management
26

27.

Process Control


The process control operation completes the transition between the
states of the process by primitives.
Primitives
A primitive is the smallest 'unit of
◉ Process creation
processing' available to a programmer of
◉ Process termination
a given machine, or can be an atomic
◉ Process blocking
element of an expression in a language.
Primitives are units with a meaning, i.e.,
◉ Process wake-up
a semantic value in the language.
◉ Process Suspending
◉ Process activating
atomic
◉ Process Priority
............
27

28.

Process Creation




Assign a unique ID and PCB to the new process
Allocate address space for a process
Initialize the PCB
◉ Set default values (eg: process state is New, ...)
Set the corresponding queue pointer
◉ Example: add new process to the ready queue list
UNIX fork/exec
WINDOWS
CreateProcess
28

29.

Process Termination

Terminate a process
◉ Reclaim resources held by the process


Close open files, disconnect from the network, reclaim allocated
memory, ...
Reclaim the PCB of the process
UNIX exit
WINDOWS
TerminateProcess
29

30.

Process Blocking
A process in the running state expects an event to occur during its
running, such as waiting for the keyboard input, waiting for the disk data
transfer completing, waiting for other processes to send messages.
When the waited event does not occur, it is executed by the process
itself Blocking primitives, changing themselves from running to blocking
UNIX: wait
WINDOWS
WaitForSingleObject
30

31.

2
Thread
31

32.

Thread concept





A thread is a basic unit of CPU utilization; it comprises a thread ID, a program
counter, a register set and a stack. It shares with other threads belong to the
same process its code section, data section, and other OS resources, such as
open files and signals.
A thread is a path of execution within a process.It is also known as lightweight
process.
A process can contain multiple threads. The program starts as a single-threaded
process
The idea is to achieve parallelism by dividing a process into multiple threads.
The primary difference is that threads within the same process run in a shared
memory space, while processes run in separate memory spaces.
32

33.

Thread
Advantages of Thread over Process

Responsiveness: If the process is divided into multiple threads, if one thread
completes its execution, then its output can be immediately returned.

Faster context switch: Context switch time between threads is lower
compared to process context switch. Process context switching requires
more overhead from the CPU.

Effective utilization of multiprocessor system: If we have multiple threads in
a single process, then we can schedule multiple threads on multiple
processor. This will make process execution faster.
33

34.

Introduction of Threads

Why do we introduce threads in the process?

Reasons

Application needs

Cost considerations

Performance considerations
34

35.

Thread-----Application needs
Word processing software with three threads
35

36.

A multi-threaded web server
36

37.

Cost considerations
Process operations:
process creation
process termination
process communication
process switching
---> Need more Time / space,
Limit the increase in
concurrency
Thread: Low consuption of
time/space
Creating a new thread takes less
time (The same is true for
termination)
Switching between two threads
spends less time
Communication between threads
does not need to call for the
kernel (threads in the same
process, share memory and files)
37

38.

Performance considerations

Multiple threads, some calculations, some I/O

Multiple processors
38

39.

User level thread, kernel level thread, hybrid......
Thread implementation
39

40.

User level thread
◉ Create a thread library in user
space:Provide a set of management
◉ Run time system: completion of thread
management (operation,thread table)
◉ Process manages the kernel, and does
not know the existence of the threads
◉ Thread switching does not need the
kernel privileges.
◉ Example: UNIX
40

41.

Kernel level thread
◉ The kernel manages all threads, and
provide API to the application
◉ The kernel maintains the context of
processes and threads
◉ Thread switching requires kernel support
◉ Schedule based on thread
◉ Example: Windows
41

42.

Hybrid model
◉ Thread creation is
done in user space
◉ Thread scheduling is
completed in kernel
mode
◉ Example: Solaris
42

43.

Key points
◉ Process:
◉ multiprogramming, concurrency
◉ Program vs process
◉ Process states and switching
◉ Process components
◉ PCB
◉ Process switch
◉ Process control
◉ Thread
◉ Process vs thread
◉ Thread components
◉ Thread implementing
43

44.

Thanks!
Any questions ?
You can find me at
◉ wechat: Operating System
◉ qq: 957527748
◉ zh_yingnan@126.com
44
English     Русский Rules