0% found this document useful (0 votes)
13 views

Process Management

Uploaded by

Tien Anh Do
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Process Management

Uploaded by

Tien Anh Do
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

CHAPTER 3: PROCESS

MANAGEMENT

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 1
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
CONTENT

1. Concepts related to the process


2. Thread
3. Scheduling process
4. Synchronize Simultaneous Processes
5. Gridlock and hunger

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 2
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
1. What is Process?

▪ Process is a program that is in the process of being


implemented.
Program Process
Static entities Dynamic Entities

Do not own specific resources Given some resources to store


process and execute commands
▪ Process is generated when a program is loaded into
memory for execution.
▪ User Process
▪ System process
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 3
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
2. Status of the process
▪ Differentiated by 2 states: running and not running
▪ => Not fully reflecting information about the process status
▪ => 5-state model: new, ready, running, waiting, finished.
▪ Newly created: the process is being
created
Điều độ CPU
▪ Ready: the process is waiting for the
Mới Sẵn Kết
CPU to execute its command khởi sàng
Chạy
thúc
tạo
▪ Run: the command of the process Ngắt

executed by the CPU Kết thúc


vào/ra
Vào/ra hoặc
chờ sự kiện

▪ Waiting: the process of waiting for an Chờ


đợi
event to happen (blocked)
▪ End: the process has finished its Sơ đồ chuyển đổi trạng thái của tiến trình
implementation but has not yet been
deleted
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 4
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
2. Status of the process
▪ Initialization -> ready: the initialization
process is complete and has been loaded
into memory, just waiting for the CPU to
be leveled to run.
Điều độ
▪ Ready -> run: due to the CPU scheduling CPU
Mới Sẵn
result of the OS, the process is allocated khởi sàng
Chạy Kết
thúc
CPU by the OS and enters the running tạo
Ngắt
state Kết thúc Vào/ra hoặc
vào/ra chờ sự kiện
▪ Run -> Ready: The OS allocates the CPU Chờ
to another process, due to the đợi

schedul/interrupt result, the current process


goes to the ready state and waits for the Transition diagram of the state of the process
CPU to be provisioned to run again.
▪ Run -> wait: When the process only runs
when an event occurs, switch to process
waiting for the CPU to be allocated to
continue running.
▪ Run-> end: when process has been done.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 5
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Information describing the process
▪ In order to be able to manage the process, the OS needs to
have information about that process. The information of the
process is stored in a data structure called the Process
Control Block (PCB))
▪ Main information in PCB:
▪ Process Identifier (PID)
▪ process Status: one of five states
▪ Contents of some CPU registers:
▪ Command pointer register: points to the next command
▪ Stack pointer register: saves parameters/function status when executing a
function call/procedure of the program
▪ Conditional and state registers: contain the state after performing
logical/arithmetic operations
▪ Universal registers
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 6
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Information describing the process
The reason for saving these registers in the PCB is that the process can be moved
out of the running state to make room for another process (such as when there is
a break). When the process resumes, the OS will use the information from the
PCB to restore the contents of the registers, allowing the process to resume from
the state it was in before it stopped.
▪ Information for process scheduling: priority of the process, location
in the queue, amount of resources owned, etc.
▪ Information about the memory of the process: where it is located in
memory
▪ List of other resources: open files, incoming and outgoing devices
that the process uses
▪ Statistics for management: CPU usage, time limit

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 7
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
4. Tables and process lists

▪ To manage, the OS needs to know the location of the PCBs.


The OS uses a process board that contains pointers to the
PCB of the entire process present in the system
▪ In addition, PCBs of processes in the same state or waiting
for a resource are linked into a list, each of which contains
several processes in the same state.
Đang chạy PCB
Bảng tiến trình
Con trỏ tới PCB 1
bảng tiến trình
Tiến trình 1

Tiến trình 2
Sẵn sàng PCB PCB PCB
Tiến trình 3

….
Tiến trình n PCB n Chờ đợi đọc đĩa PCB PCB

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 8
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
5. Operations with the process

Process management activities include a number of tasks


such as creating new processes, ending processes, switching
between processes, scheduling, synchronization, ensuring
communication between processes.
1. Create a new process:
To create a new process, the OS takes several steps as follows:
▪ Assign an identifier number to the newly created process
and create a cell in the process table
▪ Create memory space for process and PCB
▪ PCB Initialization: The OS assigns the values of the
components of the PCB
▪ Link the PCB of the process to the management lists, for
example to the list of running processes.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 9
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Operations with the process
2. End of process:
▪ Normal termination: asks the OS to terminate itself by
calling the system exit() call.
▪ Terminated:
▪ Ended by the father's process
▪ Due to errors
▪ Requires more memory than the system can provide
▪ Longer than the time limit
▪ Terminated by system administration or operating system

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 10
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Operations with the process
3. Transition between processes:
▪ During execution, the CPU can be converted from the
current process to execute another process.
▪ The information about the current process (contained in the
PCB) is called the context of the process. The transition
between processes is also known as context switching
▪ The process transition occurs when:
▪ Interrupted: interrupted by clock/shut in/out
▪ System call process.
▪ Before moving on to another process, the context is
saved to the PCB
▪ When the CPU is allocated again, the context is restored
from the PCB to the corresponding registers and boards.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 11
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Operations with the process

3. Transition between processes:


