CST334 – Operating Systems - Week 5
This week in Operating Systems, we are focusing on an
introduction to concurrency. The use of threads improves the processing speed
when used with a multi-processor system. Program blocking progress is also
avoided using threads by utilizing the CPU for other activities while waiting
on I/O request to complete. Threads share the same address space but will have
multiple program counters and separate calls to the stack. To create threads,
the function pthread create() Is used in conjunction with pthread join() or
procedure call.
Locks ensure the atomic execution of instructions by
protecting critical sections. The basic routines for locks using mutex(mutual
exclusion) in pthreads library are:
** Example of wait call with
condition, singling is done from the calling thread.
Pthread_mutex_lock(&lock);
while (ready == 0)
Pthread_cond_wait(&cond, &lock);
Pthread_mutex_unlock(&lock);
The purpose of the wait call is to
put the thread to sleep and release the lock once the condition is lifted and
the global variable ready values are set to a non-zero value. Locks are
evaluated for protecting the critical sections, fairness while voiding thread
starvation, and reasonable time overheads for better performance.
A condition variable is used to
allow parent thread to wait on the child to finish execution or meet a
condition. The advantageous of using condition variable over spin is
efficiency. The condition variable does not waste the CPU time and signaling -
signal() – the waiting thread to wake up once a condition is satisfied.
No comments:
Post a Comment