MCP-Unit 2
MCP-Unit 2
1. Performance
2. Scalability
3. Synchronization and Data sharing - Data races
4. Synchronization Primitives
(Mutexes, Locks, Semaphores, Barriers)
5. Deadlocks and Live locks
6. Communication between threads
(Condition variables, Signals, Message queues
and Pipes)
1. PERFORMANCE
There are two common metrics for performance:
• The time it would take to complete this calculation is some cost per
addition, k, multiplied by the number of additions, N. So, the time
would be k ∗ N.
• Restructuring of work –
Eg: Loop Containing Floating-Point Arithmetic
double total=0.0;
for (int i=0; i<1000; i++)
{
total = total + 1.0;
}
Strength Reduction
At this step, the compiler reads all the object files
and looks for optimizations it can perform using
full knowledge of the entire application.
• Function A() calls function B(), but function B() is
defined in the file b.c, and function A() is defined in
the file a.c
2. SCALABILITY
Disadvantage :
A spin lock will spin on a virtual CPU monopolizing that
resource.
In comparison, a mutex lock will sleep and free the
virtual CPU for another thread to use.
SEMAPHORES
• Semaphores are counters that can be either incremented or
decremented.
int main()
{
installHandler( SIGNAL, signalHandler );
sendSignal( SIGNAL );
}
MESSAGE QUEUES
A message queue is a data structure that can be shared
between multiple processes.
The method for using pipe is much like the method for
using a file – Make, Open ,Read, Write, Close, Delete.
The pipe is opened, data is written into it or read from it,
and then the pipe is closed.
For example, the output from the command ls, which lists
all the files in a directory, could be piped into the wc
command, which counts the number of lines, words, and
characters in the input.
$ ls | wc
Pipe
Flow of Data
Full Duplex Communication via Two Pipes
p0
p1
Setting Up and Writing into a Pipe
Two types:
Unnamed pipes – Stream/Traditional pipe
Named pipes – FIFO pipe
UNNAMED PIPES
• Unnamed pipes can only be used between related
process, such as parent/child or child/child process.
The method for using a named pipe is much like the method
for using a file –
CreateFile, OpenFile, ReadFile, WriteFile, Close File
• When generated, named pipes have a directory
entry.