▪ What information must be updated and saved to the PCB
when transferring the process? => Depending on each
case:
▪ Simple case: The system switches to in/out and then
returns to continue the process:
▪ The context contains information that can be interrupted by the
processing function
▪ => log content, CPU status

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 12
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
I. CONCEPTS RELATED TO THE PROCESS
3. Operations with the process

▪ Complicated case: After performing the break, the


system performs another process.
▪ Change the process status
▪ Updating Statistical Information in PCB
▪ Move the PCB link of the process to the list corresponding to
the new state
▪ PCB update of the newly selected process
▪ Update register contents and CPU status.
▪ => Transforming the process takes time

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 13
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
1. Concept

▪ The process is considered from 2 aspects:


▪ The process is 1 unit that owns the resources
▪ The process is 1 unit that performs the calculation and processing
work.
▪ Previous OS: each process corresponds to only 1 unique
processing unit
▪ => The process cannot perform more than one task at a time

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 14
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
1. Concept
▪ Modern OS: allows for the separation of the command
execution role of the process
▪ Each unit that executes a process's instruction, i.e., 1 CPU-
allocated instruction string for independent execution is
called an execution thread
▪ The current operating system usually supports multithreading
(multithreading) => allowing multiple command strings to be
executed at the same time

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 15
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
2. Resources of Processes and Thread

▪ In the system that allows multi-threading, the


process is still 1 unit for the OS to distribute
resources
▪ Each process shares a number of resources:
▪ Memory space of the process (logic): contains the
program (commands), the data part of the process.
▪ Other resources: open files, devices, or input/output ports

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 16
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
2. Resources of Processes and Thread

▪ At this point, there is a difference between a single-threaded process


and a multi-threaded process.
▪ Single-threaded model:
▪ The process has a PCB management block that contains
full information on the process status and register values
▪ The stack contains parameters,
functions/procedures/subprograms
▪ As the process processes, it masters the contents of the
registers and command pointers

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 17
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
2. Resources of Processes and Thread
▪ Multi-threaded model:
▪ Each thread needs to be able to manage the command
pointer, register content
▪ Threads also have their own state
▪ => contained in the thread management block
▪ Threads also need to have their own stack
▪ All threads of a process that shares memory space and
resources
▪ Threads have the same address space and have access to
process data

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 18
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
2. Resources of Processes and Thread

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 19
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. Thread
3. Advantages of the multi-threaded model

▪ Increased performance and time savings


▪ Easily share resources and information
▪ Increase responsiveness
▪ Take advantage of a processing architecture with
multiple CPUs
▪ Favorable for organizing the program

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 20
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. Thread
4. Multiplication and user-level thread

▪ Thread can be created and managed at 2 levels:


▪ User level
▪ Kernel level.
▪ User-level streams: created and managed without
OS support
▪ Multiplier level thread: created and managed by OS

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 21
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
4.1. User-level

▪ Generated and managed by the app itself


▪ The OS is unaware of the existence of such
threads
▪ Use libraries provided by programming
languages
▪ The OS still treats the process as a single unit
with a single state
▪ CPU distribution is done for the whole process

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 22
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
4.1. User level

▪ Advantages:
▪ Switching threads does not require switching to kernel
mode => saves time
▪ The application can be moderated according to its own
characteristics, regardless of the moderation of the OS
▪ Can be used on OS that do not support multi-threading

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 23
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
4.1.User level

▪ Disadvantages:
▪ When a thread calls a system call and is blocked, the
entire process is blocked
▪ => does not allow to take advantage of the
responsiveness of the multi-threaded model
▪ Doesn't allow taking advantage of multi-CPU
architectures because the OS distributes CPUs for the
whole process, not for specific threads

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 24
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
4.2. Kernel level

▪ The kernel level thread is created and managed by the OS


▪ The OS provides a programming interface: consists of system calls
that the application can request to create/delete a thread and change
the parameters related to the thread management
▪ Increase the responsiveness and concurrency of threads in the same
process
▪ Create and convert streams performed in multiply mode => slow
speed
▪ Windows and Linux operating systems support kernel-level thread

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 25
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. THREAD
4.3. Combination

▪ User-level thread and kernel-level thread can be


used
▪ According to these organizations:
▪ User-level threads are created in user-mode thanks to
the app's library
▪ User-level threads are mapped to a corresponding
number or fewer than kernel-level threads
▪ The number of kernel-level threads depends on the
specific system, for example, a CPU-rich system will
have more kernel-level threads.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 26
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
II. Thread
4.3. Combination

chế độ chế độ
người dùng người dùng
chế độ chế độ
nhân nhân

a) Mô hình mức người


dùng P

b) Mô hình mức nhân

LUỒNG mức người dùng


chế độ
người dùng
chế độ LUỒNG mức nhân
nhân

P tiến trình

c) Mô hình kết hợp

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 27
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
1. Concept

▪ In a system that allows multiple programs, multiple


processes can exist and execute at the same time.
▪ Multi-program technology has many advantages because it
allows efficient CPU usage, meeting the computing needs of
users
▪ However, posing more complex problems for the OS

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 28
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
1. Concept

▪ Scheduling or scheduling is the decision of which processes


