13 Lecture
13 Lecture
Synchronization &
Interprocess
Communication
Agenda
• Announcements
• Lectures are being recorded
• Assignment 3 will be announced on Brightspace
• Lecture Contents
• Fixing Linked Lists
• Interprocess Communication (IPC)
en
for ( ; n->next != null; n = n->next) {
if (n->next->key == key) break
}
ok
lock(n)
lock(n->next)
tmp = n
n = n->next
tmp->next = n->next
unlock(tmp)
Br
}
unlock(n)
Node Lock Insert(key,
item)
insert(key, item) {
Node n = Node(key, item)
lock(head)
if(key < head->key) {
n->next = head
head = n
unlock(head)
n?
} else {
unlock(head)
for (tmp = head; tmp->next != null; tmp = tmp->next) {
ke
if(tmp->next->key >= key) break
}
lock(tmp)
ro
n->next = tmp->next
tmp->next = n
unlock(tmp)
}
What’s Broken
What’s Broken? (Insert)
• Consider two concurrent operations:
• Node 2 being deleted.
• A node being inserted between nodes 2 and 3
Insert waits
• The inserted node could be lost for lock
Interrupt
Handler
Queues
P1
Interrupt
Handler P2
P42
P99
Process Table
Proc 42
The Message Passing
(Post-Office) Facility
• Queue Pi stores messages (or senders) to the
process Pi
• The head of queue Pi is the next message to be
received by Pi Queues
P1
• To send a message processes must know the ID P2
or name of the process they are sending to P42
• The process asks the kernel to enqueue the P99
message on the corresponding queue
• To receive a message, it is not (always) Message Passing Facility
necessary for the process to explicitly specify
the sender
• The process asks the kernel to remove a message
from their queue and return it
• Analogy: A queue is the same as a mailbox
Process Naming
Process Naming
• How processes are named and how process names are
discovered is a whole topic beyond scope of this course
Example: On the Internet
• Processes are named by IP/port #
• Use DNS to look up IP addresses and port #s of specific
processes: e.g., 25 for e-mail (SMTP)
• Other Examples:
• On a single system each process has unique process ID (pid)
assigned by the OS
• Each thread has a unique thread ID (tid), assigned by the OS
or run-time system such as Java
• Another idea is to use a registry
Name Registry
• Analogy: A phone book
• Processes register their names on a central server
• Other processes can query server to find names
E.g., this is what DNS is Hello!
PID: 99
Registry Server ? Name:
ice
37 : Carol Al Dave
PID: 42
(42,Alice) 13 : Bob
Name: (42)
Alice 42 : Alice
Synchronous
Message Passsing
Should Processes Block?
• Questions:
• When a process sends a message, should it block until the
message is received?
• When a process receives a message, should it block until the
message arrives?
• Answers depend on the system:
• Typically, receivers will block until the message arrives
• Why?
• There are exceptions
• Whether senders should block depends on what type of
behaviour is desirable:
• Synchronous sends
• Asynchronous sends
Synchronous Send PID: 42
Name:
void Consumer() {
while (1) {
Recv(producer, p)
consume(p);
}
}