Chapter - 04 - Process Management
Chapter - 04 - Process Management
Process concepts
Coordination of processes
Communication between processes
Synchronize processes
Deadlock
Process
The process is a processing program
Each process has an address space, a command pointer, a
separate set of registers and stacks.
The process may require some resources such as CPU,
main memory, files and I/O devices.
The operating system uses a scheduler to determine
when to stop the processing of the processing process
and select the next process to be performed.
In the system, there are processes of the operating
system and the users.
Purpose for multiple processes working
simultaneously
PCB
Operations on processes
Yêu cầu
I/O ds đợi tài nguyên
tài nguyên
Hết
thời gian
Ngắt xảy ra
Đợi 1 ngắt
Types of scheduling
Job scheduling
Select which job is put in the main memory to perform
Determines the degree of multiplication
Low frequency of operation
Process scheduling
Select a process available (loaded in main memory, and
have enough resources to run) to allocate CPU for that
process to execute.
Have a high operating frequency (1 time / 100 ms).
Use the best algorithms
Scheduling algorithms
FIFO algorithm
Round Robin algorithm
Priority algorithm
Shortest-job-first algorithm (SJF)
Multiple priority algorithm
Lottery Scheduling Strategy (Lottery)
FIFO algorithm
Preemptive Scheduling
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Quantum: 4 milliseconds
P1 P2 P3 P1
0 1 4 7 30
Purpose:
to share information such as file sharing, memory, ...
cooperating to complete the job
Mechanisms:
Communication by signal (Signal)
Communication by pipe (Pipe)
Communication via shared memory (shared memory)
Communication by message (Message)
Communication by socket
Communication by signal (Signal)
Signal Describe
SIGINT Users press Ctrl-C to stop processing the process
SIGILL Process execute an invalid instruction
SIGKILL Request to end a process
SIGFPT Error divided by 0
SIGSEGV The process of retrieving to an invalid address
SIGCLD Child process ends
void producer()
{ while(1)
{ create_product ();
send(consumer, product); //send product to consumer
}
}
void consumer()
{ while(1)
{ receive(producer, product); //consumer waiting to receive product
consume (product);
}
}
Communication by socket
Each process needs to create a separate socket
Each socket is tied to a different port.
The read / write operations on the socket are the exchange of data
between two processes.
How to communicate by socket
Mail contact (socket acts as a post office)
"Sending process" writes data to its socket, the data will be transferred to the
socket of "receive process"
"Receive process" will receive data by reading data from the socket of "receive
process"
Telephone communication (socket plays the role of switchboard)
Two processes that need to be connected before data transmission / reception
and connection are maintained during data transmission
PROCESS SYNCHRONIZATION
Ensure parallel processing processes do not mislead each
other.
Request exclusive access(Mutual exclusion):
At one point, only one process has access to an unreachable
resource.
Synchronization requirements:
processes that need to work together to get things done.
Two "synchronous problems" need to be solved:
the problem of "exclusive access" ("critical section problem")
the problem of "coordinated implementation".
Critical Section
The code of a process maybe occur when accessing shared resources
(variables, files, ...).
Example:
if (account >= withdrawal money) account = account - withdrawal money;
else print “cannot withdraw money! “;
The conditions required when solving the critical
section problem
lock = 1;
critical-section ();.
lock = 0;
noncritical-section();
}
turn = 1; turn = 0;
Noncritical-section (); Noncritical-section ();
} }
Two processes certainly cannot enter the critical section at the same time, because at
one time the turn has only one value.
Violation: a process can be prevented into the critical section by another process not
in the critical section.
Peterson algorithm (used for 2 processes)
int busy=FALSE; // TRUE There is progress in the critical section and opposite is FALSE.
int blocked=0; // Count the number of processes being locked
while (TRUE)
{
if (busy)
{
blocked = blocked + 1;
sleep();
}
busy = FALSE;
if (blocked>0)
{
wakeup(); //wake up a pending process
blocked = blocked - 1;
}
Noncritical-section ();
}
Using Semaphore structure
The semaphore s variable has the following properties:
A positive integer value e;
A queue f: the list of pending processes on the semaphore s.
Two operations on the semaphore s:
Down (s): e = e-1.
Ife <0, the process must wait in f (sleep), otherwise the process
continues.
Up (s): e = e + 1.
If e <= 0, select a process in f for further execution (wake up).
Using Semaphore structure
Down(s) P is the process of performing the Down(s) or Up(s)
{ e = e - 1; operation.
if (e < 0)
{ status(P)= blocked; // Switch P to blocked state (wait) enter(P,f); // Let P in the queue f
}
}
Up(s)
{ e = e + 1;
if (e<= 0 )
{
exit(Q,f); // Take a Q process out of the queue f
status (Q) = ready; // Move Q to ready state
enter(Q,ready-list); // Put Q on the ready-list of systems
}
}
Using Semaphore structure
The operating system needs to install Down and Up operations
to be exclusive.
The structure of the semaphore:
class semaphore
{
int e;
PCB * f; //Semaphore's own list
public:
down();
up();
};
|e| = Process number waiting on f.
Solve the problem of critical section with
Semaphores
Example:
1 A Down 0 A
2 B Down -1 A B
3 C Down -2 A B, C
4 A Up -1 B C
Solving synchronous problems with Semaphores
e(s)=1;
while (1)
{
Down(s);
critical-section ();
Noncritical-section ();
}
Problems when using semaphore
Using semaphore can cause congestion.
P1:
{ Two processes P1, P2 used 2 shared
down(s1); down(s2); semaphore s1=s2=1
….
up(s1); up(s2); If the order does as following:
} P1: down(s1), P2: down(s2),
P2: P1: down(s2), P2: down(s1)
{ Then s1=s2=-1,
down(s2); down(s1); So P1, P2 are waiting forever
….
up(s2); up(s1);
}
Using Monitors structure
Monitor is a special structure (class)
exclusive methods (critical-section)
variables (shared for processes)
Variables in the monitor can only be accessed by methods in
the monitor
At one point, there is only one process that is operated
inside a monitor.
Condition Variable c
used to synchronize the use of variables in the monitor.
Wait(c) and Signal(c):
Using Monitors structure
Wait(c)
{ status(P)= blocked; // Move P to the waiting state
enter(P,f(c)); // Put P on the queue f(c) of the condition variable c
}
Wait(c): switch the state of call progress to
waiting (blocked) and set this process to the
Signal(c) queue of the condition variable c.
{
if (f(c) != NULL)
{
exit(Q,f(c)); // Get the Q process waiting on c
statusQ) = ready; // Move Q to ready state
enter(Q,ready-list); // Put Q on the ready-list
} Signal(c): if there is a waiting process in the queue of c, re-
} activate that process and the calling process will leave the
monitor. If no process is waiting in the queue of c, the
Signal(c) command is ignored.
Using Monitors structure
3
4
Monitors and the 5 philosophers have dinner
Problem
monitor philosopher
{ enum {thinking, hungry, eating} state[5];// shared variables for philosophers
condition self[5]; // condition variables for synchronise the having dinner
void pickup(int i)
{
state[i] = hungry; // philosopher_i is hungry
test(i); // Check before sending food to philosopher_i
if (state[i] != eating) self[i].wait(); // waiting resource
}
void putdown(int i)
{
state[i] = thinking; // philosopher_i is thinking
test((i+4) % 5); // check philosopher on the right, if OK take this philosopher having
dinner
test((i+1) % 5);// check philosopher on the left, if OK take this philosopher having
dinner
}
Monitors and the 5 philosophers have dinner
Problem
In case of deadlock?
Methods for handling and preventing deadlocks
ST4: Check the safe state of the system (using Algorithm to determine the safe state).
Example - resource allocation to avoid deadlocks
ST0: Calculate “Need”, which is the remaining need for each resource j
of each process i:
Need[i,j]=Max[i,j]-Allocation[i,j]
P2 0 0 0 0 0 0
P3 1 0 3 2 1 1
P4 4 2 0 0 0 2
Example - resource allocation to avoid deadlocks
+ Choose P1
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 0 0 0 7 2 3
P2 0 0 0 0 0 0
P3 1 0 3 2 1 1
P4 4 2 0 0 0 2
Example - resource allocation to avoid deadlocks
+ Choose P3:
R1 R2 R3 R1 R2 R3 R1 R2 R3
P1 0 0 0 0 0 0 9 3 4
P2 0 0 0 0 0 0
P3 0 0 0 0 0 0
P4 4 2 0 0 0 2
Example - resource allocation to avoid deadlocks
+ Choose P4:
R1 R3 R4
R1 R3 R4 P1 P2 P3
P1 P2 P3
P1 P2 P3
P4
R2 R5 R2 P4 R5
P4
Resources waiting
Resource-allocation graph Resource allocation test graph
graph on demand
For Resources with many instances