use hardware resources, when, and for how long
▪ Focus on CPU moderation
▪ => Decide the order and timing of CPU usage
▪ Progress scheduling and thread scheduling:
▪ Previous system: the process is the main executing unit =>
scheduling the execution with the process
▪ Threading support system: the core-level thread is the unit that
performs the CPU-level, not the process.
▪ => Use the term process scheduling stream scheduling broadly

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 29
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
2. Types of scheduling
1. Long-term and short-term scheduling:
▪ Long-term scheduling:
▪ Implement when you first create a process
▪ Will the OS decide the process be added to the active list?
▪ If accepted, the system will have a new process. Conversely, wait until
another time to create and implement
▪ Impact on multi-program levels.
▪ Mid-term scheduling : Điều độ
dài hạn
▪ Decision on the process of granting Điều độ ngắn hạn
MEM to implement?
Mới Sẵn Kết
Chạy
▪ Short-term scheduling: khởi
tạo Điều độ
sàng thúc
trung hạn
▪Decide which process is allocated
CPU to execute
Chờ
▪ Execute with process in a ready đợi

state
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 30
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
2. Types of scheduling
2. Scheduling with redistribution and non-
redistribution:
▪ Scheduling with redistribution:
▪ OS can use a kill mechanism to reclaim the CPU of a process
that is in a running state.
▪ Scheduling with non-redistribution:
▪ Processes that are in the running state will be CPU used until
one of the following scenarios occurs:
▪ Closing process
▪ The process must go to a waiting state due to I/O execution.
▪ => Cooperative Scheduling: because it is only possible when
the process is cooperative and the CPU is ceded.
▪ If the process is not cooperative, use infinite CPU => other
processes that are not CPU-provisioned (Windows 96, NT)
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 31
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
2. Types of scheduling

2. Scheduling with redistribution:


▪ Compared to non-redistributive scheduling, redistributed
scheduling has more advantages.
▪ OS is more proactive, independent of the operation of the
process
▪ Ensuring real time sharing
▪ Requires hardware with a timer and some other support
▪ The problem of managing the process is more
complicated

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 32
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
3. Criteria
Some commonly used criteria:
1. Amount of process completed:
▪ Number of completed processes in 1 unit of time
▪ Measure the effectiveness of the system.
2. CPU Usage Performance:
▪ Try to let the CPU take as few breaks as possible
3. Average life cycle time of the process:
▪ From the time of the request to create the process to the end.
4. Waiting time:
▪ The total time the process is in the ready and waiting state for
CPU provisioning
▪ Direct Effect of Process scheduling Algorithm
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 33
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
3. Criteria

5. Response time:
▪ This is a user-oriented criterion and is often used in live
interaction systems.
6. Predictability:
▪ The life cycle, waiting time, response time must be stable,
independent of the load of the system.
7. Fairness:
▪ Processes of the same priority must be treated equally

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 34
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
1. First-come, first-served (FCFS) algorithm:
▪ The process that requires the CPU first will be provisioned
first
▪ OS puts ready processes in the FIFO queue
▪ New process is placed at the bottom of the queue
▪ Simple, fairness
▪ Large average wait time
▪ => Great impact on the overall performance of the whole
system
▪ Usually the scheduling algorithm does not redistribute, after
the process is granted CPU, it will use the CPU until it
finishes or must be stopped to wait for the output.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 35
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
1. First-come, first-served (FCFS) algorithm:
Give 3 processes with the order of occurrence and
CPU cycle length as follows:

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 36
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
1. First-come, first-served (FCFS) algorithm:
Give 3 processes with the order of occurrence and
CPU cycle length as follows:

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 37
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
2. Round robin (RR):
▪ Modification of FCFS for timeshare systems
▪ There is an additional redistribution mechanism using the
interrupt of the meter
▪ The system defines small intervals of time called
quantum/slice time t
▪ When the CPU is freed, OS sets the clock's time in
quantum length, takes the process at the top of the queue,
and grants the CPU to it
▪ The process ends before the time expires: return control
to the OS
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 38
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
2. Round robin:
▪ The quantum of time is out and the process is not over:
▪ Interrupt Birth Clock
▪ The ongoing process has been stopped
▪ Control passes to OS's interrupt handling function
▪ OS moves the process to the end of the queue, takes the process
at the beginning, and continues.
▪ Improved response time compared to FCFS
▪ The average wait time is still long
▪ Time Quantum Length Selection?

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 39
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
2. Round robin

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 40
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
3. Shortest process Priority (SPF):
▪ Select in the process queue with the next shortest CPU usage cycle
to distribute the CPU
▪ If there are multiple processes with equal next CPU cycles, select
the process that comes first
▪ The average wait time is much smaller than that of FCFS
▪ Difficult to do because you have to know the length of the next
CPU cycle:
▪ In batch processing systems: based on the maximum registration time
provided by the programmer
▪ Predict the next CPU cycle length: based on the average length of previous
CPU cycles.
▪ No redistribution
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 41
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
3. Shortest process Priority (SPF)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 42
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
4. Shortest remaining time priority scheduling:
▪ SPF has an additional redistribution mechanism (SRTF)
▪ When a new process appears in the queue, the OS compares the
remaining time of the running process to the remaining time of the
new process
▪ If a new process appears with a shorter remaining time, the OS
reclaims the CPU of the running process, distributing it to the new
process
▪ Small average wait time
▪ The OS must predict the CPU cycle length of the process
▪ Less process switching than RR

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 43
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm
4. Shortest remaining time priority (SRTF)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 44
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm

5. Scheduling has priority:


