Redis - 大厂程序员是怎么用的| 青训营笔记

73 阅读2分钟

这是我参与「第五届青训营 」笔记创作活动的第12天

C5 cpu Scheduling

Minimize response time Maximize Throughout Fairness share CPU amony user in a equtiable way

CPU utilization

Turnaround time of a process

Waiting time of a process

nonpreemptive preemptive FCFS 、Round Robin(RR) Scheduling、SRTF priority scheduling can be either preemptive or non-preemptive Multiple level scheduling Hard-real time systems Requires to complete a critical task within a guaranteed amount of time. Missing a deadline is a total system failure. Soft real-time systems can miss some deadline,but eventually performance will degrade if too many are missed.

C6 Process synchronization

对应编程题

Race condition

access and manipulate shared data concurrently.The final value of share data depends on which process .

Atomic Operation

Mutal exclusion

ensuring that only one process does a particular thing at once.

critial section

piece of code only one process can be executed at once.only one process at time can be get into the code.

what is semaphore(信号量)

A Semaphore has a non-negative integer value and support the following two operations.

image.png Binary semaphore:integer value can range only between 0 and 1;can be simpler to implement,also known as mutex locks. Counting semaphore: integer value can range over an unretricted domain. Deadlock Strarvation image.png

image.png

How to use semaphere to solve the problem?

  1. determine concurrent and sequential operations.
  2. determine the rules of mutual exclusion and synchronization.
  3. determine the procedure of mutual exclusion and synchronization.
  4. determine the number and meaning of semaphores.
  5. determine the inital value of the semaphores.
  6. determine the position of P and V operations.

whenever a process need a block

classical synchronization problems Bound buffer problem Reader and writer problem Dining-Philosophers problem sleeping barber problem

C7 Deadlocks

four requirements for deadlocks

Mutual exclusion

Hold and wait

No preemption

Circular wait

deadlock aviodance deadlock prevention image.png

How the Banker's algorithm works?

1 let work and finish be vectors of length m and n,respectively Initialize: a.work=available b.finish[i]=flase for i=o.1...i-1 2.find an i such that both: a.finish[i]=false b.needi<=work if no such i exists,go to step 4. 3.work=work+allocationi finish[i]=true go to step 2. 4.If finish[i]==true for all i.then the system is in a safe state. 不能只写序列,要写计算过程。 if there is no enough for process B,unsafe state. Resource-request algorithm for process Pi. Requsti=requst vector for process Pi.

What to do when detect deadlocks?

Terminate process,force it to give up resources

preempt resource without killing off process

Roll back action of deadlocked processes

Many operating systems use other options