Upload
adela-reed
View
234
Download
3
Tags:
Embed Size (px)
Citation preview
13-Sep-200613-Sep-2006 22
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 Recap of the lecture
13-Sep-200613-Sep-2006 33
Review of Lecture 7
The wait and exec system calls and sample code
Cooperating processes Producer-consumer problem
13-Sep-200613-Sep-2006 44
Interprocess Interprocess Communication (IPC)Communication (IPC)
Mechanism for processes to Mechanism for processes to communicate and to synchronize communicate and to synchronize their actions.their actions.
Message system – processes Message system – processes communicate with each other communicate with each other without resorting to shared without resorting to shared variables.variables.
13-Sep-200613-Sep-2006 55
IPC facility provides two operations:IPC facility provides two operations:Send Send ((messagemessage) – message size ) – message size
fixed or variable fixed or variable Receive Receive ((messagemessage))
Interprocess Interprocess Communication (IPC)Communication (IPC)
13-Sep-200613-Sep-2006 66
If If PP and and QQ wish to communicate, wish to communicate, they need to:they need to:establish a establish a communicationcommunication linklink
between thembetween themexchange messages via exchange messages via
send/receivesend/receive
Interprocess Interprocess Communication (IPC)Communication (IPC)
13-Sep-200613-Sep-2006 77
Implementation of communication Implementation of communication linklinkphysicalphysical (e.g., shared memory, (e.g., shared memory,
hardware bus)hardware bus) logicallogical (e.g., logical properties) (e.g., logical properties)
Interprocess Interprocess Communication (IPC)Communication (IPC)
13-Sep-200613-Sep-2006 88
Implementation Implementation QuestionsQuestions
How are links established?How are links established? Can a link be associated with more Can a link be associated with more
than two processes?than two processes? How many links can there be How many links can there be
between every pair of between every pair of communicating processes?communicating processes?
13-Sep-200613-Sep-2006 99
Implementation Implementation QuestionsQuestions
What is the capacity of a link?What is the capacity of a link? Is the size of a message that the Is the size of a message that the
link can accommodate fixed or link can accommodate fixed or variable?variable?
Is a link unidirectional or bi-Is a link unidirectional or bi-directional?directional?
13-Sep-200613-Sep-2006 1010
Direct CommunicationDirect Communication
Processes must name each other Processes must name each other explicitly:explicitly:sendsend ( (P, messageP, message) – send a ) – send a
message to process Pmessage to process PReceive Receive ((Q, messageQ, message) – receive a ) – receive a
message from process Qmessage from process Q
13-Sep-200613-Sep-2006 1111
Direct CommunicationDirect Communication
Properties of communication linkProperties of communication link Links are established automatically.Links are established automatically. A link is associated with exactly one A link is associated with exactly one
pair of communicating processes.pair of communicating processes. Between each pair there exists Between each pair there exists
exactly one link.exactly one link. The link may be unidirectional, but is The link may be unidirectional, but is
usually bi-directional.usually bi-directional.
13-Sep-200613-Sep-2006 1212
Indirect CommunicationIndirect Communication
Messages are directed and Messages are directed and received from mailboxes (also received from mailboxes (also referred to as ports).referred to as ports). Each mailbox has a unique id.Each mailbox has a unique id. Processes can communicate only if Processes can communicate only if
they share a mailbox.they share a mailbox.
13-Sep-200613-Sep-2006 1313
Indirect Communication …Indirect Communication … Properties of communication linkProperties of communication link
Link established only if processes Link established only if processes share a common mailboxshare a common mailbox
A link may be associated with many A link may be associated with many processes.processes.
Each pair of processes may share Each pair of processes may share several communication links.several communication links.
Link may be unidirectional Link may be unidirectional or bi-directional.or bi-directional.
13-Sep-200613-Sep-2006 1414
OperationsOperations create a new mailboxcreate a new mailbox send and receive messages through send and receive messages through
mailboxmailbox destroy a mailboxdestroy a mailbox
Primitives are defined as:Primitives are defined as:send send ((A, messageA, message) )
receive receive ((A, messageA, message))
Indirect Communication …Indirect Communication …
13-Sep-200613-Sep-2006 1515
Mailbox sharingMailbox sharingPP11, P, P22,, and and P P33 share mailbox A. share mailbox A.
PP11, sends; , sends; PP22 andand P P33 receive. receive.
Who gets the message?Who gets the message?
Indirect Communication …Indirect Communication …
13-Sep-200613-Sep-2006 1616
SolutionsSolutionsAllow a link to be associated with Allow a link to be associated with
at most two processes.at most two processes.Allow only one process at a time Allow only one process at a time
to execute a receive operation.to execute a receive operation.Allow the system to select Allow the system to select
arbitrarily the receiver. Sender is arbitrarily the receiver. Sender is notified who the receiver was.notified who the receiver was.
Indirect Communication …Indirect Communication …
13-Sep-200613-Sep-2006 1717
SynchronizationSynchronization Message passing may be either Message passing may be either
blocking or non-blocking.blocking or non-blocking. BlockingBlocking is considered is considered
synchronoussynchronous Non-blockingNon-blocking is considered is considered
asynchronousasynchronous sendsend and and receivereceive primitives may primitives may
be either blocking or non-blocking.be either blocking or non-blocking.
13-Sep-200613-Sep-2006 1818
BufferingBuffering Queue of messages attached to the Queue of messages attached to the
link; implemented in one of three ways.link; implemented in one of three ways.
Zero capacityZero capacity – No messages – No messages Sender Sender must wait for receivermust wait for receiver
Bounded capacityBounded capacity – – nn messages messagesSender must wait if link full.Sender must wait if link full.
Unbounded capacityUnbounded capacity – infinite length – infinite length Sender never waits.Sender never waits.
13-Sep-200613-Sep-2006 1919
UNIX/Linux IPC ToolsUNIX/Linux IPC Tools PipePipe Named pipe (FIFO)Named pipe (FIFO) BSD SocketBSD Socket TLITLI Message queueMessage queue Shared memoryShared memory Etc.Etc.
13-Sep-200613-Sep-2006 2020
UNIX/Linux Pipe Important system calls
pipe, read, write, close pipe: Create a pipe for IPC read: Read from a pipe write: Write data to a pipe close: Close/destroy a pipe
13-Sep-200613-Sep-2006 2121
File Descriptors in UNIX/Linux
An integer returned by open() system call
Used as an index in the per process file descriptor table (PPFDT)
Used in read, write, and close calls
13-Sep-200613-Sep-2006 2222
File Descriptors in UNIX/Linux
Size of PPFDT is equal to the number of files that a process can open simultaneously (OPEN_MAX in Linux—see <linux/limits.h>
Used as an index in the per process file descriptor table (PPFDT)
Used in read, write, and close calls
13-Sep-200613-Sep-2006 2323
UNIX/Linux Pipe Important characteristics of a pipe
Stream of bytes Used as half-duplex channel Bounded buffer Maximum data written is PIPE_BUF
(defined in <sys/param.h> under UNIX and in <linux/param.h> in Linux)—5120 and 4096, respectively
13-Sep-200613-Sep-2006 2424
#include <unistd.h> int pipe (int filedes[2]);
Synopsis of pipe System Call
13-Sep-200613-Sep-2006 2626
/* 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
13-Sep-200613-Sep-2006 2727
rc = pipe (pipefd);
if (rc < 0) {
perror("pipe");
exit(1);
}
pid = fork ();
if (pid < 0) {
perror("fork");
exit(1);
}
Sample Code
13-Sep-200613-Sep-2006 2828
if (pid == 0) { /* Child’s Code */
close(pipefd[0]);
write(pipefd[1], testString, strlen(testString));
close(pipefd[1]);
exit(0);
}
Sample Code
13-Sep-200613-Sep-2006 2929
/* 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);
Sample Code