▪ Each process has 1 priority
▪ The process with a higher priority will be granted the
CPU first
▪ Processes with equal priority are scheduled under the
FCFS
▪ Priority levels are determined according to various
criteria

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 45
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
III. Process scheduling
4. Algorithm

5. Scheduling has priority

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 46
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
▪ In systems that allow multiple processes/lines to coexist,
due to the direct or indirect relationship between
processes, many complex problems can arise compared
to the case of only one process.
▪ Coexistence processes are called concurrent
processes/conflicting processes.
▪ Simultaneous process management is an important issue:
▪ Communication between processes
▪ Compete and share resources
▪ Coordinate and synchronize processes
▪ The problem of deadlock
▪ Starvation
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 47
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes

a. The process of competing for resources with each


other:
▪ Even if the processes do not know about each other's
existence, there is still an indirect relationship between
the processes and can be affected by the same need to
use some resources such as memory, disks, and
peripherals that support I/O.
▪ Consider the case where two processes have the same
need to use the same resource:
• Although each process does not exchange information with
the other, because only one process is allocated resources =>
the other process will have to wait, affecting the
implementation time => affected by the competitive process.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 48
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes
For the process of resource competition with each other, it is
necessary to solve some of the following problems:
▪ Ensuring mutual exclusion:
▪ When processes access a resource together, the resource's
ability to share is limited
▪ Mutual exclusion => must ensure that one process is accessing
a resource when another process is not accessing that resource.
▪ That resource is called a critical resource. Program sections
that require the use of critical resources are called critical
sections
▪ There is only 1 process in danger = > mutually exclusive
▪ To solve this problem, it is necessary to build a mechanism that
allows the coordination of the operation of the system and the
process itself
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 49
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes

a. The process of resource competition with each


other:
▪ Do not let deadlocks occur:
• Ensuring mutual exclusion can cause a deadlock => Deadlock:
a situation in which two or more processes cannot be carried
out further due to mutual waiting.
• For example, there are two processes, P1 and P2, each of which
needs to be allocated resources T1 and T2. Due to the allocation
results of the OS, P1 is allocated T1 and P2 is granted T2. P1
will wait for P2 to release T2, P2 will wait for P1 to release T1
to be able to continue. Results P1 and P2 fell into a deadlock.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 50
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes

a. The process of resource competition with each


other :
▪ No starvation:
• Waiting too long without taking your turn to use resources
• For example, the three processes P1, P2, and P3 have the same
repetitive request for a certain resource. Due to the
mutualexclusion results and the way resources are allocated,
P1 and P2 are resourced multiple times, respectively, while P3
never has a => turn to continue without a deadlock.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 51
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes
2. The process of collaborating with each other
through common resources:
▪ Information can be exchanged by sharing shared
memory (global variables), or files.
▪ Concurrent processes accessing shared data raises
a number of issues:
• Requires mutual exclusion assurance
• There is a deadlock and hunger
• Data consistency requirements

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 52
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes
b. The process of collaborating with each other
through common resources:
▪ Race condition: a situation in which several
lines/processes read and write data are used in general
and the result depends on the order of the read and
write operations
▪ => Put shared data access and update operations in the
danger section and use mutual exclusions to ensure
they are not interrupted by other processes.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 53
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
1. Problems with concurrent processes

c. The process is communicated by sending


messages:
▪ Cooperation processes can exchange information
directly with each other by sending messages
(message passing).
▪ The communication mechanism is supported by a
library of programming languages or operating
systems.
▪ No mutual exclusion requirements
▪ There may be deadlock and hunger
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 54
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
An important requirement when synchronizing the
process is to solve the problem of critical passages and
eliminate mutuals.
The solution to the critical section problem needs to
satisfy the following requirements:
▪ Mutual exclusion: If multiple processes have a critical
section for the same resource, only one process is in the
critical section at a time.
▪ Procession conditions: A process being carried out
outside the critical section is not allowed to prevent other
processes from entering its critical section. For example,
if a process is suspended outside the critical section, it is
not allowed to affect the other process.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 55
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
2. Request with a solution for the critical section

▪ Limited waiting: If the process needs to enter the critical


section, it must be entered after a certain limited period of
time. This means that no deadlock or starvation are
allowed for the process.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 56
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
2. Request with a solution for the critical section

▪ In addition, the solution to the critical section


problem is built based on the following
assumptions:
▪ Hypotheses:
▪ The solution does not depend on the speed of the
processes
▪ No process is allowed to sit for too long in the critical
section
▪ Reading and writing memory operations are atomic
operations and cannot be interrupted in the middle

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 57
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
2. Request with a solution for the critical section

▪ The solutions are divided into 3 main groups:


