C-CS316 - Lect06 - Process Synchronization (Part 1) - 2
C-CS316 - Lect06 - Process Synchronization (Part 1) - 2
Operating Systems
C-CS316 Spring 2024
LECTURE 06:
Process Synchronization
Dr. Basma Hassan Dr. Ahmed Salama
[email protected] [email protected]
Bounded-buffer
Places no limit on the size of Assumes that there is a fixed
the buffer buffer size
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . . Producer
Buffer
} item;
item buffer[BUFFER_SIZE];
int in = 0; Consumer
int out = 0;
Consumer
entry section
critical section
exit section
remainder section
Progress If no process is executing in its critical section and there exist some
processes that wish to enter their critical section, then the selection of
the process that will enter the critical section next cannot be postponed
Liveness
indefinitely
Bounded A bound must exist on the number of times that other processes are
Waiting allowed to enter their critical sections after a process has made a request
to enter its critical section and before that request is granted
Rule of thumb:
When designing a concurrent algorithm, worry about safety first but don’t forget liveness!
CPU
while (true){
/*critical section*/
turn = j;
/*remainder section*/
flag[i] = false;
/*remainder section*/
flag[i] = false;
/*remainder section*/
}
• Definition
boolean test_and_set (boolean *flag){
boolean org_value = *flag;
*flag = true;
return org_value;
}
• Properties
• Executed atomically
• Returns the original value of passed parameter
• Set the new value of passed parameter to true
• Definition
int compare_and_swap(int *value, int expected, int new_value){
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
• Properties
• Executed atomically
• Returns the original value of passed parameter value
• Set the variable value the value of the passed parameter new_value but only if
*value == expected is true.
• That is, the swap takes place only under this condition.
critical section
release lock
release() {
remainder section
available = true;
}
}
• Solution:
acquire() {
while (test-and-set(&lock))
; /* busy wait */
}
release() {
lock = false;
}
• Solution:
acquire() {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* busy wait */
}
release() {
lock = 0;
}
• Binary Semaphore
• Integer value can range only between 0 and 1 → Count = 1
• Same as a mutex lock
• Represents single access to a resource
• Count = 1
• Counting Semaphore
• Integer value can range over an unrestricted domain → Count = N
• Represents a resource with many units available
• Multiple threads can pass the semaphore
• Two operations:
• block – place the process invoking the operation on the appropriate
waiting queue
• wakeup – remove one of processes in the waiting queue and place
it in the ready queue
• Waiting queue
typedef struct {
int value;
struct process *list;
} semaphore;