You are on page 1of 2

Ho Chi Minh University of Technology

Faculty of Computer Science and Engineering

Operating Systems
Laboratory Report 5
April 24, 2021

Student’s name: Vu Hoang Hai


Student ID: 1952669
Class: CC01 - CO2017
Date Performed: April 24, 2021
Instructor: Le Thanh Van

1 Race conditions

Scenario:
Consider a banking system that maintains an account balance with two functions:
deposit(amount) and withdraw(amount). These two functions are passed the
amount that is to be deposited or withdrawn from the bank account balance.
Assume that a husband and wife share a bank account. Concurrently, the husband
calls the withdraw(amount) function and the wife calls deposit(amount).
Analyse:
This is a mathematical situation of processing synchronization of a shared resource,
in this case, the bank account. It is shared by two recipients, the wife using
deposit(amount) to store cash and the husband uses withdraw(amount) to get
cash. This is simulation of a producer-consumer mathematical model.
Let S be the shared bank account that is used by both persons. A is the action
of withdrawing of the husband and B is the action of the wife depositing. These
scenarios may happens:
– A and B happens concurrently or continuously-without-paralleling crediting
V . This is a normal transaction.
– A and B both use V at the same time, causing racing condition of resource
V . This may lead to inaccuracy, errors or faulty execution of crediting V and
the amount may shift unexpectedly, either larger or smaller.

To solve this premable issue, mutex locking techniques or semaphore can be applied
to address out-of-sync issues above.

1
2 Syncing performance

Task
Remove all entry and exit sections in cond_usg.c and compare performance.
Solution
Performance-wise comparisons of Lab 5 implementation (using a generic array to
store count at each thread) versus Lab 6 (using mutex lock to manage and sync
shared resources between threads):

– Lab 5 way of doing ensures no race condition, faster execution and accuracy
of algorithms. However it uses significantly more memory space than in lab 6.
– Lab 6 uses less memory space than in lab 5 (1 global count). However, mutex
locking must be used to prevent race conditions and algorithmic errors, hence
the execution time is generally longer because each thread must wait until the
global variable is freed.

You might also like