▪ Software solutions:
• User/programmer does it themselves (usually with the support
of programming libraries)
• The OS provides a number of tools (functions and data
structures that support programmers via system calls)
▪ Hardware solutions
• Based on some special machine commands (Interrupt disable,
test-and-set)
▪ Using the support of OS or programming language
libraries

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 58
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
3. Peterson Algorithm
▪ The software solution does not require hardware or
operating system support.
▪ Initial proposal for synchronizing 2 processes.
▪ P0 and P1 perform simultaneously with a common
resource and a common critical passage
▪ Each process is infinitely executed and alternates between
the critical section and the rest of the process
▪ Requires 2 processes of exchanging information through 2
common variables:
▪ Int Turn: determines which turn of the process to enter the critical
section
▪ Flag for each process: flag[i]=true if the ith process requests to
enter the critical section
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 59
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES
SIMULTANEOUSLY
3. Giải thuật peterson (tt)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 60
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
3. Peterson Algorithm
▪ Meet the requirements:
▪ Mutual exclusion conditions
▪ Procession conditions:
▪ P0 can only be prevented by P1 from entering the danger section
if flag[1] = true and turn = 1 is always true
▪ There are 2 possibilities with P1 outside the danger section:
▪ P1 is not ready to enter the danger section => flag[1] = false, P0 can enter
the danger section immediately
▪ P1 has flag[1]=true set and is in a loop while => turn = 1 or 0.
▪ Turn = 0: P0 into the critical section right away
▪ Turn = 1: P1 enters the danger section, then sets the flag[1] = false => returns to
possibility 1.

▪ Limited waiting
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 61
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
3. Peterson Algorithm(tt)

▪ Practical use is relatively complicated


▪ It requires the process of entering the critical
section to be in an active waiting state
▪ Active waiting: the process still has to use the CPU
to check if it can enter the critical section?
▪ => CPU Waste

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 62
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
4. Hardware Solutions

▪ Computer hardware can be designed to solve the


problem of mutual exclusion and critical section.
▪ Hardware solutions are usually easy to use and
have good speeds.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 63
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE SIMULTANEOUS PROCESSES
4. Hardware Solutions
1. Prohibition of interrupts:
▪ In the case of a computer with only one CPU, at a time
only one process is executed
▪ The process that has the CPU will execute until it calls
the operating system service or is interrupted
▪ To handle critical section => prohibits interrupts from
occurring while the process is in the critical section to
access resources
▪ Ensure that the process is carried out in its entirety and is
not interfered with by other processes
▪ Simple
▪ Reduced OS flexibility.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 64
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE SIMULTANEOUS PROCESSES
4. Hardware Solutions(tt)

2. Use special machine commands:


▪ The hardware is designed to have a number of special
machine commands
▪ 2 operations to check and change the value for a variable
(memory cell), or operations to compare and swap the
values of two variables, performed in the same machine
command
▪ => Ensuring that it is done together without interfering in
the middle – atomic manipulation
▪ Called a "check and establish" command – Test_and_Set

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 65
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS
PROCESSES
2. Use special 4. Hardware
machine Solutions(tt):
commands (tt)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 66
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
4. Hardware Solutions (tt)
2. Use special machine commands (tt):
const int n; //n là số lượng tiến trình
bool lock;
void P(int i){ //tiến trình P(i)
for(;;){ //lặp vô hạn
while(Test_and_Set(lock));//lặp đến khi điều kiện không thỏa
<Đoạn nguy hiểm>
lock = false;
<Phần còn lại của tiến trình>
}
}
void main(){
lock = false;
//tắt tiến trình chính, chạy đồng thời n tiến trình
StartProcess(P(1)); …. StartProcess(P(n));
}
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 67
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
4. Hardware Solutions (tt)

2. Use special machine commands(tt):


▪ Advantage:
▪ The use is relatively simple and intuitive
▪ Can be used to synchronize multiple processes
▪ Can be used for multi-processing with multiple CPUs but with
shared memory.
▪ Disadvantage:
▪ Active Waiting
▪ May cause stavation

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 68
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
5. Semaphore

▪ The S is an integer initialized by the concurrent servicing


of a resource
▪ The value of S can only be changed by calling 2
operations, Wait and Signal.
▪ Wait(S):
▪ Reduce S by 1 unit
▪ If the value is S<0, the wait(S) call process will be blocked.
▪ Signal(S):
▪ Increase S to 1 unit
▪ If the value of S≤0:1 in the processes that are being blocked is released and
can be further implemented.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 69
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
5. Semaphore

▪ Wait and Signal are atomic operations


▪ To avoid active waiting, use 2 lockdown and wake up
operations:
▪ If the process of the wait operation is performed, the flag value is
negative, instead of actively waiting, it will be blocked (by the
block operation) and added to the queue of the flag
▪ When there is a process that performs a signal operation, one of
the locked processes will be put into a ready state thanks to the
wakeup operation contained in the signal

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 70
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZE PROCESSES SIMULTANEOUSLY
5. Semaphore(tt)
▪ The semaphore is used by the process to send signals
before entering and after exiting the critical section
▪ When the process needs to access resources, perform the
Wait operation of the corresponding semaphore.
▪ Negative semaphore value:
▪ Resources are used to their full capacity
▪ The Wait implementation process will be blocked until the resource is freed
up
▪ If another process performs Wait on the flag, the semaphore value will
decrease further
▪ The absolute value of the negative semaphore corresponds to the number of
blocked processes.
▪ After the resource is used up, the process performs the Signal
operation on the same semaphore: increases the value of the
semaphore and allows a blocked process to continue
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 71
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
5. Semaphore (tt)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 72
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
5. Semaphore (tt)
const int n; //n là số lượng tiến trình
semaphore S = 1;
void P(int i){ //tiến trình P(i)
for(;;){ //lặp vô hạn
Wait(S);
<Đoạn nguy hiểm>
Signal(S);
<Phần còn lại của tiến trình>
}
}
void main(){
//tắt tiến trình chính, chạy đồng thời n tiến trình
StartProcess(P(1));
...
StartProcess(P(n));
}

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 73
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
6. Some synchronous problems
1. The problem of philosophers eating rice:
▪ 5 philosophers sit on chairs around a round table
▪ There are 5 chopsticks on the table: 1 on the right and 1 on the left
▪ The philosopher can pick up two chopsticks in any order: one by
one and the chopsticks are not in someone else's hand
▪ When both chopsticks are held: the philosopher begins to eat and
does not put the chopsticks in place during the meal
▪ After eating, the philosopher put 2 chopsticks on the table
▪ => 5 philosophers like 5 simultaneous processes with the
dangerous resource being chopsticks and the dangerous part being
the part using chopsticks to eat
▪ The flag that allows solving the problem is as follows:
• Each chopstick is represented by 1 flag
• Pick up chopsticks: wait(); Put the wand down: signal()
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 74
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
6. Some synchronous problems (tt)
1. The problem of philosophers eating rice:
semaphore chopstick[5] = {1,1,1,1,1};
void Philosopher(int i){ //tiến trình P(i)
for(;;){ //lặp vô hạn
Wait(chopstick[i]); //lấy đũa bên trái
Wait(chopstick[(i+1)%5]); //lấy đũa bên phải
<Ăn cơm>
Signal(chopstick[(i+1)%5]);
Signal(chopstick[i]);
<Suy nghĩ>
}
}
void main(){
// chạy đồng thời 5 tiến trình
StartProcess(Philosopher(0));
...
StartProcess(Philosopher (4));
}
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 75
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
6. Some synchronous problems (tt)

