Inter-Task Communication
Inter-Task Communication
Inter-Task Communication
04/27/01
Lecture # 29
16.070
Synchronization
Semaphores
Mailboxes and Queues
Deadlock
Running
Blocked
Wait Queue
READY
Ready Queue
Smith 4/27/01
GPS
translation
Inertial Nav
interpreter
Instrument
Interfaces
Smith 4/27/01
Navigation
Package
Position
Data
Display
Subsystem
Smith 4/27/01
Inter-task Communication
Regular operating systems have many options for passing
messages between processes, but most involve significant
overhead and arent deterministic.
Pipes, message queues, semaphores, Remote Procedure Calls,
Sockets, Datagrams, etc.
Smith 4/27/01
Global Variables:
an example in pseudocode
int finished = 0;
main()
{
spawn( task1 );
spawn( task2 );
spawn( task3 );
finished++;
}
void task3 (void)
{
find_out_why_white_shirts_give_you_
black_belly_button_lint();
finished++;
Smith 4/27/01
Mailboxes
Post() - write operation- puts data in mailbox
Pend() - read operation- gets data from mailbox
Just like using a buffer or shared memory, except:
If no data is available, pend() task is suspended
Mutual exclusion built in:
if somebody is posting, pend() has to wait.
Buffering Data
If you have a producer and a consumer that work at
different rates, a buffer can keep things running smoothly
As long as buffer isnt full, producer can write
As long as buffer isnt empty, consumer can read
Consumer
Producer
Smith 4/27/01
Write()
Write()
Smith 4/27/01
10
Altitude
Laser Input
90
90 m
80
70
60
48
48 m
38
28
23 m
Smith 4/27/01
11
INS Task
Laser Task
Altitude:
Retrieve altitude
50m
Get alt. from sensor
50m
42m
Subtract _ from
altitude.
40m
Altitude:
Replace altitude
40m
42m
42m
Altitude:
40m
12
Timing Problem:
INS Input
Altitude
60
-10m
-10m
-10m
-10m
-10m
-10m
60 m
50
40
43 m
30
20
26 m
10
0
Smith 4/27/01
Laser Input
9m
13
Avoiding Conflict
We need to be careful in
multi-tasked systems,
especially when modifying
shared data.
We want to make sure that in
certain critical sections of the
code, no two processes have
access to data at the same
time.
Smith 4/27/01
14
Mutual Exclusion
If we set a flag (memory is busy, please hold), we
can run into the same problem as the previous
example:
Flag set?
Flag set?
Set flag
Set flag
Write to
memory
Smith 4/27/01
Write to
memory
Unset
Flag
15
Atomic Operations
An operating system that supports multiple tasks
will also support atomic semaphores.
The names of the functions that implement
semaphores vary from system to system (
test-set/release; lock/unlock; wait/signal; P()/V() )
The idea: You check a lock before entering a
critical section. If its set, you wait. If it isnt, you
go through the lock and unset it on your way out.
The word atomic means checking the lock and
setting it only takes one operation- it cant be
interrupted.
Smith 4/27/01
16
Semaphore Example
TelescopeImageUpdate()
{
if( time_is_now() )
{
lock( image_map );
update( curr_image[] );
unlock( image_map );
Smith 4/27/01
ImageTransmit()
{
broadcast( curr_image[] );
unlock( image_map );
unlock( transmitter );
}
17
Deadlock!
Process_Image_Weights()
ImageTransmit()
{
{
Waiting on
lock( image_map );
if( command == XMIT )
Waiting on
transmitter
{
image_map
color_map( curr_image[] );
lock( transmitter );
lock( transmitter );
lock( image_map );
broadcast(colored_image[]);
broadcast( curr_image[] );
unlock( transmitter );
unlock( image_map );
unlock( image_map );
unlock( transmitter );
}
}
Smith 4/27/01
18
19
20
Summary
Smith 4/27/01
21