Contiguous allocation is one early method
transient
and kernel changing sizefirst
hole that is big enoughsmallest
hole that is big enough; must search entire list, unless ordered by size
largest
hole; must also search entire list
main program
、procedure
、function
、method
、object
、local variables
、global variables
、common block
、stack
、symbol table
、arrays
<segment-number, offset>
segment number s is legal if s < STLR
addresses + read requests
, or address + data and write requests
CPU must check every memory access generated in user mode to be sure it is between base and limit for that user
Logical vs. Physical Address Space
I/O
to /
from process memory space
Resource Allocation Graph
Resource Allocation Graph With A Deadlock
Graph With A Cycle But No Deadlock
/* thread one runs in this function */
void *do work one(void *param)
{
pthread mutex lock(&first mutex);
pthread mutex lock(&second mutex);
/** * Do some work */
pthread mutex unlock(&second mutex);
pthread mutex unlock(&first mutex);
pthread exit(0);
}
/* thread two runs in this function */
void *do work two(void *param)
{
pthread mutex lock(&second mutex);
pthread mutex lock(&first mutex);
/** * Do some work */
pthread mutex unlock(&first mutex);
pthread mutex unlock(&second mutex);
pthread exit(0);
}
void transaction(Account from, Account to, double amount)
{
mutex lock1, lock2;
lock1 = get lock(from);
lock2 = get lock(to);
acquire(lock1);
acquire(lock2);
withdraw(from, amount);
deposit(to, amount);
release(lock2);
release(lock1);
}
wait()
and signal()
wait(S)
while (S <= 0)
; //busy wait
S--;
}
signal(S) {
S++;
}
mutex lock
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Must guarantee that no two processes can execute wait()
and signal()
on the same semaphore at the same time
block
– place the process invoking the operation on the appropriate waiting queuewakeup
– remove one of processes in the waiting queue and place it in the ready queuetypedef struct{
int value;
struct process *list;
} semaphore;
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
// add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
// remove a process P from S->list;
wakeup(P);
}
}
Let S and Q be two semaphores initialized to 1
mutex
initialized to the value 1full
initialized to the value 0empty
initialized to the value ndo {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
rw_mutex
initialized to 1mutex
initialized to 1read_count
initialized to 0do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
do {
wait(mutex);
read_count++;
if (read_count == 1) // only for the first reader entering
wait(rw_mutex);
signal(mutex); // if the first reader wait for rw_mutex, other readers will be blocked on wait(mutex)
...
/* reading is performed */
...
wait(mutex);
read_count--;
if (read_count == 0) // only for the last reader leaving
signal(rw_mutex);
signal(mutex);
} while (true)
reader-writer locks
do {
wait ( chopstick[i] );
wait ( chopStick[ (i+1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i+1) % 5] );
// think
} while (TRUE);
semaphore mutex; // (initially = 1)
semaphore next; // (initially = 0)
int next_count = 0;
wait(mutex);
...
body of F;
...
if (next_count > 0)
signal(next)
else
signal(mutex);
semaphore x_sem; // (initially = 0)
int x_count = 0;
x.wait
can be implemented as:x-count++;
if (next_count > 0)
signal(next);
else
signal(mutex);
wait(x_sem);
x-count--;
monitor ResourceAllocator
{
boolean busy;
condition x;
void acquire(int time) {
if (busy)
x.wait(time);
busy = TRUE;
}
void release() {
busy = FALSE;
x.signal();
}
initialization code() {
busy = FALSE;
}
}
pickup()
and putdown()
in the following sequence:
DiningPhilosophers.pickup(i);
EAT
DiningPhilosophers.putdown(i);
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self [i].wait;
}
void putdown (int i) {
state[i] = THINKING;
/* test left and right neighbors */
test((i + 4) % 5);
test((i + 1) % 5);
}
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING)) {
state[i] = EATING;
self[i].signal();
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
最近剛重灌 Ubuntu,在此紀錄個人安裝 Vim 時常用的設定與外掛。
ubuntu 17.04
個人常用的 Prompt 顏色格式
PS1=’[\e[1;33m]\u@\h[\e[m]:[\e[1;34m]\W[\e[1;32m]$[\e[m] ‘