2. The problem of producers and consumers with limited


buffers:
▪ Producer: creates the product, puts it in a place called a buffer, 1
product at a time
▪ Consumer: take the product from the buffer, 1 product at a time,
for use
▪ Limited buffer capacity, containing up to N products

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 76
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
6. Some synchronous problems (tt)

2. The problem of producers and consumers with limited


buffers:
▪ Sync Requirements:
1. Producers and consumers may not use buffers at the same time
2. When the buffer is empty, the consumer should not try to grab the product
3. When the buffer is full, the manufacturer must not add products.
▪ Solving with Semaphore:
1. Requirement 1: use the lock semaphore initialized with 1
2. Requirement 2: empty semaphore, initialized with 0
3. Requirement 3: full semaphore, initialized with N

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 77
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
6. Some synchronous problems (tt)
2. The problem of producers and consumers:
Const int N; // kích thước bộ đệm Semaphore empty = 0;
Semaphore lock = 1; Semaphore full = N
Void producer () { Void consumer() {
for (; ;) { for (; ;) {
<sản xuất> wait (empty);
wait (full); wait (lock);
wait (lock); <lấy 1 sản phẩm từ bộ đệm>
<thêm 1 sản phẩm vào bộ đệm> signal (lock);
signal (lock); signal (full);
signal (empty); <tiêu dùng>
} }
} }
Void main() {
startProcess(producer);
startProcess(consumer);
}
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 78
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor
▪ Defined as an abstract data type of a higher-order
programming language
▪ Includes a separate data, an initialization function, and
some function or method for accessing the data:
▪ Process/flow can only access the monitor's data through the
monitor's functions or methods
▪ At each time:
▪ Only one process is performed in the monitor
▪ The other process called the monitor's function to be blocked, queued up the
monitor to wait until the monitor was released.
▪ => Ensuring mutual exclusion of dangerous sections
▪ Put dangerous resources inside the monitor

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 79
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)

▪ The process is in progress in the monitor and is stopped


to wait for the event or some condition to be satisfied
=> returns the monitor for another process to use.
▪ The waiting process will be restored from the stop once
the waiting condition is satisfied
▪ => Using Conditional Variables
▪ Declared and used in the monitor with 2 operations:
cwait() and csignal()

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 80
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)

▪ x.cwait():
▪ The process is in the monitor and calls the cwait blocked until
condition x occurs
▪ The process is queued by the condition variable x
▪ The monitor is released and another process will be entered.
▪ x.csignal():
▪ The process of calling csignal to notify that condition x has been
satisfied
▪ If there is a process that is being blocked and is in the queue of x
due to the previous call to x.cwait() it will be freed
▪ If there is no blocked process, the csignal operation will have no
effect at all
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 81
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)
Monitor

Dữ liệu cục bộ

condition x hàng đợi điều kiện


các biến điều kiện

condition y

hàng đợi chung của monitor

hàm hàm hàm

hàm khởi tạo

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 82
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)
monitor BoundedBuffer
product buffer[N]; //bộ đệm chứa N sản phẩm kiểu product
int count; //số lượng sản phẩm hiện thời trong bộ đệm
condition notFull, notEmpty; //các biến điều kiện
public:
boundedbuffer( ) { //khởi tạo
count = 0;
}
void append (product x) {
if (count == N)
notFull.cwait ( ); //dừng và chờ đến khi buffer có chỗ
<Thêm một sản phẩm vào buffer>
count++;
notEmpty.csignal ();
}

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 83
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)

product take ( ) {
if (count == 0)
notEmptry.cwait (); //chờ đến khi buffer không rỗng
<Lấy một sản phẩm x từ buffer>
count --;
notFull.csignal ( );
}
}

void producer ( ) { //tiến trình người sản xuất


for (;;){
<Sản xuất sản phẩm x>
BoundedBuffer.append (x);
}
}

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 84
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
IV. SYNCHRONIZATION OF SIMULTANEOUS PROCESSES
7. Monitor (tt)

