CS241 System Programming: Discussion Section 4 Feb 13 - Feb 16
CS241 System Programming: Discussion Section 4 Feb 13 - Feb 16
CS241 System Programming: Discussion Section 4 Feb 13 - Feb 16
System Programming
Discussion Section 4
Feb 13 – Feb 16
Outline
z Synchronization
z Condition Variables
z Producer-Consumer Problem
z Reader-Writer Problem
z CPU Scheduling
z Metrics
z Examples
Review
z What is wrong with the following code fragment
in multi-threaded environment? How would you
modify it?
static int count;
void increment(void) {
count++;
}
void decrement(void) {
count--;
}
int getcount() {
return count;
}
Review
z Thread-Safe version (using mutex)
static int count = 0;
static pthread_mutex_t countlock = PTHREAD_MUTEX_INITIALIZER;
z #include <pthread.h>
z Type: pthread_cond_t
z Two main operations
z Wait: pthread_cond_wait
z Signal: pthread_cond_signal
Example
z Waiting for x==y condition
pthread_mutex_lock(&m);
while (x != y)
pthread_cond_wait(&v, &m);
/* modify x or y if necessary */
pthread_mutex_unlock(&m);
z Static Initializer
pthread_cont_t cond = PTHREAD_COND_INITIALIZER;
z Internals
z Causes the thread to release the mutex
z Sleeps until signaled
z Reacquires the lock when waken up
z Variation: pthread_cond_timedwait
Signaling on CVs
z Signal
z Wakes up one waiting thread
int pthread_cond_signal(pthread_cond_t *cond);
z Broadcast
z Wakes up all waiting threads
int pthread_cond_broadcast(pthread_cond_t *cond);
Example
z Thread-safe Barrier (Program 13.13)
int waitbarrier(void) { /* wait at the barrier until all n threads arrive */
int berror = 0;
int error;
z Reader-Writer Synchronization
z Strong Reader Synchronization
z Preference to readers (e.g. a library database)
z Strong Writer Synchronization
z Preference to writers (e.g. an airline reservation system)
z #include <pthread.h>
z Type: pthread_rwlock_t
z Three main operations
z Read Lock: pthread_rwlock_rdlock
z Write Lock: pthread_rwlock_wrlock
z Unlock: pthread_rwlock_unlock
Creating / Destroying rwlock
z Creating a read-write lock
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
const pthread_rwlockattr_t *restrict attr);
z Algorithms
z First-Come First-Serve (FCFS)
z Shortest Job First (SJF)
z Round Robin (RR)
z Priority
CPU Scheduling
z Preemptive vs Non-preemptive
z Non-Preemptive: The running thread keeps CPU until it
voluntarily gives it up
z Preemptive: The running thread can be forced to give up
CPU by another thread
z Metrics
z Waiting Time
z Total amount time that a thread waits
z Turnaround Time
z (Thread finish time – thread entry time)
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
Initial Condition
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
After 8 seconds
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
After 11 seconds
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
After 18 seconds
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
After 24 seconds
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
0 8 11 18 24
P2 waiting time: 0 The average waiting time (AWT):
P4 waiting time: 8
P3 waiting time: 11 (0+8+11+18)/4 = 9.25
P1 waiting time: 18
A Simple Example (Priority)
Process Duration Priority # Arrival Time
P1 6 4 0
P2 8 1 0
P3 7 3 0
P4 3 2 0
0 8 11 18 24
P2 turnaround time: 8 The average turnaround time (ATT):
P4 turnaround time: 11
P3 turnaround time: 18 (8+11+18+24)/4 = 15.25
P1 turnaround time: 24
POSIX Scheduling
z #include <sched.h>
z CPU Scheduling
z Metrics
z Waiting Time, Turnaround Time
z Examples