Tuesday, July 23, 2024

CST334 - Week 5

 

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:


int pthread_mutex_lock(pthread_mutex_t *mutex);
   int pthread_mutex_unlock(pthread_mutex_t *mutex);
   int pthread_mutex_trylock(pthread_mutex_t *mutex);
      int pthread_mutex_timedlock(pthread_mutex_t *mutex,
struct timespec *abs_timeout);

 

** 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

CST462S - Final Week

Service Learning Journal Reflections This marks the final week of our Service Learning experience. Our team successfully completed the final...