void consumer ( ) { //tiến trình người tiêu dùng


for (;;){
product x = BoundedBuffer.take ();
<Tiêu dùng x>
}
}
void main() {
Thực hiện song song producer và consumer.
}

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 85
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. DEADLOCK
1. Definitions
▪ One problem that makes it difficult for the processes at
the same time is the deadlock.
▪ A deadlock is a situation in which a process group that
competes for resources or has cooperation has to stop
indefinitely
▪ Because the process has to wait for an event that can
only be generated by another process is also in a waiting
state

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 86
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. DEADLOCK
1. Definitions
▪ Example:
▪ The two processes P and Q are carried out simultaneously
▪ Each process needs to use two resources X and Y
simultaneously for a certain amount of time
▪ Resources X and Y are only capable of serving one process
at a time
▪ To use resources, each process needs to perform a resource
request operation.
▪ After this is done, the process releases the resource using
the Release operation.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 87
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. DEADLOCK
1. Definitions

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 88
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
2. Conditions for deadlock occurrence

▪ The deadlock occurs when the following 4 conditions


are satisfied at the same time:
1. Mutual exclusion: there is a dangerous resource, at a single
time in the process of use. Other processes that require this
resource will have to stop waiting for the resource to be freed.
2. Hold and wait: the process of holding a resource while
waiting, such as waiting for another resource to be granted.
3. No preemption: the resource held by the process cannot be
redistributed to another process unless the holding process
voluntarily releases the resource
4. Waiting for the circle: there exists process group P1, P2, ...,
Pn so that P1 waits for the resource held by P2, P2 waits for the
resource held by P3, ..., Pn waits for the resource held by P1.
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 89
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
2. Conditions for deadlock occurrence (tt)

▪ Solve the deadlock problem in a way that:


▪ Deadlock prevention: ensuring that one of the four
conditions for a deadlock is never satisfied
▪ Deadlock avoidance: allows certain deadlock
conditions to be satisfied but ensures that the deadlock
is not reached
▪ Deadlock detection: allows deadlocks to occur, detect
deadlocks, and restore the system to a non-deadlock
condition

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 90
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
3. Preventing deadlocks
▪ Make sure at least 1 of the 4 conditions do not
occur
▪ Mutual exclusion: Mutual exclusion can be
avoided with resources that allow multiple
processes to be used at once, such as files in
read mode. However, in fact, there is always a
resource that is not capable of sharing at the
same time => cannot prevent the condition of
mutual exclusion
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 91
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
3. Preventing deadlocks
▪ Hold and wait: There are two ways to prevent this
condition:
▪ Option 1:
▪ Process requirements must receive all necessary resources before
proceeding
▪ If you don't get enough, the process is blocked to wait until you can
get enough resources.
▪ Option 2:
▪ Progression can only request resources if no other resources are kept
▪ Before requesting additional resources, the process must release the
granted resource and re-request it (if necessary) along with the new
resource

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 92
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
3. Deadlock Prevention (tt)

▪ No redistribution:
▪ Option 1:
▪ When a process requests resources but can't because they've been
allocated, the OS reclaims all the resources it holds
▪ The process can only be continued after the old resource is obtained
along with the new required resource.
▪ Option 2:
▪ When the progress requires resources, if available, they will be allocated
immediately
▪ If a resource is held by another process and this process is waiting to
grant more resources, it will be withdrawn to grant the requested process
▪ If both of the above conditions are not met, the process of requesting
resources must wait
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 93
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
3. Deadlock Prevention (tt)

▪ Circle waiting:
▪ Define the order for resource types and allow the resource request
process only so that the resource that the next request process has
an order greater than the resource it requested first
▪ Suppose in the system there are n types of symbol resources R1,
R2, ..., Rn
▪ Suppose these types of resources are arranged in ascending order
of the index
▪ If the process has requested some Ri-type resource, then the
process is only allowed to request an Rj resource if j > i
▪ If a process needs multiple resources of the same type, it must
request all resources of that type at the same time
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang 94
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. deadlock
4. Preventing Deadlocks
▪ Deadlock prevention solutions focus on using
rules or constraints to prevent deadlocks.
Disadvantages: make the use of resources
inefficient, reduce the efficiency of the process.
▪ Preventing deadlocks:
▪ Use rules or constraints when allocating resources to
prevent deadlocks
▪ Inefficient use of resources, reducing process
efficiency

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 95
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing Deadlocks
▪ Preventing Deadlocks:
▪ Allow the first 3 conditions to occur and only ensure
that the deadlock is never reached
▪ Each resource request of the process will be reviewed
and decided according to the specific situation
▪ OS Request Process provides information about
resource usage (maximum number of process
resources to be used)

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 96
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm

▪ Lender algorithm because of the similarity between


deciding to lend money and allocating resources in a
computer.
▪ A good lender is a person who can lend a lot
▪ However, when lending in excess of the actual
amount of money, there will be a risk because each
borrower cannot borrow enough money to serve the
business and therefore, both the bank and the lender
are in a deadlock.

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 97
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm
▪ The OS requires the process of providing information about the
use of its resources and using this information when allocating
▪ When the process wants to initialize, the resource type and the
maximum number of resources for each type will be required
▪ If the number of requests does not exceed the system capacity,
the process will be initiated
▪ Status is determined by the current resource usage status in the
system:
▪ The maximum number of resources that the process requires:
▪ As a matrix M[n][m]: n is the number of processes, m: number of resources
▪ M[i][j]: the maximum number of resources of form j that process i requires

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 98
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm

