Round-robin
Is assigned a time slice to each process, generally set 10ms, this ensures that each process will not be too hungry, but the average turnaround time is relatively large, and there will be additional overhead of process switching
Multilevel feedback queue
That is, there are many ready queue, then queue using different scheduling algorithms are not the same, the use of more flexible
Real-Time Scheduling
Each task to define a start time, and the deadline to complete this task in this section
It divided into hard and soft time limit
If a hard time not to miss serious consequences, must be completed in the worst case
Soft advance can not meet the requirements, then reduce as far as possible to meet the requirements on the line
Synchronization issues:
In real life example:
Refrigerator bread
A eat bread
Go to the refrigerator to find
B found no bread to eat
Go looking to buy a refrigerator
Found no
Buy
This will buy a duplicate of bread, if within the computer, making a lot of repetitive things will be inestimable, and then consider the practice
Here it involves a synchronized computer, these operations are defined as atomic operations, it is critical that local area, while only one person access
There are three critical areas to achieve
1, disable interrupts directly in hardware that is broken is disabled, then the switching process can not produce, so as to achieve only one visit, but may cause other processes into starvation, the system will be stopped
2, the operating software using some shared variables
3, high-level abstraction into atomic operations
signal
Represents the number of resources, there are two operations P, V
P: I am applying on behalf of a resource, resource -1 if <0, then in a wait state
V: on behalf of the release of a resource, resource +1, if <= 0, but the application on behalf of someone in a wait state, then I went to wake it up
an int the num = 3 ; P(){ num--; if(num<0) { add the current into the waiting queue In this block blockage wait for resources } } V () { num++; if(num<=0){ remove removed from the queue wakeup wake up a thread } }
Classic question: producer - consumer
Requirements: a bucket can hold n items, producers continue in making things, consumers continue to take things in, but the operation of the bucket can only be a time to do the same, if the bucket is full, producers need to wait if the bucket is empty, then consumers need to wait
int X = . 1 ; // Representative mutex, only one person at the same time operating the tub int Y = 0 ; // Number thing represents the current bucket int Z = n-; // Representative bucket now remaining space Product ( ) { // Manufacturer P (z); P(x); Making things V(x); V (y); } custmer(){ P(y); P(x) ; take things V(x); V(z); }
The tube:
The tube is also a way for the critical region, it is the process of operation, taking into account the current thread wait time for some reason, this time to give up lock critical areas, to other process before execution, which is set in there waiting queue
Wait (): wait for the queue to join their species, and then wake up mutually exclusive access to a waiter or release of the tube
Signal (): a thread will wait in the queue wake
The tube in the form of producer - consumer issues
Lock Lock int COUNT = 0 int the X-, the y- // something that represents the number and spatial product(){ lock-Acquire() ; while(count==n){ notFull.wait ( & Lock ) // himself added queue } Add c to the buffer; count++ notEmpty.Signal(); lock->release } cutomer(){ lock->Acquire() while(count==0) notEmpty.Wait(&lock); remove c from buffer count-- notFull.Signal lock->Release() }
Dining Philosophers
Requirements: Dining Philosophers individual n n is the fork in a circle around everyone on both sides of the fork, respectively, and each philosopher can do two things, thinking and eating, eating fork must take their left and right to eat, how orderly execution continues
Here is the obvious fork mutex
Semaphore practice
int Cha [ . 5 ] // . 5 fork while(1){ Think () // philosophers Reflection P (cha [i]); P (father [i + 1 ]) EAT () // seating V (cha [i]); V (cha [i + 1 ]) } // if each philosopher hands at the same time take the left fork, and then not have the right, we can not release the resources, then in a deadlock while(1){ Think () // philosophers Reflection P (In Flag) // critical region P (cha [i]); P (father [i + 1 ]) EAT () // seating V (cha [i]); V (cha [i + 1 ]) V (flag); } // Although this method may be, but after add a critical section, obviously five people can simultaneously two meals, but this became only one person while(1){ Think () // philosophers Consideration IF (I% 2 == 0 ) { P (father [i]); P (father [i + 1 ]) } else{ P (father [i + 1 ]); P (father [i]); } EAT () // seating V (cha [i]); V (cha [i + 1 ]) } Deadlock because the first option just have to take the time to appear on the left, so as long as people take control in different directions is not the same does not happen this way
Reader - Writer
Requirements: the reader can have multiple access, write only one person write, read and write can not be performed at the same time
Deadlock Concepts:
Deadlock is in the state are in the process of application resources and also hold their own resources to the other side
Four necessary conditions for deadlock: a deadlock will occur while the establishment of four
1, mutually exclusive: these resources must be mutually exclusive, but only one can access
2, requests and maintain: they have the resources and the resources of the state in the application
3, non-preemptive: can only process runs out on their own initiative to release their own resources can not snatch
4, loop Wait: to form a ring A-> Resources B-> C-> A in the other applications are here
Deadlock
Deadlock Prevention: four necessary conditions constitute the deadlock, as long as the one destroyed, it will fall deadlock prevention, so there are four ways
Deadlock Avoidance: If the current resource allocation deadlock occurs, then the application will not receive a distribution of resources, and safety can be obtained by banker's algorithm sequence
Deadlock detection and recovery