CSE
451: Introduction to Operating Systems
Section 6, May/3/2001
Jochen Jδger
1.
Project
3 tips, tricks and suggestions
2.
Page
replacement
No changes needed in:
client.c, common.c, common.h, example_thread.c,
threadpool.h, threadpool_test.c
Minor changes needed in:
server.c
Major changes needed in:
threadpool.c
Lock: binary semaphore
Semaphore: counter
Condition variable: inside a lock, suspend and resume
PTHREAD_CREATE_DETACHED
pthread_mutex_t
mutex; // guarding critical
sections
pthread_cond_t
job;
pthread_mutex_lock(&mutex);
pthread_cond_wait(&job, &mutex);
pthread_mutex_unlock(&mutex);
evaluate return codes
clock() approx of
CPU time used by the program
time() seconds since Jan/1/1970
double
difftime(time_t t1, time_t t0) time passed in secs
int
gettimeofday(struct timeval *tv, struct timezone *tz)
GMT 8
Pacific time
Sample writeup:
Thread pool API
The thread pool API consists of three operations:
1.
threadpool create_threadpool(int
num_threads)
Preconditions: num_threads
> 0
Postconditions:
o
Returns
NULL
in case of failure.
o
If
successful, returns a pointer to a
Notes:
o
During
the execution of the constructor only the constructing thread and the worker
threads have access to the pool instance.
o
2.
void dispatch(threadpool pool,
dispatch_fn job_fn, void* job_args)
Preconditions:
o
pool
is a valid thread pool
instance.
o
The
calling thread is not one of the thread pool's worker threads.
o
The
code entered through job_fn
does not call any
operations on pool
.
Postconditions: The job represented by job_fn
has been dispatched to the
thread pool and will eventually be executed. Maybe. See the list of bugs.
Notes:
o
Bugs:
o
A
successfully dispatched job should be guaranteed to be processed eventually,
but the interface provides no way to indicate success or failure to the caller.
o
Thread pool implementation
The thread pool implementation consists of the thread pool structure,
the three API methods, and the thread function that each of the worker threads
runs in.
This is the thread pool structure:
typedef struct _threadpool_st
{
unsigned int num_threads;
unsigned int num_idle;
pthread_mutex_t mutex;
pthread_cond_t job;
} _threadpool;
_threadpool::num_threads
(num_threads) is
used to keep track of how many pooled threads are managed by this thread pool.
Synchronization primitives
The thread pool implementation uses
mutexes and
condition
variables:
_threadpool::mutex
(mutex) is used to
guard all critical sections.
Critical sections
There are
critical sections. The critical section in
exists to
protect
all others
·
The
constructor has
critical sections.
o
The
first
o
·
The
dispatch method has
critical sections.
o
·
The
destructor has
critical sections.
o
Why
does page replacement work?
Beladys
algo vs. SJF?
Beladys
anomaly?
Belady
nor LRU show Beladys anomaly, FIFO does
Counting
algorithms:
-
LFU
(least frequently used)
-
MFU
(most frequently used)
LRU
(least recently used) clock
instead of replacing page
referenced furthest away in the past, replace page that is old enough
other
names:
(NRU)
Not Recently Used, 2nd Chance or clock algo
circular linked list of all
physical pages
clock hand is used to
select a good LRU candidate
if the ref bit is on, turn it
off and go to next page
Problems:
overhead if running out of
memory
accuracy degrades if memory
large
Enhanced
LRU