▪ Number of resources remaining:


▪ As a vector A[m]
▪ A[j] is the number of j-type resources remaining and can be allocated.
▪ Amount of resources granted per process:
▪ As a D[n][m] matrix
▪ D[i][j] is the amount of j-type resources granted to process i.
▪ The amount of resources that still need to be allocated:
▪ As a matrix C[n][m]
▪ C[i][j]=M[i][j]-D[i][j] is the amount of j-type resources that process i still
needs to allocate

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang 99
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm

▪ Safe state: a state from which there is at least one


allocation plan so that a deadlock does not occur
▪ How to prevent a deadlock:
▪ When a process requires a resource to be granted, the system
assumes that the resource is granted
▪ Update the status again & determine if it's safe?
▪ If it's safe, the resources will be realized.
▪ In contrast, the process is blocked and waits until it can be safely
dispensed

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm
▪ Secure Status Determination Algorithm:
1. Declare the W array of size m and the F array of size n. (F[i] contains the status of
whether the ith process has ended, W contains the remaining resources)
Initiating W=A and F[i]=false (i=0,...,n-1)
2. Find it for:
F[i] = false and C[i][j] W[j] with every j=0,...,m-1
If there is no such i then go to step 4
3. a) W = W + D[i]
b) F[i] = true
c) Go back to step 2
4. If F[i] = true for every i =0,...,n-1 then the safe state
Else unsafe status

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
4. Preventing impasse – lender algorithm
▪ The system has 3 types of resources X, Y, Z
X Y Z
with the initial number X=10, Y=5, Z=7 P1 7 5 3
M=
▪ 4 processes P1, P2, P3, P4 have the maximum P2 3 2 2
P3 9 0 2
resource requirements for the table P4 2 2 2

▪ Consider the following state of the system: Yêu cầu tối đa

▪ Is a safe state C=M-D


X Y Z A= X Y Z
▪ Is it possible to find out how to allocate
P1 0 1 0 P1 7 4 3
X Y Z
P2 1 2 2
▪ leading to a deadlock, e.g., P2,P2P4, P1,
2 P3
0 0 3 3 4
P3 6 0 0
P3 3 0 2
D= Còn lại
P4 0 1 1
P4 2 1 1
Đã cấp Còn cần cấp

▪ P1 requires the allocation of 3 Y-type resources, i.e. request = (0,3,0). If the


request is satisfied, the system switches to the following status: X Y Z
▪ Unsafe Status X Y Z
P1
P2
7
1
1
2
3
2
3 0 4
▪ => request (0,3,0) is denied Còn lại
P3 6 0 0
P4 0 1 1
Còn cần cấp
www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
5. Deadlock Detection and Handling

▪ The system does not take preventive/preventive measures


= > possible deadlock
▪ The system periodically checks to detect whether there is a
deadlock or not?
▪ If it does, the system will process it to restore the
deadlock-free state

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
5. Deadlock Detection and Handling (tt)

▪ Deadlock Detection:
▪ In case each resource type has only one resource => use a graph
that represents the relationship between the two waits in the
middle of the process
▪ Build a resource allocation graph:
▪ Nodes are progress and resources
▪ A resource is connected to a process by a
directed arc if the resource is granted to that
process
▪ A process is connected to a resource with a
directional arc if the process is being allocated
that resource

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
5. Deadlock Detection and Handling (tt)
▪ Waiting Graph:
▪ Built from the resource allocation graph by discarding the nodes
corresponding to the resource and entering the arcs that pass through the
abandoned node
▪ Allows the detection of a circular wait progression condition that is sufficient
to generate a deadlockUse a cycle detection algorithm on a directional graph
to detect a deadlock on a waiting graph

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
5. Deadlock Detection and Handling (tt)

▪ When the deadlock was detected:


▪ A deadlock can only appear after a process requires resources and
is not satisfied
▪ => Run the deadlock detection algorithm every time there is an
unsatisfied resource allocation request
▪ => Allows deadlock detection as soon as it occurs
▪ Frequent running degrades system performance
▪ => Reduce the frequency of running the deadlock detection
algorithm:
▪ After each cycle from a few tens of minutes to a few hours
▪ When there are some signs such as CPU usage dropping below a certain
threshold

www.ptit.edu.vn GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH


Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1
V. Deadlock
5. Deadlock Detection and Handling (tt)

▪ Handling when you are stuck:


▪ End all stalled processes
▪ End one by one, each process that is at a standstill until the
deadlock is over:
▪ OS must re-run the deadlock detection algorithm after the end of a
process
▪ OS can choose the order in which the process ends based on certain
criteria.
▪ Restore progress to the point before the deadlock then redo the
process from this point:
▪ Requires the OS to store state so that it can perform rollback and restore
to previous checkpoints
▪ When running again, processes may fall into a deadlock again.
▪ Reclaim resources from deadlocked processes one by one until the
deadlock is over GIẢNG VIÊN: THS NGUYỄN THỊ NGỌC VINH
www.ptit.edu.vn
Trang
BỘ MÔN: KHOA HỌC MÁY TÍNH – KHOA CNTT1

You might also like