Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Operating Systems - 1
Background
Concurrent access to shared data may result
in data inconsistency.
Maintaining data consistency requires
mechanisms to ensure the orderly execution
of cooperating processes.
Operating Systems - 1
Bounded-Buffer
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
int counter = 0;
Producer process
Consumer process
item nextProduced;
item nextConsumed;
while (1) {
while (counter == BUFFER_SIZE)
; /* do nothing */
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
while (1) {
while (counter == 0)
; /* do nothing */
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
Operating Systems - 1
Bounded Buffer
The statements
counter++;
counter--;
must be performed atomically.
Atomic operation means an operation that
completes in its entirety without interruption.
Operating Systems - 1
Bounded Buffer
The statement counter++ may be implemented in
machine language as:
register1 = counter
register1 = register1 + 1
counter = register1
The statement counter-- may be implemented as:
register2 = counter
register2 = register2 1
counter = register2
Operating Systems - 1
Bounded Buffer
If both the producer and consumer attempt to
update the buffer concurrently, the assembly
language statements may get interleaved.
Interleaving depends upon how the producer
and consumer processes are scheduled.
Operating Systems - 1
Bounded Buffer
Race Condition
Race condition:
The situation where several processes access
and manipulate shared data concurrently. The
final value of the shared data depends upon which
process finishes last.
Operating Systems - 1
Operating Systems - 1
Mutual Exclusion
Operating Systems - 1
1. Mutual Exclusion
No two processes may be simultaneously inside
their critical regions
2. Progress
No process running outside the critical region may block
other process,
Only those processes that are not executing in their
remainder section can participate in deciding which will
enter its critical section next, within a finite time.
3. Bounded Waiting
No process should have to wait forever to enter its
critical region.
Lock Variables
A variable with a value 0 for allowing the
processes to enter the critical section and 1 for
restricting it is used.
Operating Systems - 1
Operating Systems - 1
Strict Alternation
Algorithm 2
Shared variables
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [0] = true P0 ready to enter its critical section
Process P0
do {
flag[0] = true;
while (flag[1]) ;
critical section
flag [0] = false;
remainder section
} while (1);
Process P1
do {
flag[1] = true;
while (flag[0]) ;
critical section
flag [1] = false;
remainder section
} while (1);
The problem lies when both the flags of the process are true at the same
instant.
Operating Systems - 1
Testing Condition
Process- B
FLAG_B = True
Testing Condition
! Flag_B
! Flag_A
Critical Section
Operating Systems - 1
Hardware Solutions
Disabling the interrupts
Operating Systems - 1
Petersons Solution
Restricted to two process
Alternate execution between critical section
and remainder section
Shared data:
int turn; //whose turn to enter critical section
boolean flag[2]; //true means process ready to
enter CS
Operating Systems - 1
Petersons algorithm
Combined shared variables of algorithms 1 (alternation) and 2.
Process Pi
do {
flag [i]:= true;
turn = j;
while (flag [j] && turn == j) ;
critical section
flag [i] = false;
remainder section
} while (1);
Meets all three requirements; solves the critical-section problem for
two processes.
Operating Systems - 1
turn:= 1;
Process P1:
Do {
flag[1]:=true;
// 1 wants in
turn:=0;
// 0 gives a chance to 1
// 1 gives a chance to 0
while
(flag[1]&&turn==1);
CS
flag[0]:=false;
while
(flag[0]&&turn==0);
CS
flag[1]:=false;
// 0 no longer wants in
// 1 no longer wants in
RS
While (1);
RS
While (1);
There could be a situation that both flag [0] and flag[1] are true.
But even in that case the turn can have only one value.
Operating Systems - 1
Process- B
FLAG_B = True
Turn = B
Turn = A
Testing Condition
Testing Condition
! (Flag_B and Turn== B)
FLAG_ A = false
Critical Section
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
false
Pb
True false
false
1 0 0
Pc
True false
false
Pd
True false
false
Operating Systems - 1
false
Pb
false
1 0
Pc
false
2 0
Pd
True false
false
Operating Systems - 1
false
0
PID
101
Pb
false
Pc
false
103
Pd
false
104
102
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Process Pi:
repeat
disable interrupts
critical section
enable interrupts
remainder section
forever
Synchronization Hardware
Many systems provide hardware support for
critical section code
Uniprocessors could disable interrupts
Currently running code would execute without
preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
special
atomic
TestAndSet Instruction
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv;
}
Operating Systems - 1
remainder section
} while (TRUE);
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Semaphore
Operating Systems - 1
Semaphore Principle
Two or more processes can cooperate by means
of simple signals, such that a process can be
forced to stop at a specified place until it has
received a specific signal.
Semaphore Variable
Semaphore S is an integer variable
Operations possible on the semaphore variable are
may be initialized to a nonnegative value.
The wait operation decrements the semaphore value.
The signal operation increments the value of the semaphore.
Binary semaphore
It can take only 0 and 1.
Operating Systems - 1
Semaphores
Semaphore is a type of generalized lock
Operating Systems - 1
}
wait (S)
{
while (S<=0)
; // no -op
S--;
}
Operating Systems - 1
signal (S)
{
S++;
}
Operating Systems - 1
To receive a signal
wait ( ) is used
Operating Systems - 1
Operating Systems - 1
Operating Systems - 1
Semaphore Implementation
Must guarantee that no two processes can execute
wait () and signal () on the same semaphore at the
same time
The main disadvantage of the semaphore definition
given here is that it requires Busy Waiting (SPIN
LOCK).
While a process is in its critical section, any other
process that tries to enter its critical section must
loop continuously in the entry code.
Note that applications may spend lots of time in
critical sections and therefore this is not a good
solution.
Operating Systems - 1
Operating Systems - 1
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
Operating Systems - 1
wait (Q);
wait (S);
.
.
.
signal (Q);
signal (S);
Priority Inversion Scheduling problem when lowerpriority process holds a lock needed by higher-priority
process
Solved via priority-inheritance protocol
Operating Systems - 1
Operating Systems - 1
Bounded-Buffer Problem
N buffers, each can hold one item
Operating Systems - 1
mutex=1
empty=n
full=0
Operating Systems - 1
Producer
do {
//
Consumer process
do {
} while (TRUE);
Operating Systems - 1
wait (full);
wait (mutex);
// remove an item from
// buffer to nextc
signal (mutex);
signal (empty);
// consume the item in
// nextc
} while (TRUE);
Operating Systems - 1
sem_init
int sem_init(sem_t *sem, int pshared, unsigned int initial_value);
sem_init() initializes the unnamed semaphore at the address pointed to
by sem.
The value :initial value for the semaphore.
If pshared = 0, semaphore is shared between the threads of a process,
(e.g., a global variable, or a variable allocated dynamically on the heap).
If pshared = nonzero, semaphore is shared between processes, located
in a region of shared memory. Forked child can also access the
semaphore.
sem_init() returns 0 on success; on error, -1 is returned, and errno is set
to indicate the error.
Error
EINVAL value exceeds SEM_VALUE_MAX.
ENOSYS pshared is nonzero, but the system does not support
process-shared semaphores (see sem_overview(7)).
Operating Systems - 1
sem_post
int sem_post(sem_t *sem);
sem_post() increments (unlocks) the semaphore pointed to by sem.
If the semaphore's value > 0, then another process or thread
blocked in a sem_wait(3) call will be woken up and proceed to lock
the semaphore.
sem_post() returns 0 on success; on error, the value of the
semaphore is left unchanged, -1 is returned, and errno is set to
indicate the error.
Errors
EINVAL sem is not a valid semaphore.
EOVERFLOW The maximum allowable value for a semaphore would be
exceeded.
Operating Systems - 1
sem_wait
int sem_wait(sem_t *sem);
sem_wait() decrements (locks) the semaphore pointed to by sem.
If the semaphore's value >0, then the decrement proceeds, and the
function returns, immediately.
If the semaphore value =0, then the call blocks until it becomes possible to
perform the decrement (i.e., the semaphore value rises above zero).
return 0 on success; on error, the value of the semaphore is left
unchanged, -1 is returned, and errno is set to indicate the error.
Errors
EINTR The call was interrupted by a signal handler; see signal(7).
EINVAL sem is not a valid semaphore.
ETIMEDOUT The call timed out before the semaphore could be locked.
Operating Systems - 1
Operating Systems - 1
Readers-Writers Problem
A data set is shared among a number of concurrent
processes
Readers only read the data set; they do not perform
any updates
Writers can both read and write
Data set
Semaphore mutex initialized to 1
Semaphore wrt initialized to 1
Integer readcount initialized to 0
Operating Systems - 1
do {
Only one process executes at
wait (mutex) ;
a time to make consistency for
readcount ++ ;
readcount variable
if (readcount == 1)
writing is performed
wait (wrt) ;
To see no process writing
signal (mutex) Allow another to read simultaneously
signal (wrt) ;
} while (TRUE);
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0)
To allow process for writing signal (wrt) ;
signal (mutex) ;
} while (TRUE);
Operating Systems - 1
Readers-Writers Problem
Variations
First variation no reader kept waiting unless writer has
permission to use shared object
Second variation once writer is ready, it performs write
asap
Both may have starvation leading to even more variations
Problem is solved on some systems by kernel providing
reader-writer locks
http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSyste
ms/6_Synchronization.html
Operating Systems - 1
Dining-Philosophers Problem
Operating Systems - 1
Dining-Philosophers Problem
Algorithm
The structure of Philosopher i:
do {
P( chopstick[i] );
P(chopStick[ (i + 1) % 5] );
// eat
V( chopstick[i] );
V(chopstick[ (i + 1) % 5] );
// think
} while (TRUE);