Djotio-Cours SE-Operating System Lecture 8 - 0
Djotio-Cours SE-Operating System Lecture 8 - 0
Systems
Lecture 8
Agenda for Today
Review of previous lecture
Interprocess communication (IPC) and
process synchronization
UNIX/Linux IPC tools (pipe, named pipe—
FIFO, socket, message queue, shared
memory)
Use of pipe
Producer-consumer problem
Interprocess
Communication (IPC)
Mechanism for processes to
communicate and to synchronize
their actions.
Message system – processes
communicate with each other
without resorting to shared variables.
Interprocess
Communication (IPC)
IPC facility provides two operations:
Send (message) – message size fixed
or variable
Receive (message)
Interprocess
Communication (IPC)
If P and Q wish to communicate, they
need to:
establish a communication link
between them
exchange messages via send/receive
Interprocess
Communication (IPC)
Implementation of communication link
physical (e.g., shared memory,
hardware bus)
logical (e.g., logical properties)
Implementation
Questions
How are links established?
Can a link be associated with more
than two processes?
How many links can there be between
every pair of communicating
processes?
Implementation
Questions
Mailbox sharing
P1, P2, and P3 share mailbox A.
P1, sends; P2 and P3 receive.
Who gets the message?
Indirect Communication …
Solutions
Allow a link to be associated with at
most two processes.
Allow only one process at a time to
execute a receive operation.
Allow the system to select arbitrarily
the receiver. Sender is notified who
the receiver was.
Synchronization
Message passing may be either
blocking or non-blocking.
Blocking is considered synchronous
Non-blocking is considered
asynchronous
send and receive primitives may be
either blocking or non-blocking.
Buffering
Queue of messages attached to the link;
implemented in one of three ways.
Zero capacity – No messages Sender must wait for
receiver
#include <unistd.h>
int pipe (int filedes[2]);
Example
parent child
fork
P P
Read Write
end end
Sample Code
/* Parent creates pipe, forks a child, child writes into
pipe, and parent reads from pipe */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
main()
{
int pipefd[2], pid, n, rc, nr, status;
char *testString = "Hello, world!\n“, buf[1024];
Sample Code
rc = pipe (pipefd);
if (rc < 0) {
perror("pipe");
exit(1);
}
pid = fork ();
if (pid < 0) {
perror("fork");
exit(1);
}
Sample Code
if (pid == 0) { /* Child’s Code */
close(pipefd[0]);
write(pipefd[1], testString, strlen(testString));
close(pipefd[1]);
exit(0);
}
Sample Code
/* Parent’s Code */
close(pipefd[1]);
n = strlen(testString);
nr = read(pipefd[0], buf, n);
rc = write(1, buf, nr);
wait(&status);
printf("Good work child!\n");
return(0);
Recap of Lecture
Review of previous lecture
Interprocess communication (IPC) and
process synchronization
UNIX/Linux IPC tools (pipe, FIFO, socket,
message queue, shared memory, etc.)
Use of UNIX pipe